Timer design that 'resets' each time an i/p goes low...

Puma560

New Member
Hi all. I hope someone can help with the code for me.

I'm trying to make a timer that does the following (it's to activate a lamp when a PIR sensor detects movement):


It monitors the state of an input. When the input momentarily goes LOW, a timer starts for 20 seconds sending an output HIGH (to drive a relay).

If the same input momentarily goes LOW again during the 20 seconds, the timer effectively starts again, leaving the output activated for a further 20 seconds.


The problem I am having seems to be monitoring the state of the i/p during the 20 second period. I'm currently using the 'pause' command which effectively halts the program for 20 seconds before moving on, ignoring any change to the i/p.

Ideally I'd like to use the 08M or 08M2. Thanks in advance
 

hippy

Technical Support
Staff member
Welcome to the PICAXE forum.

The trick is to pause for shorter periods and loop until 20 seconds (20000ms) have elapsed. In that loop you can reset the count of elapsed time. The simplest would be something like ...

Code:
Do
  Do:Loop Until pinC.3 = 0
  High B.7
  w0 = 0
  Do
    Pause 1
    w0 = w0 + 1
    If pinC.3 = 0 Then
      w0 = 0
    End If
  Loop Until w0 >= 20000
  Low B.7
Loop
You will probably have to reduce the 20000 to make it a more accurate 20 seconds.

That can be optimised to ...

Code:
Do
  Do:Loop Until pinC.3 = 0
  High B.7
  w0 = 0
  Do
    Pause 1
    w0 = w0 + 1 * pinC.3
  Loop Until w0 >= 20000
  Low B.7
Loop
There is perhaps a more optimal solution which relies on resetting the index variable of a FOR-NEXT loop -

Code:
Do
  Do:Loop Until pinC.3 = 0
  High B.7
  For w0 = 0 To 20000
    Pause 1
    w0 = w0 * pinC.3
  Next
  Low B.7
Loop
 
Top