Shop OBEX P1 Docs P2 Docs Learn Events
Counting Time Between Button Press — Parallax Forums

Counting Time Between Button Press

simpleboysimpleboy Posts: 5
edited 2013-01-05 20:34 in General Discussion
Hello,


I was hoping someone here could help with a question I had regarding the basic stamp 2.
I'm new to programing so forgive me if this very basic and obvious.




I would like to write program that makes an led flash whenever a button is pressed exactly 500 milliseconds apart, is this possible?
or even a few milliseconds either side of 500 ie. 490-510.

I read something about the Basic stamp not having a RTC or something, would this be a problem for my program if all the program was what I mentioned above?




Thank you for your help.

Comments

  • Mike GMike G Posts: 2,702
    edited 2013-01-03 18:06
    Look up the Pause command in the Basic Stamp manual. The Pause command waits for x milliseconds.
  • simpleboysimpleboy Posts: 5
    edited 2013-01-03 18:22
    Thank you Mike G,

    I have looked at the PAUSE command, and see it works in milliseconds, but I don't understand how I can use it to get the time between 2 button presses.
    Sorry, I thought I had to use the COUNT command somehow, but I'm new and so I'm glad for any help, and nudge in the right direction.
  • ercoerco Posts: 20,259
    edited 2013-01-03 18:46
    Pause is not what you want. That ignores everything that goes on during the Pause. You want to loop through a time-calibrated counting loop, constantly looking for button presses at the right time. But first, you need to consider how, if and when the button will be released from the last button press (switch closure). Switches have bounce, too. What type of button/switch?

    Do you want the half-second interval to be from initial switch closure to the next switch closure, no matter how long the button was held down (under a half-second, obviously)?
  • simpleboysimpleboy Posts: 5
    edited 2013-01-03 18:54
    Hi erco,

    I want to use a tactile switch, and the count to start as soon as it's clicked...Im guessing thats the initial closure of the circuit, then to count until the second click, again upon closure.
    Is this complicated to program?

    I have heard the bouncing issue with switches, and know it can be fixed by adding a capictor, or writing in some extra code??

    Thank you.
  • Duane C. JohnsonDuane C. Johnson Posts: 955
    edited 2013-01-03 19:19
    How much time do you expect between button pushes?

    If this interval is less than about 54 seconds you could simply read a counter on each button push, then:
    ((Count Value 2) - (Count Value 1)) / 80_000_000MHz = Seconds
    This is quite accurate.

    Duane J
  • simpleboysimpleboy Posts: 5
    edited 2013-01-03 19:28
    Hi Duane C. Johnson,

    I really appreciate yours and everybody else's help, however :blank: I just don't understand how to implement your advice.
    between button pushes I expect to be a maximum of 3 seconds. Is it possible to get some example in code?

    I understand If this is to much trouble.

    Thank you.
  • ercoerco Posts: 20,259
    edited 2013-01-03 19:49
    Nothing's too hard, simpleboy.

    So you want an LED to light briefly only if a single pushbutton is pressed repeatedly around 500 milliseconds, right? Your 3-second timeout is immaterial...

    Yes, a capacitor can help, and yes, code can somewhat work around bounces. How good are you at programming the BS2? Have you worked thru "the book" yet? (WAM)
  • simpleboysimpleboy Posts: 5
    edited 2013-01-03 19:56
    erco,

    Thank you, you're right nothing is to hard. I have not worked through the "What's a Micro Controller" Book that's probably a good reason I'm not good at programming.

    Thanks for all your help, I'll read through the book.
  • ercoerco Posts: 20,259
    edited 2013-01-03 20:57
    XLNT, simpleboy. It's really not hard at all, but if we just hand you the answer you won't learn anything. Please come back often with questions and let us know how you're progressing.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-01-03 22:20
    A snippet like the following on the BS2 can measure the time a pin is high, and then the time it is low, with a resolution of about 1 millisecond.
    [FONT=courier new][SIZE=1]DO
      x=0
       y=0
      DO
         x = x+1
      LOOP WHILE in0    ' loop & count while in0 is high
      DO
         y = y+1
      LOOP UNTIL in0    ' loop & count while in0 is low
      DEBUG DEC x, 32, DEC y, CR  ' show total counts
    LOOP[/SIZE][/FONT]
    
    The rate of counting has to be calibrated for each loop, because the loops will not run at exactly 1000 per second. Nevertheless, the result will be consistent, well within your +/- 10ms window, because it depends on the resonator that drives the BS2.

    Other BASIC Stamp commands that might help with your project are RCTIME and PULSIN.
  • skylightskylight Posts: 1,915
    edited 2013-01-05 06:38
    Wouldn't PAUSE work?
    DO
    Loop while in0 ' wait for initialising button push

    DO
    Pause 500
    If in1
    goto LED
    else
    Low(Pin)
    LOOP

    LED:
    High(Pin)


    you'd have to press again exactly after the 500 ms pause or the program would loop
    no allowance for switch contact bounce or holding button on of course.
    above is a one shot but if you wanted it to continously monitor for the two pushes then you could expand on it
    Pause time may have to be altered to allow for program execution time.
    you'd hit the reset button to reset the circuit or you could add code to allow another button to reset out of the LOW(Pin) loop
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2013-01-05 20:34
    One thing to keep in mind is that when you use PAUSE command then nothing else will happen until the PAUSE command is done
Sign In or Register to comment.