Shop OBEX P1 Docs P2 Docs Learn Events
Rolling timer — Parallax Forums

Rolling timer

FranklinFranklin Posts: 4,747
edited 2006-06-02 18:05 in BASIC Stamp
I'm looking for a way to initiate a response when I get three inputs (on same pin) within five seconds. The problem is the timing has to roll with the inputs such that if the span from input 1 to 3 is six seconds the inputs 2 and 3 are still in play if a fourth input would make three in five seconds. Any thoughts or code examples?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2006-06-02 15:48
    I think you either need a pseudocode description of what you're trying to do, or a flow-chart.
  • cicacica Posts: 11
    edited 2006-06-02 16:17
    This is quick and dirty, and hasn't been debugged yet, but I think it does what you want.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    
    
    Btn             PIN     0
    btnWrk          VAR     Byte
    Time1             VAR   Word
    Time2             VAR   Word
    Time3             VAR   Word
    Timer             VAR   Word
    duration_needed   VAR   Word
    
    timer=0
    duration_needed = 5000 'Play with this number to get 5 seconds
    
    Main:
      ' The BUTTON instruction will cause the program to branch to
      ' No_Press unless P0 = 0
    
    PAUSE 5
    BUTTON Btn, 0, 200, 20, btnWrk, 0, No_Press
    time3=timer
    IF time3 - time1 < duration_needed THEN GOTO SUCCESS
    ELSE
    time1=time2-timer
    time2=time3-timer
    timer=time2
    ENDIF
    GOTO Main
    
    
    
    No_Press:
      timer = timer + 1
      GOTO Main
    
    
    Success:
     DEBUG "Success"
    END
    



    -Tom

    Post Edited (cica) : 6/2/2006 4:53:03 PM GMT
  • FranklinFranklin Posts: 4,747
    edited 2006-06-02 16:56
    check for input
    if input start a timer and increment counter
    if counter > 2 and timer < 5 GOTO next thing
    (this part I don't know
    if timer > 5 delete first count and set timer to second event and continue)
    

    So if the events happen as, event1...4 seconds later event2...2 seconds event3...2 seconds event4
    Time between event1 and event3 is more than 5 seconds so no output but time between event2 and event4 is 4 seconds so OUTPUT!
    Hope this makes it more clear. I would have included a flowchart but I don't have software for that.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • FranklinFranklin Posts: 4,747
    edited 2006-06-02 16:58
    Thanks cica, I'll look at that closer when I can.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-06-02 17:37
    How this is done wll depend on other aspects of your project.
    1) How accurate does it have to be?
    2) Do you have an external real time clock or timer that you want to use as the reference, or do you want to do it all in PBASIC?
    3) Does the program have to be doing other things while waiting for the three clicks, or can it be modal, just sitting there waiting for the button as its only task?

    PB   VAR in1   ' pushbutton state, debounce with RC
    time1=254   ' initialize times on long side, bytes
    time2=254
    time3=254
    DO
     DO : timer=timer+1 MAX 254 : PAUSE 100 : LOOP WHILE PB   ' count 0.1 seconds PB=1
      DO : timer=timer+1 MAX 254 : PAUSE 100 : LOOP UNTIL PB  ' count 0.1 seconds PB=0
       ' PB has been released 0-->1, this snippet counts timer from release to release
      time1 = time2
      time2= time3
      time3 = timer
      IF time1+time2+time3 > 60 THEN GOSUB stuff2do  ' 6.0 seconds
     LOOP
    



    That is a modal snipppet devoted to monitoring the button with a granualarity of 0.1 second. You can see the problem if the stuff2do takes a long or indeterminate amount of time. While it is off doing the stuff, it is not timing of the button. If the stuff takes a definite amount of time, you can adjust the timer or the PAUSE to compensate. Similarly, if other stuff has to be done by the program while waiting for the buttons, it can be stuffed in as part of the PAUSE in each timing loop, and the PAUSE adjusted. It all depends on precision required and the other stuff. The situation and programming is diffferent if you have an external reference clock source.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • FranklinFranklin Posts: 4,747
    edited 2006-06-02 17:49
    At this point... No it does not need to be very accurate, it's to reduce false triggering of a PIR motion detector. RTC? If there is one on the PDB I have it but I don't think I need the accuracy. It would be nice to do other things until it gets the first pulse and after the routine times out. Thanks for the input.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-06-02 18:03
    Franklin -

    Just for the record, many PIR detectors have built-in false alarm detection. Even beyond that, some are dual-sensing, in that they sense BOTH I/R _and_ movement, and won't fire off unless BOTH are present.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • cicacica Posts: 11
    edited 2006-06-02 18:05
    Franklin-

    I used to install alarm systems for a living. There are PIR detectors that require 3 events in order to trigger. I'd lose the stamp and buy one of these $20 detectors.

    -Tom
Sign In or Register to comment.