Best aproach to create a 'retriggerable' timer?

elanman99

Senior Member
I an not an accomplished programmer and am struggling to decide the best way to make a retriggerable timer with an 18M2 (because I have them already)

I only have one input that starts a 30 second timer and need to make one output pin high for that period. If there is no further input the outputs goes/stays low. If an input signal arrives whilst the timer is in progress it has to start the 30 seconds timer again so that the output remains high continually and only goes low 30 seconds after the last received input trigger.

Using an M2 I thought the 'Time' variable would be the simplest method but after searching the forum and studying the manuals I still cannot properly grasp how to use it. One possible complication is that the input signal (logic high) might vary in length from 100mS to 1.5 seconds.

None of the scores of example Picaxe timers seem relevant as what i am trying to make is just a monostable really.

Any pointers as to which way I should concentrate would be appreciated.

Ian
 

marks

Senior Member
Hi elanman99,
here's one approach to this you can try.
Code:
[color=Navy]#picaxe [/color][color=Black]18m2[/color]
[color=Navy]#terminal 19200[/color]
[color=Blue]SETFREQ M16          [/color][color=Green]' OR M4 time 1 second[/color]

[color=Purple]dirsB [/color][color=DarkCyan]= [/color][color=Navy]%00001111             [/color]
[color=Purple]dirsC [/color][color=DarkCyan]= [/color][color=Navy]%10111000
                        
    [/color][color=Blue]SYMBOL RUNTIME   [/color][color=DarkCyan]= [/color][color=Navy]30  [/color][color=Green]' secs [/color]
[color=Blue]SYMBOL [/color][color=Purple]RESETswitch   [/color][color=DarkCyan]= [/color][color=Purple]pinC.0[/color]
[color=Blue]SYMBOL [/color][color=Purple]LIGHT         [/color][color=DarkCyan]= [/color][color=Purple]outpinB.0[/color]

[color=Blue]SYMBOL [/color][color=Purple]countdownTIME   [/color][color=DarkCyan]= [/color][color=Purple]B1[/color]
[color=Blue]SYMBOL [/color][color=Purple]STARTmode       [/color][color=DarkCyan]= [/color][color=Purple]B2[/color]

[color=Black]Main:
   [/color][color=Blue]IF  [/color][color=Purple]countdownTIME [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]THEN [/color][color=Black]: [/color][color=Blue]DISABLETIME [/color][color=Black]: [/color][color=Purple]STARTmode [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Black]:[/color][color=Purple]LIGHT [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Black]: [/color][color=Blue]ENDIF

      IF [/color][color=Purple]RESETswitch [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]THEN [/color][color=Black]: [/color][color=Purple]TIME [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Black]: [/color][color=Blue]ENABLETIME [/color][color=Black]: [/color][color=Purple]STARTmode [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Black]: [/color][color=Blue]ENDIF
         
       [/color][color=Purple]countdownTIME [/color][color=DarkCyan]= [/color][color=Blue]RUNTIME [/color][color=DarkCyan]- [/color][color=Purple]TIME
       
        [/color][color=Blue]IF [/color][color=Purple]STARTmode [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]AND [/color][color=Purple]countdownTIME [/color][color=DarkCyan]<> [/color][color=Navy]0 [/color][color=Blue]THEN [/color][color=Black]: [/color][color=Purple]LIGHT [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Black]: [/color][color=Blue]ENDIF[/color]

[color=Black]ControlStatus:[/color]
[color=Blue]SERTXD([/color][color=Red]"  LIGHT="[/color][color=Black],#[/color][color=Purple]LIGHT[/color][color=Black],[/color][color=Red]"  countdownTIME  "[/color][color=Black],#[/color][color=Purple]countdownTIME[/color][color=Black], [/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf)


GOTO [/color][color=Black]Main:[/color]
 

elanman99

Senior Member
Many thanks to 'marks' who went above and beyond the call of duty in his reply by actually creating the complete code to perform the very function I needed!

I am most grateful as I would have been happy just to find out the general concept

His code make my attempt look like rubbish (it was!). I was going to post my code but when I saw marks code I hurriedly copied and pasted it overwriting my lines in the process.

Thanks

Ian
 
Top