SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By dustin_writes
#111868
Programming question:
I need to measure the total number of seconds a button is pressed. More specifically, I need to count the number of seconds first press, second press, third press, etc, and add the total number of seconds that the switch has been activated.

Could anyone point me in the direction? Any resources you might recomend are greatly appriciated.

Thanks,
-dustin
By Cory
#112320
Have a look at this post: http://www.arduino.cc/cgi-bin/yabb2/YaB ... 1255126091

Basically, you can use millis() to keep track of time. You can then store them as variables, such as initial for the first press, then another for duration, etc. There are several example sketches and libraries on the Arduino website that make using buttons much simpler.

Here's a great library that offers multiple button modes such as one-shot and timer: http://www.arduino.cc/playground/Code/Buttons
By dustin_writes
#112998
Cory wrote:Have a look at this post: http://www.arduino.cc/cgi-bin/yabb2/YaB ... 1255126091

Basically, you can use millis() to keep track of time. You can then store them as variables, such as initial for the first press, then another for duration, etc. There are several example sketches and libraries on the Arduino website that make using buttons much simpler.

Here's a great library that offers multiple button modes such as one-shot and timer: http://www.arduino.cc/playground/Code/Buttons

Great! Thanks so much for the help. I've got a pretty good outline for the sketch already but the libraries will be a great help.
By Cory
#113031
No problem. When I first used butons, I originally did all the coding myself. Once I found out about the libraries out there, I realized its not only faster, but also much simpler and more efficient to use hardware abstraction libraries!