How to exit from a while loop?

Hi guys,

I am relatively new to arduino since it has been like 5 days since i started using it. I am trying to build a very simple project on Knight rider. I have 2 buttons. One of the buttons will be used to switch on the led's (doing the chase effect) while the other will be used to switch the led's off. I have managed to do this with only one led (on and off) using the 'if' statement. When using the 'if' statement in the Knight rider the chase effect lasted only for one cycle instead of continuous looping, which is why i used the 'while' statement. The code I used is shown below;

const int buttonPin1 = 28;
const int buttonPin2 = 22;

int buttonState1 = 0;
int buttonState2 = 0;
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;
int pin7 = 7;
int timer = 60;

void setup(){ 
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
  pinMode(pin6, OUTPUT);
  pinMode(pin7, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
}

void loop() {  
  
   buttonState1 = digitalRead(buttonPin1);
   buttonState2 = digitalRead(buttonPin2);
  
   while (buttonState1== HIGH) { 
    
   digitalWrite(pin2, HIGH);
   delay(timer);
   digitalWrite(pin2, LOW);
   delay(timer);
   
   digitalWrite(pin3, HIGH);
   delay(timer);
   digitalWrite(pin3, LOW);
   delay(timer);
   
   digitalWrite(pin4, HIGH);
   delay(timer);
   digitalWrite(pin4, LOW);
   delay(timer);

   digitalWrite(pin5, HIGH);
   delay(timer);
   digitalWrite(pin5, LOW);
   delay(timer);

   digitalWrite(pin6, HIGH);
   delay(timer);
   digitalWrite(pin6, LOW);
   delay(timer);

   digitalWrite(pin7, HIGH);
   delay(timer);
   digitalWrite(pin7, LOW);
   delay(timer);
   
   digitalWrite(pin6, HIGH);
   delay(timer);
   digitalWrite(pin6, LOW);
   delay(timer);
   
   digitalWrite(pin5, HIGH);
   delay(timer);
   digitalWrite(pin5, LOW);
   delay(timer);
   
   digitalWrite(pin4, HIGH);
   delay(timer);
   digitalWrite(pin4, LOW);
   delay(timer);
   
   digitalWrite(pin3, HIGH);
   delay(timer);
   digitalWrite(pin3, LOW);
   delay(timer); 
   }
   if (buttonState2 == HIGH) {    
   digitalWrite(pin2, LOW);
   digitalWrite(pin3, LOW);
   digitalWrite(pin4, LOW);
   digitalWrite(pin5, LOW);
   digitalWrite(pin6, LOW);
   digitalWrite(pin7, LOW);
   
   }
}

Moderator edit: The usual.

There is something wrong with the bottom 'if' part. So if you guys can help me how to arrange the code in order to be able to exit this while loop when pressing the other button, it will be greatly appreciated.

Thanks.

You're reading both switches at the same time, but you've got a load of delays in there, so you're acting on the state of button2 as it was at the top of the loop.

Thanks for the fast reply, really appreciated.

Can you clarify exactly how I can make the arduino read both buttons separately rather than at the same time?

If you look at your code, you read buttonState1 once at the top of "loop", then if it is HIGH, you enter the "while" loop, but you never read it again, so you'll stay in the "while" loop.
Even if you put another read in the while loop, you've got over a second's worth of delays, so you'll have to hold the button down for quite some time before it's read.

Time to look at the blink without delay example.

Thanks AWOL'

I am having a look at it now.