PIR sensor

regpye

New Member
I am thinking about a new project that will need a PIR sensor. The circuit will be using a 14M2 chip, and the board has already been made and has a DRPlayer, LDR, 5 switched inputs and 4 high power mosfet outputs, plus power supply suited for 12 volt plus input.
I want to design a security system for the side yard entry area. I have very bright flashing red/blue lights that run on 12 volts, the DRPlayer could be used for sounding a warning message(s), plenty of spare power outputs and inputs available for other things not thought of yet.
I am not sure how the PIR should be connected, I am guessing it is just a high/low switch using the signal pin?
Any advice on PIR systems and coding would be appreciated.
 

cpedw

Senior Member
This PIR runs on anything 3-15V and gives high/low output that can be connected direct to a picaxe input if the supply voltage is kept suitably low.
 

hippy

Technical Support
Staff member
PIR sensors and modules will usually have a signal which switches between 0V and supply voltage, or have an open-collector output which will pull a pulled-up input pin down to 0V.

Traditional burglar alarm PIR units will usually have a relay contact operating as a switch, which can also act as 'open-collector' by connecting one side to 0V.
 

regpye

New Member
PIR sensors and modules will usually have a signal which switches between 0V and supply voltage, or have an open-collector output which will pull a pulled-up input pin down to 0V.

Traditional burglar alarm PIR units will usually have a relay contact operating as a switch, which can also act as 'open-collector' by connecting one side to 0V.
Thanks Hippy, that makes it clearer for me.
 

69-cat

Member
I use PIRs for all of my Halloween prop controllers. On first power up, I have a total of a 90 second delay the let the PIR settle down then the code starts.
Dave
 

regpye

New Member
I bought some HC-SR505 and then found SR501 that have more features, so I now have plenty of both for various project in the future.
I feel this could be a fun project that can have several features added to it to utilise the 14M2 chip more fully.
 

regpye

New Member
I rewrote the code again and saved in the usual way and it happened once again. I rewrote it again and this time copied it to the clipboard and pasted into a notepad editor so I wouldn't loose my work. I could then load it into PE6 and continue programming, but on save it messed up once more.
Also when running the code for testing, I haven't done a lot, I am getting a stack overflow message at the end for some reason.
The code that has been saved in the notepad editor is attached. Can someone please check it out for me?
 

Attachments

papaof2

Senior Member
Simulation stops at line 52 with:

Stack error - return without gosub.

Why do you have a "return" two lines above the DetermineDayOrNight sub? Perhaps a leftover from something else?
 

regpye

New Member
Okay, I am in trouble again.
I have written my code and it works to some extent, but I do not know how to overcome a small problem.
In the code section Final_warning I have a gosub to Police_Flash and that is stopping the code in Final_Warning from processing.
What I wanted to happen is for the sound file to play in Final_Warning and the lights to be displayed for 3 minutes in Police_Flash.
I originally had the Police_Flash within the code of Final_Warning and that worked fine, except the program would stop until the 3 minutes were up. I would like to have the rest of the code able to process any other triggers during this 3 minute hold up. Maybe it is not possible and I will have to put the code back into Final_Warning and just wait the time out?
In the code I have remmed out some of the loops to reduce the time for testing purposes in the 3 minute section.

Code:
; *******************************
;    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'


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 Police_Flash ; 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
  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
   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
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
return
;---------------------------------------------------------
Final_warning:        ;B.1 once started will flash for 3 minutes
SerTxd("PoliceFlash sound 0005", CR, LF)

 SetFreq BAUD_FREQ
 Pause 10
 SerOut TX, BAUD, ( $7E, $FF, $06, $0F, $00, $01, $05, $EF )
  SerTxd(" ", CR, LF)
 gosub Check_Busy
 SetFreq MDEFAULT
 
gosub Police_Flash
 Return
