Button states and timer?


I'm fairly new to Arduino, I have a question on how can you instruct the Arduino that if a push button isn't pressed for an amount of time it'll reset a process or output. Here's a simple code, I'm counting up to 10 and it should light up another led when it reaches 10, but if it doesn't reach 10 for an amount of time, or no button press is made in that amount of time it'll go back to it's original state.

int startbtn = 5;

int startled = 13;
int stopled = 12;

int defaultState = 0;

int count = 0;

void setup() {
  pinMode(5, INPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);

void loop() {
  digitalWrite(stopled, HIGH);

  int buttonState = digitalRead(2);

  if (buttonState != defaultState) {
    if (buttonState == HIGH) {

      if (count == 10) {
        digitalWrite(startled, HIGH);
        digitalWrite(stopled, LOW);

Now, I want to revert back to the original state if the counter doesn't reach 10 for a specific time and reset the process to it's initial state like stopled will be HIGH again and counter will return to zero again.

Any help is accepted, thank you

in principle, you create a timer eg:

unsigned long myTimer1 ;

then at some point you set it:

myTimer1= millis() ;

and later you test it:

if ( millis() - myTimer1 > 10000 ) {

  // timer expired after 10000 mS (10 seconds) so do something


To do something when 'something has not happened for a specific period of time' you:

A: Store the time whenever the thing happens. B: Compare the current time minus the stored time against the specified period of time.

If the time since the last time the thing happened is greater than the specified periodof time then that 'something has not happened for a specific period of time'.