Arduino timer

Hi, I'm trying to achieve the following functionality.

When button pressed,
Timer starts, counting from 0 to 1,2,3, N...
Button Low
Timer stops
assign number of seconds to a variable
reset timer to 0

are there any libraries I can use for the timer. I couldn't find any to meet these requirements, I would like to know if anyone else had to do something similar.

Do you mean that the count should increment every second ?

The simplest way is to use millis() - see the demo several things at a time.

Just increment the count something like this

if (millis() - prevMillis >= 1000) {
  count ++;
  prevMillis += 1000;

Usually it is best to use pinMode(buttonPin, INPUT_PULLUP); which means that the pin reads HIGH when the button is not pressed.

Try creating your program and if you can't get it to work post your code and we will try to help.


Look at the state change detection example, too. You have things to do when the switch BECOMES pressed (not IS pressed) and things to do when the switch BECOMES released (not IS released).