' Real Time Power Meter LED detector / Counter
' This will go on our power meter, and hopefully send data in the house for
' Real-time monitoring of power consumption
'
' First, need a circuit to detect the IR LED and send clear logic signals
' To the Picaxe...may be onto something...
' Using a varient of a circuit found here: http://ciseco.co.uk/forum/viewtopic.php?f=15&t=59
' Will use INPUT 2 on the picaxe 8m, as it's a Schmitt Trigger level input
' Input is normally high, goes low on a pulse
'
' Built for Picaxe 08M
' March 2010
' A Standard Picaxe 08M
' __________
' 5V VCC 1- | |- 8 0V GND
' Serial In 2- | |- 7 Out 0 / Serial Out / Infraout
' ADC 4 / Out 4 / In 4 3- | 08M |- 6 In 1 / Out 1 / ADC 1
' Infrain / In 3 4- | |- 5 In 2 / Out 2 / ADC 2 / pwm 2 / tune
' ----------
'
' The LED Detector / Counter / Transmitter
' __________
' 5V VCC 1- | |- 8 0V GND
' Serial In 2- | |- 7 Serial Out
' LED Status 3- | 08M |- 6
' 4- | |- 5 LED Flashing Trigger input
' ----------
'
' Available Bytes: b0 - b13
' Available Words: w0 - w6
'
' Currently 168 Bytes out of 256
#Picaxe08m ' Instruct Compiler to use a Picaxe 08M Chip
' *** Declare Variables
Symbol LEDin = pin2 ' Using input 2 as it's a Schmitt Trigger input, which has the logic
' levels I need to make this work
Symbol LEDout = 4 ' Something to blink and help develop circuit
Symbol ElapsedTime = w6 ' b12 and b13 now unavailable due to w6
Symbol Countdown = w5 ' b10 and b11 now unavailable due to w5
Symbol PreviousTime = w4 ' Previous elapsedTime b8 & b9 now unavailable
' *** End Declare Section
' *** Start Init Section / set up variables
Low LEDout
ElapsedTime = 0
Countdown = ElapsedTime + 1500 ' Countdown is 15 seconds (give or take)
setint %00000000, %00000100 ' Set interrupt on input 2, going low (first #=input, Second #=mask)
' *** End Init Section
main: ' The Main program
' Want to send an updaet every 15 seconds regardless of value change
do while ElapsedTime < Countdown
pause 10 ' wait 10 ms (may adjust for fine tuning later)
inc ElapsedTime 'ElapsedTime = ElapsedTimei + 1
loop
gosub Update ' loop complete; so 15 seconds has elapsed with no pulse (or same freq)
goto main ' Start over
interrupt:
' ToDo: In here, need to take elapsed time, compare with previous value (to see if changed)
' If changed, then transmit new elapsed time between pulses to serial (RF later)
' If not changed, reset ElapsedTime counter and
' And jump back to program
If ElapsedTime = PreviousTime Then
ElapsedTime = 0
' There's a little bug here; if for some reason, the flashes come in the same frequency
' (exactly) then there will be no updates until the frequency changes. As resetting
' ElapsedTime means it will never be = or > Countdown
' Will look and see if this is an issue down the road; initial testing indicates
' it won't be a problem; value should always be changing...
Else
Gosub Update ' Send the new time...
PreviousTime = ElapsedTime 'Reset the compare variable
ElapsedTime = 0 ' Reset Elapsed Time
Endif
do while LEDin <> 1 ' Wait for interrupt condition to clear before re-arming
loop
setint %00000000, %00000100 ' Set interrupt on input 2, going low (first #=input, Second #=mask)
Return
Update: ' Here is where we transmit the time between pulses on serial
high LEDout
Setint OFF ' Turn the interrupt off to prevent it from triggering during x-mit
sertxd("Elapsed Time Between Pulses: ",#ElapsedTime," centi-seconds",13,10)
Gosub Reset_Countdown ' may be able to remove this later for space...
do while LEDin <> 1 ' Wait for interrupt condition to clear before re-arming
loop
setint %00000000, %00000100 ' Set interrupt on input 2, going low (first #=input, Second #=mask)
low LEDout
Return
Reset_Countdown: 'This sets the 15 second count down timer...system should update every
' 15 seconds even if no pulse is detected...elapsedtime will continue to
' increment, so this will show decaying power consumption
' Until 11 minutes...where the counter rolls over...then the universe explodes
Countdown = ElapsedTime + 1500
if Countdown < ElapsedTime Then
' What do do if roll-over? 65535
' Roll-over means nearly 11 minutes have elapsed without a flash...
' Either a power out, or I've gone crazy and unplugged EVERYTHING
' Let's assume I'm crazy
ElapsedTime = 0 ' For the moment, just reset and start over...unlikely to see this event
Countdown = ElapsedTime + 1500
endif
Return
end