Refreshing vaues after time

ZOR

Senior Member
I have code that is monitoring the output of a PIR sensor.

The code counts the number of triggers before sounding an alarm bell.
My problem is knowing how to add code to sensibly include a time period where the incrementing B4 gets reset, (i.e it could increment over days to reach it's limit) maybe resetting every 10 minutes of no activity.

Any help/advice appreciated

Code:
B4=0
SB:

' Monitor for PIR trigger (PIR1=1)

If PIR1=0 then ' PIR triggered
B4=B4+1
end if
IF B4=3 Then goto SOUNDBELL
pause 2000
goto sb

SOUNDBELL:
' make noise etc
END
 

hippy

Technical Support
Staff member
To do it properly you need to keep track of when each activation occurs so you can drop those activations which occurred some time ago while keeping those still within the period currently being measured.

A good compromise can be had by looking at it from a different perspective; count all activations and when that hits hit N you trigger the alarm, when there has been no activity for time T you reset the counter to zero. You rest the timer on every activation. Something like -

Code:
Do
  If PirActivated Then
    timeout = 0
    count = count + 1
  Else
    if TimerTicked Then
      If timeout > T Then
        count = 0
      Else
        timeout = timeout + 1
      End If
  End If
Loop Until count > N
Gosub ActivateAlarm
Note that "If PirActivated" cannot simply be "If pinX.Y=0" or count will quickly escalate each time round the loop so you have to add additional code to detect each new activation. Likewise something for determining "If TimerTicked".
 

ZOR

Senior Member
I looked in Manual 2 for the word Time or Timer, and could not find a reference to it. However I tried this and it looked as though I would use this in my code. I don't know how far it would put a value in B2, but I could trap it at a certain value and restart it?

Is it safe, will it do what I want?

Code:
DD:
DEBUG
B2=TIME
GOTO DD
 

ZOR

Senior Member
Many thanks, I can have a play with that too. Hidden under variables I would not have found that. Regards
 

rossko57

Senior Member
Only just now! You're not the only one to struggle finding docs on 'time', this is its nature as a "special variable" and not a command or function that folks set out looking for.
 

ZOR

Senior Member
Thanks rossko57, yes, don't know why it's not included in the functions lists ie do, loop etc
Also the question on the linked page above seemed to have no answer?
 

ZOR

Senior Member
Thanks hippy, yes I can see the information on the two links, it was just that I referred to manual 2 but did not find it.

Thanks rossko57, that comment of yours sorted that question out perfectly,
 

ZOR

Senior Member
I am trying to find a way around a problem I have with this code. Maybe I should go down the Pause mode?

Code:
SYMBOL PIR1 = PINC.4

enabletime	

do
w0=time
If PIR1=0 then ' PIR triggered
b4=b4+1 ' increment 
' Come out here to sound alarm if B4=x
do loop while PIR1=0 ' let PIR reset to avoid b4 incrementing wrongly
End If
loop while w0<3 ' Use numeric number to set overall time period of code block
My problem is the total event time period varies when the PIR gets triggered due to the time/loop to allow the PIR to reset itself. If I don't allow the PIR to settle back then b4 increases in vast amount.

Can someone see how to amend this code to keep the overall time consistant regardless of how many PIR events occur within the loop. Thanks
 
Last edited:

rossko57

Senior Member
What overall time are you having trouble with? Real-time (and the Picaxe 'time' variable') will march on while your PIR does whatever it does.
I thought you were looking for a trigger-free period - wouldn't you want the period timing to start at the end of the last trigger?
 

JimPerry

Senior Member
I think you are looking at this in the wrong way - if a commercial PIR is installed correctly then a PIR event means an alarm should be triggered. You can set commercial control panels for "double knock" which activates full alarm if two triggers within 5 seconds.

I've got a few commercial control panels and PIR's + keypads etc in my attic PM me if you want anything... :rolleyes:

PS: They will ONLY work with a small SLA though! :(
 

hippy

Technical Support
Staff member
You need to add a flag so you can tell what previous state the PIR had ...
Code:
If PIR1 = 0 Then
  If lastPir = 1 Then
    b4 = b4 + 1
  End If
  lastPir = 0
Else
  lastPir = 1
End If
Which you could optimise to ...
Code:
If PIR1 = 0 Then
  b4 = b4 + lastPir
  lastPir = 0
Else
  lastPir = 1
End If
Or even ...
Code:
b4 = PIR1 ^ 1 & lastPir + b4
lastPir = PIR1
Or ...
Code:
b4 = lastPir &/ PIR1 + b4
lastPir = PIR1
There's probably a one-liner but I haven't figured out what it would be yet.
 
Last edited:

hippy

Technical Support
Staff member
Tada ...

Code:
Symbol PIR1 = pinC.0
Do
  b4 = b4 & 1 &/ PIR1 * 2 + b4 & $FE | PIR1 Max 255
Loop
Note that b4 increments twice, on the start and end activation, so you would need to adjust the number you compared b4 against.
 

ZOR

Senior Member
Thanks everyone.
rossko57 : The code basically follows a first time trigger. I have a do loop that continually monitors for a PIR trigger event. If an event takes place then it passes into a block of code shown, where for maybe a 2 minute period looks for further triggers. B4 counts them and I will decide what I put in the code to say thats it, enough is enough sound the bells. Otherwise if it goes through the process seeing no more triggers then it goes back to the initial do loop waiting for any trigger. I have not put a break out line in my code shown to go to the bell sounding.

hippy : many thanks, I tried your last 1 liner, but could not see how the monitoring was over a time.

Jim : Many thanks for the offer, but this alarm is just a basic setup just to give some cover in a relatives house when it's not inhabited in the future. I am building a front end that monitors PIR sensor. I will use 433mhz to fire the other half buried away to make noise.

So basically I am wanting my code to to carry on producing a time value while a PIR is getting triggered. If I physically time the start to end of code, the time is shorter than when being interrupted by PIR triggering I thought because of my line of code where I delay adding to B4 until the PIR returns to idle. Does it makes sense. Maybe I should disable and then re-enable time?
 

rossko57

Senior Member
I still don't understand the concern really. Who cares how long your code takes to run round its loop, you are using the freestanding 'time' variable as reference?
 

hippy

Technical Support
Staff member
hippy : many thanks, I tried your last 1 liner, but could not see how the monitoring was over a time.
There was no time element in that, it simply handles activation and non-activation in the same amount of time.

Modifying my earlier code, this will probably give you something which works. If there are ever five PIR activations with less than 12 seconds between each PIR activation the alarm sounds. That's the equivalent of the alarm sounding with five activations within a minute ...

Code:
Symbol PIR1         = pinC.0

Symbol pirLast      = b0
Symbol pirActivated = b1
Symbol counter      = b2

Do
  pirActivated = pirLast ^ PIR1 &/ PIR1
  pirLast = PIR1
  If pirActivated = 1 Then
    time = 0
    counter = counter + 1
  Else
    if time >= 12 Then
      counter = 0
    End If
  End If
Loop Until counter >= 5

SerTxd( "Alarm triggered" )
 
Last edited:

ZOR

Senior Member
Thanks hippy. Does your last code work as standalone as I tried it in real mode as well as simulation after changing C.0 to C.3 but I cannot get the line (counter = counter + 1) to work unless several PIR triggers take place. I tried Debug but the code mainly just loops through with time incrementing. I must be doing something wrong.

Thanks rossko57, yes maybe I should ignore my process takes a bit longer when PIR activations take place as it's the triggering information I am looking for.
 

hippy

Technical Support
Staff member
The code works for me when simulated on a 20M2 using pinC.0, pinC.3 or pinC.4

A breakpoint on the "counter=counter+1" line shows it is activating every time the pin is taken from high to low, and counter is incrementing.
 

ZOR

Senior Member
Thanks, I will play with it later again as must be something wrong at my end then Regards
 

ZOR

Senior Member
Many thanks hippy, I was looking for counter to increment when the PIR was triggered but now looking again I see the counter gets incremented when the PIR resets. So now I understand how it's working, so that's a good way to do it. Thanks again for your time very appreciated. Works a treat.
 
Last edited:

ZOR

Senior Member
This code works really well, however I am trying to integrate a part that would sense if the PIR sensor was covered over or disconnected. Because the code relies on the PIR going low/high it would not sense C.2 going low permanently (PIR has N/C contacts into C.2, connecting to 5v, holding C.2 high when not activated)

Any ideas

Code:
 Do
       
          pirActivated = pirLast ^ PIR1 &/ PIR1
          pirLast = PIR1
          
          If pirActivated = 1 Then
            time = 0
            counter = counter + 1
                            
          Else
          
            if time >= 60 then '60 = 1 Minute
              counter = 0
              time = 0
               
              
          End If:End If
         
        Loop Until counter >= 2
 

hippy

Technical Support
Staff member
This code works really well, however I am trying to integrate a part that would sense if the PIR sensor was covered over or disconnected. Because the code relies on the PIR going low/high it would not sense C.2 going low permanently
Except your PIR never goes permanently low and if it were disconnected it would most likely appear to be in its inactive state. It seems you are trying to create a design which deals with situations which will never occur and, as you are finding, is quite complicated to do that.

I doubt simply clearing the 'time' variable will achieve what you want without breaking some other part of the code in the process so, if you are going to do it, it needs to be done some other way. You need to sit down and write on paper a full spec of what the conditions are and determine when counters are reset, incremented and and when counts cause activations. You need to determine how it will work before even thinking about how to implement it.

Firstly though you need to determine exactly how the PIR behaves, because if you design it for 'relay closed while movement' and it is 'pulse while movement', you program won't likely work as desired and all that design effort will have been wasted. And of course vice-versa.

If you want to detect PIR removal then you need to do that outside of activations. There is a reason PIR sensors have two circuits rather than just one.
 

ZOR

Senior Member
Thanks hippy, you are correct in what you say. There is a tamper switch inside the PIR, I can therefore sense if that gets broken by anyone opening the PIR or cutting wires. As you say these events should not really happen as the PIR would have done it's job.

Best regards

EDIT: Thanks Jim, just saw your message as I posted this one. Yes I will use the tamper switch. Thanks again, the PIR you suggested is excellent, good to turn off it's flashing indicator. Regards
 
Top