Not able to take input from switch while another is active

Hi,

Total newbie here. I'm trying to expand on one of the projects that came with the Arduino starter kit but I've ran into a bit of an error.

I'm trying to basically flash two lights when the button is pressed and stop the flashing when another button is pressed. To make the lights flash, you don't have to hold down the button because I'm using a while loop.

However, when I try to check if the other button was pressed, and if so break, at the end of my while loop, nothing seems to occur. I can guarantee that my circuit is correct because if I try doing something else with that button, it works fine. Hopefully my code will make more sense and someone can help me find the error. Why is it not stopping the flashing if the other button was pressed? Attached is my code.

Thank you!

void loop() {
  goodSwitchCounter = digitalRead(3);
  badSwitchCounter = digitalRead(7);
  if (badSwitchCounter == HIGH){
    while (goodSwitchCounter != HIGH){
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, HIGH);
      delay(250);
      digitalWrite(6, LOW);
      digitalWrite(5, HIGH);
      delay(250);
    }
  }
 else{
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
 }
}

Read blink without delay example in the tutorials section.
Blink without delay

Your supplied code is incomplete (missing setup, variable definitions, etc) and so doesn’t compile.

Edit: If you never re-read the state of goodSwitchCounter inside the while loop, it will still have the state of the button when you initially sampled it. Re sample the pin inside the while loop.

If you want a responsive program don't use WHILE or FOR loops as they block the Arduino until the complete. Just use IF and allow loop() to do the iteration.

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R