; *******************************
; Written by: Reg Pye
; Function: DFPlayer security board
; Target PICAXE: 14M2
; *******************************
Symbol BAUD_FREQ = M8
Symbol BAUD = T9600_8
Symbol MP3_SET_VOLUME = $06 ; Set the volume
Symbol MAX_VOLUME = $1E ; Max volume level, $1E = 30
Symbol Busy_Delay = 200 ; Change Busy delay value here
Symbol PoliceFlash = B.1 ;power mosfet
Symbol FloodLED = B.2 ;power mosfet
Symbol Busy_Pin = PinB.3 ; DF Player Busy Pin
Symbol TX = B.4 ;DFPlayer RX
Symbol WarningLED = B.5
Symbol In3 = PinC.0 ;Final warning - sound 0005
Symbol In2 = PinC.1 ;Second entry - sound 0004
Symbol In1 = pinC.2 ;First entry - sound 0003
Symbol PIR = pinC.3 ;Main entry - sound 0001 day, 0002 night
Symbol LDR = C.4
Symbol LDR_DAY = 105 ; Greater or equal this it's day
Symbol LDR_NIGHT = 95 ; Lower or equal this it's night
Symbol reserveW0 = w0 ; b1:b0
Symbol dayOrNight = w1 ; Set to IS_DAY or IS_NIGHT
Symbol IS_DAY = 0 ; Set 'dayOrNight' to 0 to use 'On dayOrNight Goto Day, Night'
Symbol IS_NIGHT = 1 ; Set 'dayOrNight' to 1 to use 'On dayOrNight Goto Day, Night'
Symbol countdown = b7 ; countdown for do-loop, change value to suit time needed
;---------------------------------------------------------
Main:
;wait 45 ; Allow PIR to settle down before code starts
;wait 45 ; two waits because of 65 is the limit
;---------------------------------------------------------
What_time:
Gosub DetermineDayOrNight
On dayOrNight Goto Day, Night
;---------------------------------------------------------
Day:
#IfDef SIMULATING
If dayOrNight = IS_DAY Then
SerTxd("Day")
Else
SerTxd("Night")
EndIf
#EndIf
low FloodLED ; turn off light at day
SerTxd(" Floodlight off", CR, LF)
if PIR = 1 then gosub Day_warning ;Initial warning
if In2 = 1 then gosub first_entry ; second warning
if In3 = 1 then gosub second_entry ;Third warning
if In1 = 1 then gosub Final_warning ; Final warning
goto main
;---------------------------------------------------------
Night:
#IfDef SIMULATING
If dayOrNight = IS_DAY Then
SerTxd("Day")
Else
SerTxd("Night")
EndIf
#EndIf
high FloodLED ; turn on light at night
SerTxd(" Floodlight on", CR, LF)
if PIR = 1 then gosub Night_warning ;Initial warning
if In2 = 1 then gosub first_entry ; Second warning
if In3 = 1 then gosub second_entry ;Third warning
if In1 = 1 then gosub Final_warning ; Final warning
goto main
;---------------------------------------------------------
DetermineDayOrNight:
ReadAdc LDR, b10 ; Read LDR and set 'dayOrNight'
If b10 > LDR_DAY Then
dayOrNight = IS_DAY
End If
If b10 < LDR_NIGHT Then
dayOrNight = IS_NIGHT
End If
Return
;---------------------------------------------------------
Day_warning:
SerTxd("Day_warning sound 0001", CR, LF)
SetFreq BAUD_FREQ
Pause 10
SerOut TX, BAUD, ( $7E, $FF, $06, $0F, $00, $01, $01, $EF )
SerTxd(" ", CR, LF)
gosub Check_Busy
SetFreq MDEFAULT
; further code to be added here (maybe)
Return
;---------------------------------------------------------
Night_warning:
SerTxd("Night_warning sound 0002", CR, LF)
SetFreq BAUD_FREQ
Pause 10
SerOut TX, BAUD, ( $7E, $FF, $06, $0F, $00, $01, $02, $EF )
SerTxd(" ", CR, LF)
gosub Check_Busy
SetFreq MDEFAULT
; further code to be added here (maybe)
Return
;---------------------------------------------------------
Check_Busy:
Do
pause Busy_Delay
loop while Busy_Pin = 0
Return
;---------------------------------------------------------
first_entry: ;PinC.2
SerTxd("First Entry breach sound 0003", CR, LF)
SetFreq BAUD_FREQ
Pause 10
SerOut TX, BAUD, ( $7E, $FF, $06, $0F, $00, $01, $03, $EF )
SerTxd(" ", CR, LF)
gosub Check_Busy
SetFreq MDEFAULT
; further code to be added here (maybe)
return
;---------------------------------------------------------
second_entry: ;PinC.1
SerTxd("Second Entry breach sound 0004", CR, LF)
SetFreq BAUD_FREQ
Pause 10
SerOut TX, BAUD, ( $7E, $FF, $06, $0F, $00, $01, $04, $EF )
SerTxd(" ", CR, LF)
gosub Check_Busy
SetFreq MDEFAULT
; further code to be added here (maybe)
return
;---------------------------------------------------------
Warning_Led: ;B.5
; further code to be added here (maybe)
Return
;---------------------------------------------------------
Final_warning:
SerTxd("PoliceFlash on, sound 0005", CR, LF)
high PoliceFlash
SetFreq BAUD_FREQ
Pause 10
SerOut TX, BAUD, ( $7E, $FF, $06, $0F, $00, $01, $05, $EF )
SerTxd(" ", CR, LF)
gosub Check_Busy
SetFreq MDEFAULT
goto Timing
Return
Timing: ;PROBLEM HERE - needs to be able to allow other triggers to operate at the same time. B.1 once started will flash for 3 minutes (hopefully)
do
if time >10 then ;to be set for about 3 minutes, alarms have to finish in 3 minutes by law.
low PoliceFlash Time = 0 ;turn off lights, reset time
endif
SerTxd(" Time ",#time, CR, LF)
loop while time >0
Return