;---------------------------------------------------------
Warning_Led:     ;B.5
; further code to be added here
Return
;---------------------------------------------------------
Police_Flash:        ;Flashing red/blue lights, possibly add siren through an additional relay.
high PoliceFlash
 for b9 = 1 to 1;80     ; 180 = 3 minutes
pause 60000         ; wait 60 seconds
next b9
low  PoliceFlash        ; stop flashing lights
 Return
 

papaof2

Senior Member
If you want something to run 3 minutes but for the PICAXE to continue the program, you could use the PICAXE to trigger a 555 timer configured for 3 minutes that then turned on power to the device. You'd need the 555 chip and perhaps a half dozen resistors and capacitors - think not much bigger than a postage stamp. The 555 can handle a maximum of 200ma or so - you need to check its datasheet for that and the maximum voltage. Most of the PICAXE chips are not able to do totally independent multi-tasking.
 

regpye

New Member
If you want something to run 3 minutes but for the PICAXE to continue the program, you could use the PICAXE to trigger a 555 timer configured for 3 minutes that then turned on power to the device. You'd need the 555 chip and perhaps a half dozen resistors and capacitors - think not much bigger than a postage stamp. The 555 can handle a maximum of 200ma or so - you need to check its datasheet for that and the maximum voltage. Most of the PICAXE chips are not able to do totally independent multi-tasking
Thanks for that suggestion papaof2.
I have many 555 chips, but that is not what I would do in this case.
I have thought during the night that it may be as simple as reprogramming using multi-tasking with the 14M2.
If I have a few starts and one of them is for the section that is the concern, (would be as two programs running together at the same time)
I will give that some further thought.
 

regpye

New Member
I have thought during the night that it may be as simple as reprogramming using multi-tasking with the 14M2.
I have reprogrammed the code and it is working as I wanted, BUT under multitasking the frequency can't be changed so I had to rem those parts out for testing. So maybe I cannot use this approach to make the code as the freq may need to be set for the DRPlayer.
PLEASE IGNORE THE FILE ATTACHED, it doesn't work properly.
 

Attachments

Last edited:

PieM

Senior Member
In a main program :

Main:
Do
.....
Gosub ...
Gosub ...
If .... Gosub Police_Flash..
.....
If Time > 180 then
low PoliceFlash ; stop flashing lights
endif
.....
Loop
End

;---------------------------------------------------------
Police_Flash: ;Flashing red/blue lights, possibly add siren through an additional relay.
Time = 0
high PoliceFlash
Return
 

regpye

New Member
In a main program :

Main:
Do
.....
Gosub ...
Gosub ...
If .... Gosub Police_Flash..
.....
If Time > 180 then
low PoliceFlash ; stop flashing lights
endif
.....
Loop
End

;---------------------------------------------------------
Police_Flash: ;Flashing red/blue lights, possibly add siren through an additional relay.
Time = 0
high PoliceFlash
Return
I tried that, and it works to some extent, but I don't understand how it works or how to control it.
I need further information in order to implement it properly.
 

regpye

New Member
I did some testing and not sure if this is correct or not.

