Timing the interval between two events

Hi,
I was a bit stuck on something and wondered if anybody was willing to help me. I was looking around and could not find any clear answers on the web as to how I can time the interval between two events - namely the pressing of a button twice. Any direction to a tutorial or other help would be greatly appreciated.

Thanks

I was looking around and could not find any clear answers on the web as to how I can time the interval between two events - namely the pressing of a button twice.

This is trivial. Count the number of presses. Record the time when the nth press occurred in an array. Reset the counter whenever it exceeds the size of the array. The delta between the nth event and the n+1st event is what you need. The - operator might come in handy.

The time can be captured using millis() or micros(), depending on how fast you are.

Of course, you must be able to distinguish between when a switch becomes pressed and when it is pressed. You only want to record the time when the switch becomes pressed.

To know that, you need to keep track of the previous state of the switch, to determine when the current state is not the same as the previous state. Then, you need to look at the current state to determine if the switch was just pressed or just released.

You need code to monitor the state of the button (i.e. open/closed high/low) and detect when it changes.

When a button press is detected, use millis() to read the current time.

Subtract the value of millis() last time it was pressed to get the elapsed time in milliseconds since the last press.

You also need to distinguish between bouncing and actual button presses, which complicates matters a little if you need to record very rapid sequential button presses. Ultimately you will have to compromise on debouncing time and repeating time...