how to break while loop?

int button1 = 7;
int button2 = 8;
int buttonState1 = 0;
int buttonState2 = 0;
int ledPin1 = 12;
int ledPin2 = 13;
boolean k = false;

void setup()
{
  pinMode(button1 , INPUT);
  pinMode(button2 , INPUT);
  pinMode(ledPin1 , OUTPUT);
  pinMode(ledPin2 , OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);
  if (buttonState1 == HIGH)
  {
    k=true;
    while(k !=false)
    {
      Serial.println(k);
      digitalWrite(ledPin1 , HIGH);
      delay(1000);
      digitalWrite(ledPin1 , LOW);
      delay(1000);
      digitalWrite(ledPin2 , HIGH);
      delay(1000);
      digitalWrite(ledPin2 , LOW);
      delay(1000); 
      
      if (buttonState2 == HIGH)
    {
      k = false;
      {
        digitalWrite(ledPin1 , LOW);
      delay(1000);
      digitalWrite(ledPin2 , LOW);
      delay(1000); 
      }
    }
    }    
  }
}

when i press second button , my led still blinking .

how to break while loop?

One of the ways is by using break statement(it makes sense).

You should write pseudocode first, just to be sure that what you want to achive is the same as what do you think you want to achive.

After you press the button the first time there is a four second window where button presses are being ignored.

      Serial.println(k);
      digitalWrite(ledPin1 , HIGH);
      delay(1000);
      digitalWrite(ledPin1 , LOW);
      delay(1000);
      digitalWrite(ledPin2 , HIGH);
      delay(1000);
      digitalWrite(ledPin2 , LOW);
      delay(1000);

After that 4 second window, you get one read of the button and then it looks like you go back around for four more seconds. So for most of the time, you are ignoring your button. If you hold the button down and give it time to get through the whole sequence what happens? It should turn the two leds off and then loop will be called again and the whole process restarts.

i pressed button2 more than 4 seconds but it still wont stop my while .

Once you enter the WHILE loop it will stay in there and never read button2. (the only place you read the buttons is BEFORE you enter that while loop)

So if button2 wasn't down BEFORE you press button1 it will never get seen.

from the reference , while loop can be stopped if the condition become false . from my code , why i press button 2 , it wont stop the condition ?

klknight93:
from the reference , while loop can be stopped if the condition become false . from my code , why i press button 2 , it wont stop the condition ?

Pressing button2 will not change the value of buttonState2 because you’re not getting out of the loop.

void loop()
{
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);<<<<<THIS IS WHERE YOU LOOK AT THE BUTTON

  if (buttonState1 == HIGH)
  {

    k=true;
    while(k !=false)
    {
      //ONCE YOU GET INTO THIS LOOP, THE VALUE OF buttonState2 is SET IN STONE!
      Serial.println(k);
      digitalWrite(ledPin1 , HIGH);
      delay(1000);
      digitalWrite(ledPin1 , LOW);
      delay(1000);
      digitalWrite(ledPin2 , HIGH);
      delay(1000);
      digitalWrite(ledPin2 , LOW);
      delay(1000); 
      
      /*
      JUST LOOKING AT THE VALUE OF buttonState2 WILL NOT CHANGE ANYTHING
      buttonState2 is NOT getting updated with the state of the button
      IT will only get updated IF you exit this WHILE loop somehow, which is never going to happen
      */
      if (buttonState2 == HIGH)
      .....

thx for answering my question . may guide me how to get out from the loop ?

    while(k !=false)
    {
...      
      if (buttonState2 == HIGH)
    {
      k = false;

You don't read (into) buttonState2, so you will not detect that it becomes HIGH.