Code:
symbol PoliceFlash = b.1
Main:
enabletime
high PoliceFlash ; turn on flashing lights
Do
If Time > 10 then
endif
SerTxd(" Time is ", #time, CR, LF)
if time > 10 then Police_Flash
Loop
End
Police_Flash:
low PoliceFlash  ; turn off flashing lights
wait 5        ; wait so I can see what is going on
time = 0        ;reset time to zero
goto main
 

PieM

Senior Member
Rich (BB code):
Main:
Do
If input_pulse = 1 then  (or similar)
            Gosub Police_Flash
endif
if time >10 then
      low PoliceFlash
endif
loop

;------------------------------------
Police_Flash: 
Time = 0
high PoliceFlash
Return
 

regpye

New Member
Rich (BB code):
Main:
Do
If input_pulse = 1 then  (or similar)
            Gosub Police_Flash
endif
if time >10 then
      low PoliceFlash
endif
loop

;------------------------------------
Police_Flash: 
Time = 0
high PoliceFlash
Return
I have tried using this code in several forms, but although it works it doesn't do what is wanted.
There are four triggers in the total code and most of them only turn of a sound file, but one turns on a sound file and also a flashing light and possibly a siren as well. The law here says that warning sirens can only sound for 3 minutes, so I need to be able to turn off that section after 3 minutes. I have been able to code that, but at the same time I need the other triggers to be available as well. The time loop stops that from happening until it has finished.

I thought of using multi tasking, but although that works in theory doesn't allow for the frequency changes needed for the DFPlayer.
Everything has only been tested in simulation as I am still waiting on some parts to arrive, so I am not sure if there are any other problems possibly in the sound programming.

The latest copy of my code, the last section Timing is where I have the problem;
Code:
; *******************************
;    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
 

PieM

Senior Member
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
Is not the program I wrote in #22 !
You don't understand the use of time...


Main:
Do
If trig1_pulse = 1 then (or similar)
Gosub Police_Flash
endif

do something else ...
test other triggers ..........


if time >10 then
low PoliceFlash
endif
loop

;------------------------------------
Police_Flash:
Time = 0
high PoliceFlash
Return
[/code]

To test Time in simulation , add a line Pause 10 after Do
 
Last edited:

regpye

New Member
Yes you are right, I don't understand the use of time in this case.
Maybe I am thick or something, but I have tried many times to get it to work and I cannot.
Either the lights will not turn on or they won't turn off. There is something missing in my thinking.
Thanks for your patience.
Here is the last copy of my code using what I think you mean to do. It is not working properly.

Code:
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            ;First warning - sound 0003
Symbol In2 = PinC.1            ;Second warning - sound 0004
Symbol In1 = pinC.2            ;Final warning - sound 0005
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:
Do
pause 10
If In1 = 1 then
Gosub Police_Flash
endif
   
;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:
if time >10 then
low PoliceFlash
endif


SerTxd("PoliceFlash on, sound 0005", CR, LF)
SetFreq BAUD_FREQ
Pause 10
SerOut TX, BAUD, ( $7E, $FF, $06, $0F, $00, $01, $05, $EF )
  SerTxd(" ", CR, LF)
gosub Check_Busy
SetFreq MDEFAULT
loop
Return

Police_Flash:
Time = 0
high PoliceFlash
Return
 
Last edited:

The bear

Senior Member
Reg, Have you tried the "Time " on a real picaxe? Not your complete program. Just a simple code job as suggested.
Apologies if you have.
Good luck......
 

regpye

New Member
Have you tried the "Time " on a real picaxe?
No, I haven't Bear, but I will do and see what happens.
The full code I am using is a difficult one to work with, because the same trigger is being used for two jobs at the same time, one to start the DFPlayer which is no problem, and the second to drive a mosfet that needs to be on of about 3 minutes. I can make that work, but while it is working nothing else can work until it has finished. A lot can happen in a security system in 3 minutes, so I need the other triggers to be workable during this time.
I realise that the time should be able to allow that to work, but I am getting this conflict with the two actions of the trigger.
I appreciate all suggestions because I am trying hard to learn, I am not a seasoned coder.

I could use this for testing, but although it works in simulation and should in real life, it doesn't have the same elements of the full code that I need to use. Only has one trigger action.

Main:
Do
If pinC.2 = 1 then
Gosub Police_Flash
endif
if time >10 then
low b.1 SerTxd("LED is off ", CR, LF)
endif
SerTxd(" Time ",#time, CR, LF)
loop
;------------------------------------
Police_Flash:
Time = 0
high b.1 SerTxd("LED is on ", CR, LF)
Return
 

regpye

New Member
Have you tried the "Time " on a real picaxe?
Yes I have tried it out as per code above in 28
I added an extra LED on the output to give a more visual indicator, one on and the other off.
I also made it specific for 08M2 as that is what I had handy
It works perfectly as expected. In practice the LEDS toggle on and off after starting 10 seconds apart testing against a clock second hand movement with this code, so 3 minutes should be around 180.

; Target PICAXE: 08M2+
; *******************************

Main:
Do
pause 10
If pinC.3 = 1 then
Gosub Police_Flash
endif
if time >10 then
low C.2 high C.4 SerTxd("LED is off ", CR, LF)

endif
;SerTxd(" Time ",#time, CR, LF)
loop

;------------------------------------
Police_Flash:
Time = 0
high C.2 low C.4 SerTxd("LED is on ", CR, LF)
Return
 
Last edited:

regpye

New Member
You don't understand the use of time...
Main:
Do
If trig1_pulse = 1 then (or similar)
Gosub Police_Flash
endif

do something else ...
test other triggers ..........


if time >10 then ;this part of the code is never reached, so that is why it doesn't work, maybe the in-between code prevents it?
low PoliceFlash
endif
loop

;------------------------------------
Police_Flash:
Time = 0
high PoliceFlash
Return
 

regpye

New Member
Updated code, still not working correctly.

Code:
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'

;---------------------------------------------------------

Main:
do
pause 10
 If In1 = 1 then 
Gosub Police_Flash SerTxd("PoliceFlash on, sound 0005", CR, LF)
SetFreq BAUD_FREQ
 Pause 10
 SerOut TX, BAUD, ( $7E, $FF, $06, $0F, $00, $01, $05, $EF )
  SerTxd(" ", CR, LF)
  SerTxd("In1 triggered - Time ",#time, CR, LF)
gosub Check_Busy
 SetFreq MDEFAULT
endif   
;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
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

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
SerTxd(" Time ",#time, CR, LF); test the time status
return

;---------------------------------------------------------
Warning_Led:     ;B.5
; further code to be added here (maybe)
Return
;0000000000000000000000000000000000-

if time >10 then
low PoliceFlash
endif
loop

;------------------------------------
Police_Flash:
Time = 0
high PoliceFlash
Return
;0000000000000000000000000000000000
 

regpye

New Member
I fixed it myself.
I added a variable to check if the trigger had been pressed and if so, gosub to a new label instead f having just a "goto main" because the "goto main" was stopping the "time" section ever being reached.. After reaching the "time" section the variable is changed back to normal value.
Very interesting problem, I am surprised no one saw that.
 

papaof2

Senior Member
What you think you see is NOT the problem. You don't seem to understand how Gosub works.

Subs are ALWAYS after the "goto main" because they NOT a part of the sequential flow of the program. Subs are NEVER "reached" in the flow of a program. They are "called" with a Gosub and end with a Return to get back to the line after the call. That says you definitely have other problems in your code.

Is the line with that Gisub ever reached? Have you tested that in the simulator?

I'm waiting to take the next hydrocodone for my chronic back pain so now is not a good time for me to attempt that level of research.
 

regpye

New Member
What you think you see is NOT the problem. You don't seem to understand how Gosub works.
I am learning all the time and I am glad of any assistance here from the forum members. Still have a very lot t learn.
In this case the original code didn't have a "gosub" where it should have, and that is what the problem was.
Is the line with that Gisub ever reached? Have you tested that in the simulator?
Yes I woke up during the night and started the computer because I realized what could have been the problem. Reprogrammed and tested and it works fine. I placed several on screen indicators within the code to test where the program was going and what it was doing .
The test program for using "time" in #29, I not only tested in simulation, I built up a hardware board and tested for real using LED indicators. That worked perfectly, but it didn't have the additional triggers and two functions on one of the triggers which made things a little more complicated and confusing for me .

Sorry to hear about your back pain, I have just finished with that problem myself after a fall a few weeks ago.
 
Top