; This is the bit of progam that runs while the alarm is unarmed.
; It's at the top so will be run whenever Rudolph gets power.
; We want to start in an unarmed state so that's ok.
unarmed:
; Check to see if the button is pressed.
if input3 != 0 then
; If it isn't, then briefly pause then go back to the top of the unarmed loop.
pause 100
goto unarmed
end if
; If we get to here, then the button has been pressed.
; Wait until the button is released by calling the wait_for_release sub-routine
; that is defined near the bottom of the program
gosub wait_for_release
; Flash the nose for five seconds to show that the alarm will be armed
; if the button is pressed again.
for b0 = 1 to 5
high 0
; If the button is pressed then exit the loop.
if input3 = 0 then exit
pause 1000
low 0
; If the button is pressed then exit the loop.
if input3 = 0 then exit
pause 1000
next b0
; We've finished the loop, either because the five seconds ran out
; or the button was pressed.
; Is the button pressed?
if input3 = 0 then
; Yes it is! Wait until it is released.
gosub wait_for_release
; Flash the nose quickly three times to show that the sytem is now armed.
for b0 = 1 to 3
high 0
pause 200
low 0
pause 200
next b0
; And go over to the armed bit of the program.
goto armed
end if
; If we get to here then the button wasn't pressed during the five seconds
; that the nose was flashing so we jump back to the unarmed bit of the program
; and start again waiting for a button press.
goto unarmed
; This is the bit of program that runs while the alarm is armed
armed:
; The alarm is now armed so keep an eye on the light level
readadc 1,b1
; Has someone turned on the light?
; If they have, then it's time to alarm!
; Sound the alarm if the brightness is over 75%
if b1 > 75 then sound_alarm
; Has someone pressed the button to try and unarm the system?
if input3 = 0 then
; Yes, they have.
; The eyes are flashed five times and you need to let go
; of the button between the third and fourth flashes to unset
; it, or it will sound.
for b0 = 1 to 5
high 4
pause 1000
low 4
if input3 != 0 then exit
pause 1000
next b0
if b0 != 3 then sound_alarm
; Show that the system has been unarmed by flashing the eyes
; quickly.
for b0 = 1 to 3
high 4
pause 200
low 4
pause 200
next b0
goto unarmed
end if
; Alarm wasn't sounded and the button wasn't pressed.
; Go back to the start of the armed loop to check everything
; again, after a short pause to save battery and stop things getting
; too hot.
pause 100
goto armed
; This is the bit of code that sounds the alarm.
sound_alarm:
; Play Rudoplh
play 3,3
; Now unarmed
goto unarmed
; This is a sub-routine that waits until the button isn't pressed any more.
; It is used by other parts of the program.
wait_for_release:
do
pause 100
loop until input3 != 0
return