Timing key presses in Miliseconds
duffling
Posts: 73
Im looking for an elegant way to time Milliseconds between two key presses in a loop
for example :
the user presses a button , waits ,, presses the same button again .. and continues doing this
while my program calculates the time between presses and calculates this as BPM , beats per minute
then i would like to flash an LED at the given tempo
what would be the best way to attack this with the sx ? , i did it with the basic stamp
using a loop:
basically i would calculate the difference between the first click , and the secondclick , and adjust timing with a small pause
but im sure with the RTCC i can do this in a more elegant way ?
im looking for a starting point so i can start to program in that direction..
·
for example :
the user presses a button , waits ,, presses the same button again .. and continues doing this
while my program calculates the time between presses and calculates this as BPM , beats per minute
then i would like to flash an LED at the given tempo
what would be the best way to attack this with the sx ? , i did it with the basic stamp
using a loop:
basically i would calculate the difference between the first click , and the secondclick , and adjust timing with a small pause
but im sure with the RTCC i can do this in a more elegant way ?
im looking for a starting point so i can start to program in that direction..
·
Comments
Basically, this can be done with an interupt routine.
Posted below is a link to a contest entry that uses the ISR to measure the time for a pin to go high twice.....a stopwatch.
The program measures in the number of microseconds between events. You would modify the program to measure milliseconds, so running at 50 MHz is probably way to fast. Running at 4MHz the ISR would increment a counter every 4000 clock cycles, which would be 1 mS (if my calcs are correct ?)
http://www.parallax.com/sx/contest/contest_door_velocity.asp
Follow the link to the source code.
I also recommend this book....http://www.parallax.com/detail.asp?product_id=70002
which is where I learned how to use the ISR.
Ken
Post Edited (KenM) : 3/29/2005 3:08:35 PM GMT
I was intregued by your need, and thought it a very good example to demonstrate a simple task scheduler (RTOS) as well as a state machine program. These elements make code writing for sequential actions very simple as they can be quite effectively separated from each other and hence minimize their interaction impact.
The attached code nearly does what you want; but it toggles the LED for the duration of the period instead of flashing it briefly.
I'm sure you can modify the code to be exactly what you want. I have written it for you incorporating my very simple minded non-preemtive RTOS. What someone in a much earlier post called a "cooperative" RTOS, because each task relinquishes the processor when it has done a piece of its work, and needs to wait for some time to pass before carrying on. In the meantime other tasks can use the processor.
The two tasks I have are both set at the 1 millisecond priority level. The button task is implemented as a state machine, and the tempo task is just a continuously reloading LED toggling timer.
Have fun experimenting;
Peter (pjv)
many thanks for the code !
and i get some literal truncation warnings when i compile..
im trying to work out how u have done this [noparse]:)[/noparse] still learning!
thanks again
I'm sure you'll get the hang of what it's doing by single stepping through it.
I find the co-operative state machine example very useful for software UARTing, where, after detecting the start bit, every time into the data bit receiving state, a sample is taken, shifted into a receive register, and then exiting, releasing the processor to other tasks. On receiving the 8th (or ninth or tenth) data bit, the next state is directed to be the stop bit state, and when in that state, the received byte is processed. It all becomes so simple, even at 1 Megabit per second.
In following this forum, I see too many folks writing code where they actually WAIT IN A LOOP, when they should be relinquishing the processor to take care of other things. The simple minded RTOS, although far from perfect, helps immensely in this regard. Perhaps that is the legacy of the single thread approach of the Stamps.
So have fun, and enjoy!
Peter (pjv)