Picaxe 18x code
This is the listing for the Pixace 18x code. This is the main body of the system and can be implemented independently as described in the videos.
Comments please.
Enjoy.
#rem 0.15
Picaxe 18x water dampness @ Anobium
based on the concept by Ken Moffett 311209 and MPep.
Uses simple probe in the basement (cellar) and checks for dampness, moisture and water.
The UI consists of audible alarm, visual alerts, device reset/restart, mute audible alarm, test device and output to an option K107 display.
The UI device shows:
Power On LED (not controlled by Picaxe)
Sensor On LED (output controlled by Picaxe but hardware flashes the LED)
Alarm LED (output controlled by Picaxe and shows pulses related to current status and past status)
Muted LED (output controlled by Picaxe)
Mute Button (interrupts Picaxe to mute alarm, therefore controlled by Picaxe)
Test Button (raises input on Picaxe. An OPTIRELAY connects sensors (IN and OUT) together to check device.
Power Reset button (hardware reset for Picaxe and all other hardware)
XML definition
DZ - Data Schema defintion. Required for Web Service.
V1 - Integer: Current value returned from sensor
H1 - Integer: Highest historical value returned from sensor
S1 - Integer: Wet count value. Returns the value of WetCount.
M1 - Integer: Returns 1 for muted and 0 for not muted
A1 - String: String value of the current status.
Typical usage;
<dz><v1>Picaxe</v1><h1> </h1><s1>Booting</s1><c1>0</c1><a1> </a1></dz>
<dz><v1>0</v1><h1>1</h1><s1>Dry</s1><c1>0</c1><a1>Dry</a1></dz>
#endrem
#picaxe 18x
' Variables defined in memory
symbol HSval = b0 'Sensor value
symbol Muted = b1 'State variable. Siren status. Muted/Unmuted
symbol Loopx = b2 'For/Next loop usage
symbol MaxHSval= b3
symbol WetCount = b4
' Double byte variables defined in memory
symbol Mask = w3 'Bitwise variable used during LED flash routine
symbol Value = w4 'Temp variable used during LED flash routine
symbol AlertVal = w5 'Alert variable
symbol HiAlertVal = w6 'State variable. Historical alert variable
' Constants. Return values from the sensor
symbol xlimit = 22 'It's WET!
symbol moist = 17 'It's MOIST!
symbol damp = 12 'It's DAMP!
' These are binary values flash of the alert LED!
symbol WetAlert = 65535 ' On all the time
symbol WasWetalert = 61680 ' On/Off....
symbol MoistAlert = 61455 ' On/Off....
symbol DampAlert = 21845 ' On/Off....
symbol BigDelay = 1000
' Port assignments
symbol LedAlertPort = 0 'needs to be an output
symbol SirenPort = 1 'needs to be an output
symbol PwmPort = 3 'needs to be an output
symbol MutedPort = 2 'needs to be an output
symbol LCDPort = 4 'needs to be an output
symbol BuzzerPort = 5 'needs to be an output
symbol SerialPort = 6 'needs to be an output
symbol SensorPort = 0 'needs to be an input
symbol MutePortPin = pin1 'needs to be an input
symbol MutePort = 1 'needs to be an input
symbol TestPort = pin7 'needs to be an input
' Commence program
' reset PWM port
pwmout PwmPort, off
for loopx = 0 to 2
toggle SirenPort 'turn off/on Siren... Picaxe is operational
pause 75
next
' reset the ports
low LedAlertPort 'turn off the Alert LED
low SirenPort 'turn off the Siren
low MutePort 'turn off the Mute LED
' set state of global state variables
Muted = 0
HiAlertVal = 0
MaxHSval = 0
pwmout PwmPort,249,500 'PWM output 4kHz@50% to sensor driver probe
setint %00000010,%00000010 'Setup interupt for Mute button on Pin1
' Setup K107
High LCDPort ' Be sure Tx Pin is idle for some time
Pause 1000 ' wait for PICAXE LCD to boot
SerOut LCDPort, T2400, ("?G420") ' configure LCD as 4 X 20
pause 200
SerOut LCDPort, T2400, ("?G420") ' configure LCD as 4 X 20
pause 200
SerOut LCDPort, T2400, ("?!01") ' configure LCD as 4 X 20
pause 200
SerOut LCDPort, T2400, ("?f")
SerOut LCDPort, T2400, ("Booted")
for b0 = 0 to 10
serout SerialPort,N2400,("<dz><v1>Picaxe</v1><h1> </h1><s1>Booting</s1><c1>0</c1><a1> </a1></dz>",cr,lf)
pause 1000
next
Main:
serout SerialPort,N2400,("<dz>")
readadc SensorPort,HSval 'read value from the probe
serout SerialPort,N2400,("<v1>",#HSval,"</v1>")
SerOut LCDPort, T2400, ("?f") 'Output to K107 LCD
pause 20
SerOut LCDPort, T2400, ("?y3?x00v0c?y0?x00") 'Output the version of firmware
pause 200
if HSval > MaxHSval then
MaxHSval = HSval 'Store highest sample value
end if
serout SerialPort,N2400,("<h1>",#MaxHSval,"</h1>")
if HSval > 99 then
HSval = 99
end if
if HSval > xlimit then 'Test the returned value
if Muted = 0 then 'Test mute state
if TestPort is Off then
high SirenPort 'Turn on siren
else if WetCount => 10 then
high SirenPort 'Turn on siren
WetCount = 10
end if
end if
inc WetCount ' Increment WetCount
SerOut LCDPort, T2400, ("Wet=", #WetCount) 'Output to K107 LCD
serout SerialPort,N2400,("<s1>Wet</s1>")
AlertVal = WetAlert 'Set LED alert sequence value
HiAlertVal = WasWetAlert 'Set historical alert value
else if HSval > moist then 'Test the returned value
SerOut LCDPort, T2400, ("Moist ") 'Output to K107 LCD
serout SerialPort,N2400,("<s1>Moist</s1>")
AlertVal = moistalert 'Set LED alert sequence value
if AlertVal => HiAlertVal then 'Test historical value
HiAlertVal = MoistAlert 'Set historical alert value
end if
else if HSval > damp then 'Test the returned value
SerOut LCDPort, T2400, ("Damp ") 'Output to K107 LCD
serout SerialPort,N2400,("<s1>Damp</s1>")
AlertVal = dampalert 'Set LED alert sequence value
if AlertVal => HiAlertVal then 'Test historical value
HiAlertVal = DampAlert 'Set historical alert value
end if
AlertVal = %0
else 'Must be dry!
AlertVal = %0 'Set LED alert sequence value
SerOut LCDPort, T2400, ("DRY ") 'Output to K107 LCD
serout SerialPort,N2400,("<s1>Dry</s1>")
Pause 30
WetCount = 0
end if
serout SerialPort,N2400,("<c1>", #WetCount ,"</c1>")
if HSval <= damp then 'Test the current value is less then Wet
low SirenPort 'Silence siren
Muted = 0 'Set mute state
low MutedPort 'Turn off Mute LED
low LedAlertPort 'Turn off Alert LED (ensure it is off!)
end if
pause 20 'Pause for K107
if Muted = 1 then
serout SerialPort,N2400,("<m1>1</m1>")
SerOut LCDPort, T2400, ("?y0?x15Muted") 'Output to K107 LCD
pause 15
else
serout SerialPort,N2400,("<m1>0</m1>")
end if
if HiAlertVal <>0 then
SerOut LCDPort, T2400, ("?y2?x15Been?y3?x15") 'Output to K107 LCD
pause 20
end if
SerOut LCDPort, T2400, ("?y0?x06?>3",#HSval, "?<") 'Output to K107 LCD
pause 20
SerOut LCDPort, T2400, ("?y3?x15")
pause 10
if HiAlertVal = WasWetAlert then
SerOut LCDPort, T2400, ("Wet") 'Output to K107 LCD
pause 5
serout SerialPort,N2400,("<a1>Been Wet</a1>")
end if
if HiAlertVal = MoistAlert then
SerOut LCDPort, T2400, ("Moist") 'Output to K107 LCD
pause 5
serout SerialPort,N2400,("<a1>Been Moist</a1>")
end if
if HiAlertVal = DampAlert then
SerOut LCDPort, T2400, ("Damp") 'Output to K107 LCD
pause 5
serout SerialPort,N2400,("<a1>Been Damp</a1>")
end if
if HiAlertVal = 0 then
serout SerialPort,N2400,("<a1>None</a1>")
end if
serout SerialPort,N2400,("</dz>",cr,lf)
Gosub flash 'Flash the Alert LED based on the variable AlertVal
goto main
flash: 'Test current AlertVal
if AlertVal <> 0 then 'Set Value variable. This variable is destroyed during this process
Value = AlertVal
else
Value = HiAlertVal
end if
if Value <> 0 then 'Test for Zero value, no need to change LED state, should be off anyway.
low LedAlertPort 'Turn off the AlertLED
For Loopx = 1 To 16 'Loop through the double byte value bit by bit
Mask = Value & 32768 'Using a mask examine the uppermost bit
If Mask = 32768 Then 'Test mask value. It will be 0 or 1
high LedAlertPort 'Turn on LED
Else
low LedAlertPort 'Turn off LED
End if
Value = Value * 2 'Shuffle the bits
pause 125 'Delay
Next Loopx
else
pause 2000 'Delay
end if
pause BigDelay
return;
interrupt:
SerOut LCDPort, T2400, ("?fHiVal")
pause 10
SerOut LCDPort, T2400, ("?y0?x06?>3",#MaxHSval, "?<")
pause 25
do while MutePortPin is on 'Debounce routine
high BuzzerPort 'Set Buzz on
pause 50
low BuzzerPort 'Set Buzz off
pause 50
loop
if Muted = 0 then 'Test current state of MUTE
Muted = 1 'Set state
high MutedPort 'Turn LED on
low SirenPort 'Turn Siren off
else
Muted = 0 'Set state
low MutedPort 'Turn LED off
end if
SerOut LCDPort, T2400, ("?f")
pause 2
setint %000000010,%00000010 'Reset mute button interupt
return