The vaults are insufferably damp.

Tuesday, June 30, 2009

Watching Events

Suppose you have an event you want to monitor. It happens usually every 24 hours but could take as long as 72 hours, or longer in rare cases. Each time the event happens a log file is updated. Here's what I've come up with, using Powershell, Arduino, and a voltmeter:

#powershell - read time, update meter
$log = dir c:\logs\logfile.txt
$age = ((get-date) - $log.LastWriteTime).TotalHours
$meter = $age / 72.0
$serialPort.write("meter $meter")

Ok, that works, but you get a linear meter reading (0.0 to 1.0). Using a logarithmic scale makes sense here: Most of the time the event happens about daily. With a log scale the first day will take up the first 3/4 of the meter. If the meter gets beyond 75% (red zone?) something might be amiss.

$log = dir c:\logs\logfile.txt
$age = ((get-date) - $log.LastWriteTime).TotalHours
$meter = [math]::log($age) /4.27
$serialPort.write("meter $meter")

How does that distribute the time (from 0.0 to 1.0)? Try this:

1..15 | % {$_ * 6} | % {"$_ $([math]::log($_)/4.27)"}

If you run that (powershell again) you will see that at 24 hours you are using up to 0.74778. After 48 hours you get 0.91, and 72 hours is at 1.0.

For the Arduino you can make it easier by writing "meter $($meter * 256)". Then you can analogWrite the value to one of the PWM pins and connect that to your voltmeter. If the meter in question has a 3V scale you would use a voltage divider to scale the maximum 5V out to 3V full scale.

I currently have the Powershell side of this working, but instead of the Arduino I'm running the serial out through the MixW serial port bridge and back into a Processing applet.

No comments: