I need to set a timer that can get interrupted if a button gets pressed.

I am working on lights for my stairs that will light up when you get to the top or the bottom. I’m just testing around because i’m a newbie. My question is is there a way to have a timer that can get interrupted and restart the timer if a button gets pressed. so if someone goes down the steps and the timer is about to end and someone gets to the stairs and they have a short time to get down the stairs.

 // all of the led pins.
 const int one = 4;
 const int two = 5;
 const int three = 6;
 const int four = 7;
 const int five = 8;
 const int six = 9;
 const int seven = 10;
 const int eight = 11;
 const int nine = 12;
 const int ten = 13;
 
  // The button pin.
 const int button = 3;
 int state = 0;
 const int button2 = 1;
 int state2 = 0;


void setup() {
  // Initilizing in/out for each pin.
pinMode(one, OUTPUT);
pinMode(two, OUTPUT);
pinMode(three, OUTPUT);
pinMode(four, OUTPUT);
pinMode(five, OUTPUT);
pinMode(six, OUTPUT);
pinMode(seven, OUTPUT);
pinMode(eight, OUTPUT);
pinMode(nine, OUTPUT);
pinMode(ten, OUTPUT);
pinMode(button, INPUT);
pinMode(button2, INPUT);
}

void loop() {
  
  //sets the state value to either low or high based on if the button gets pressed
  state = digitalRead(button);
  state2 = digitalRead(button2);
  if(state == HIGH){
       //sequentially turns on led's
    digitalWrite(one, HIGH);
    delay(150);
    digitalWrite(two, HIGH);
    delay(150);
    digitalWrite(three, HIGH);
    delay(150);
    digitalWrite(four, HIGH);
    delay(150);
    digitalWrite(five, HIGH);
    delay(150);
    digitalWrite(six, HIGH);
    delay(150);
    digitalWrite(seven, HIGH);
    delay(150);
    digitalWrite(eight, HIGH);
    delay(150);
    digitalWrite(nine, HIGH);
    delay(150);
    digitalWrite(ten, HIGH);
   
    
    
    //sequentially turnds off the lights after a 1 min timer
    digitalWrite(one, LOW);
    delay(150);
    digitalWrite(two, LOW);
    delay(150);
    digitalWrite(three, LOW);
    delay(150);
    digitalWrite(four, LOW);
    delay(150);
    digitalWrite(five, LOW);
    delay(150);
    digitalWrite(six, LOW);
    delay(150);
    digitalWrite(seven, LOW);
    delay(150);
    digitalWrite(eight, LOW);
    delay(150);
    digitalWrite(nine, LOW);
    delay(150);
    digitalWrite(ten, LOW);
    
   

    //if your at the bottom of the stairs first (reversed)
    }
    if(state2 == HIGH){
    digitalWrite(ten, HIGH);
    delay(150);
    digitalWrite(nine, HIGH);
    delay(150);
    digitalWrite(eight, HIGH);
    delay(150);
    digitalWrite(seven, HIGH);
    delay(150);
    digitalWrite(six, HIGH);
    delay(150);
    digitalWrite(five, HIGH);
    delay(150);
    digitalWrite(four, HIGH);
    delay(150);
    digitalWrite(three, HIGH);
    delay(150);
    digitalWrite(two, HIGH);
    delay(150);
    digitalWrite(one, HIGH);
    delay(6000);
    
    //turnd the led's back off (reversed)
    digitalWrite(ten, LOW);
    delay(150);
    digitalWrite(nine, LOW);
    delay(150);
    digitalWrite(eight, LOW);
    delay(150);
    digitalWrite(seven, LOW);
    delay(150);
    digitalWrite(six, LOW);
    delay(150);
    digitalWrite(five, LOW);
    delay(150);
    digitalWrite(four, LOW);
    delay(150);
    digitalWrite(three, LOW);
    delay(150);
    digitalWrite(two, LOW);
    delay(150);
    digitalWrite(one, LOW);
  }
   
  }

sorry for bad examples. Also my code is very bad i’m a newbie.

You probably have to first get rid of those delay() statements, and switch to millis() based timing. That would at least make it respond to buttons. Next, turn the thing into a finite state machine, which in turn makes restarting a sequence trivial. Note: you can find many more tutorials on these two subjects if you need.

thanks so much. :slight_smile: