Traffic Light - Button

I made a simple circuit with my Arduino Uno board. I added 3 LEDs and a button. When the button is pressed i want the sequence to stop. I wrote the following code and my question is: Is there any way that i can stop the sequence no matter which LED the sequence is? Do i have to check the buttonstate with If statement after each LED?
Thanks in advance :smiley:

const int redled = 9;
const int yellowled = 8;
const int greenled = 7;
const int button = 2;
int buttonstate = 0;

void setup() {
  // put your setup code here, to run once:
    pinMode(redled, OUTPUT);
    pinMode(yellowled, OUTPUT);
    pinMode(greenled, OUTPUT);
    pinMode(button, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  buttonstate = digitalRead(button);
  while(buttonstate != HIGH){
    red_led();
    delay(4000);
    yellow_led();
    delay(2000);
    green_led();
    delay(4000);
    buttonstate = digitalRead(button);
   }
   if(buttonstate == HIGH){
     digitalWrite(redled, LOW);
     digitalWrite(yellowled, LOW);
     digitalWrite(greenled, LOW);
  }

}

void red_led(){
  digitalWrite(redled, HIGH);
  digitalWrite(yellowled, LOW);
  digitalWrite(greenled, LOW);
}

void yellow_led(){
  digitalWrite(redled, LOW);
  digitalWrite(yellowled, HIGH);
  digitalWrite(greenled, LOW);
}

void green_led(){
  digitalWrite(redled, LOW);
  digitalWrite(yellowled, LOW);
  digitalWrite(greenled, HIGH);
}

Do i have to check the buttonstate with If statement after each LED?

Yes. As well as reading the state after each delay().

Of course, your switch will be very unresponsive due to the delays.

Have a look at the BlinkWithoutDelay that comes with the IDE and at Demonstration code for several things at the same time

You might get the idea how to approach your problem.