Counting Time Between Button Press
simpleboy
Posts: 5
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.
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
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.
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)?
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.
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
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.
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)
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.
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.
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