#PICAXE 18M2
start0:
' this version of code amended For 8-9-17
'4 Beams NC to 5V+ . LDR to 5V+, Panel has on board isolation switches for each zone
'Day/Night LED, Lights on LED and 4 zone led's
symbol LIGHTS1 = B.0 ; lights relay 1 all outputs are neg high
symbol buzzer1 = B.1 ; Buzzer output
symbol zone1led = B.2 ; Zone 1 LED
symbol zone2led = B.3 ; Zone 2 LED
SYMBOL zone3led = B.4 ; Zone 3 LED
SYMBOL zone4led = B.5 ; Zone 4 LED
SYMBOL day_or_night_LED = B.7 ; Day/Night LED, on at night
SYMBOL Zone1 = pinC.2 ; trigger from detector 1
SYMBOL Zone2 = pinC.5 ; trigger from detector 2
SYMBOL Zone3 = pinC.6 ; trigger from detector 3
SYMBOL Zone4 = pinC.7 ; trigger from detector 4
symbol TIMEPOT = C.1 ; potentiometer input for lights on time on board
symbol LDR = C.0 ; LDR for night operation to 5V+
SYMBOL Night_Day_Setpoint = 38 ;LDR to + 5v
SYMBOL lights_on = b0 ; reserved for bit flags
symbol lights1_on = bit0 ; flag = 1 if the zone 1 lights on
symbol lights2_on = bit1 ; flag = 1 if the zone 2 lights on
symbol lights3_on = bit2 ; flag = 1 if the zone 3 lights on
symbol lights4_on = bit3 ; flag = 1 if the zone 4 lights on
SYMBOL time_for_lights = b2 ; how long to leave the lights on
symbol night_or_day = b3 ; reading from the LDR
symbol light_loop = b4 ; loop control when lights are activated
symbol light_count = b5 ; count how long to leave the lights on
symbol light1_count = b6 ; zone 1 lights on while >0
symbol light2_count = b7 ; zone 1 lights on while >0
symbol light3_count = b8 ; zone 1 lights on while >0
symbol light4_count = b9 ; zone 1 lights on while >0
'======================================================================
Init:
' set all B pins as outputs and low
let dirsB = %11111111
let pinsB = %00000000
Main:
' Main program loop
' 1 buzz subroutine is required
'
' we will loop forever
DO
' read the time for lights to be on (from pot)
readadc TIMEPOT ,time_for_lights
readadc LDR ,night_or_day
' depending on whether it is night or day act differently
if night_or_day < Night_Day_Setpoint then
' NIGHT time operation - lights and buzzer
' plus keep looping here while another loop has been broken
lights_on = 0
light_loop = 0
light_count = 0
do ; Zone NC to 5V+ so 1 is clear
if Zone1 = 0 then ; test if zone 1 is (still) active
Pause 50 ;anti bounce delay
high LIGHTS1 ; turn on the zone 1 lights
gosub buzz1 ; sound the zone 1 buzzer
lights1_on = 1 ; set the zone1 activity flag
light1_count = time_for_lights ; keep the zone 1 "timer" at max until zone 1 cleared
end if
if Zone2 = 0 then ; test if zone 2 is (still) active
Pause 50 ;anti bounce delay
high LIGHTS1
gosub buzz1
lights1_on = 1
light1_count = time_for_lights
end if
if Zone3 = 0 then ; test if zone 3 is (still) active
Pause 50 ;anti bounce delay
high LIGHTS1
gosub buzz1
lights1_on = 1
light1_count = time_for_lights
end if
if Zone4 = 0 then
Pause 50 ;anti bounce delay
high LIGHTS1
gosub buzz1
lights1_on = 1
light1_count = time_for_lights
end if
IF lights_on > 0 THEN ; check if ANY zone is active
light_loop = 1 ; set the general alarm flag
pause 500 ; wait a while
IF light1_count>0 THEN ; if the zone 1 "timer" is active
DEC light1_count ; then decrement the remaining period
ELSE
LOW LIGHTS1 ; otherwise if zone 1 timer at zero
lights1_on = 0 ; turn lights off and clear activity flag
ENDIF
IF light2_count>0 THEN
DEC light2_count
ELSE
;LOW LIGHTS1
lights2_on = 0
ENDIF
IF light3_count>0 THEN
DEC light3_count
ELSE
;LOW LIGHTS1
lights3_on = 0
ENDIF
IF light4_count>0 THEN
DEC light4_count
ELSE
;LOW LIGHTS1
lights4_on = 0
ENDIF
ELSE
light_loop = 0
ENDIF
' now check whether to keep looping here
' if the light counter is set but is less than the duration check then keep looping
loop while light_loop = 1
else
' DAY time operation - just sound the buzzer
if Zone1 = 0 pause 50 then gosub buzz1 ;only one buzzer output
if Zone2 = 0 pause 50 then gosub buzz1 ;triggered by each beam
if Zone3 = 0 pause 50 then gosub buzz1 ;pause 50 anti bounce
if Zone4 = 0 pause 50 then gosub buzz1
end if
LOOP
'======================================================================
buzz1: ; buzz on off 4 times
high Buzzer1
pause 500
low Buzzer1
pause 500
high Buzzer1
pause 500
low Buzzer1
pause 500
high Buzzer1
pause 500
low Buzzer1
pause 500
high Buzzer1
pause 500
low Buzzer1
pause 500
return
start1: ;For LED indicators on panel
Main2:
if night_or_day < Night_Day_Setpoint then high day_or_night_led
else low day_or_night_led endif ; this gives a day night LED on the panel
; yellow LED on at night
if zone1 = 0 then low zone1led ; this has 4 LED's to show zone status
else high zone1led endif ; green LED on if zone clear
if zone2 = 0 then low zone2led
else high zone2led endif
if zone3 = 0 then low zone3led
else high zone3led endif
if zone4 = 0 then low zone4led
else high zone4led
endif
goto main2