How to break while infinite loop using 2nd button or 3rd button and start another LED behaviour

Note that I used the press and release button when I press the button the LEDs are off and after I release the button LED behavior will start, and how to stop the infinite loop if I'll try to press and release another button and try to use another LED behaviour

const int  buttonPin = 4;   
const int  buttonPin2 = 3;
const int  buttonPin3 = 2;
const int ledPin[8] = {13,12,11,10,9,8,7,6};      
int buttonState = 0;   
int buttonState2 = 0; 
int buttonState3 = 0; 
int lastButtonState = 0; 
int lastButtonState2 = 0; 
int lastButtonState3 = 0;  
int d = 250;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  for (int i=0; i<8; i++){
    pinMode(ledPin[i], OUTPUT);
  }
}
void loop() {
//BLINKING LIGHTS
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == LOW) {  

      while(true){ 
       		for (int i=0; i<8; i++){
       		digitalWrite(ledPin[i], HIGH);
      		}
  	  		delay(d);
        	for (int i=0; i<8; i++){
       		digitalWrite(ledPin[i], LOW);
            }
            delay(d); 		
      }
            for (int i=0; i<8; i++){
            digitalWrite(ledPin[i], LOW);
       }
      } 
    else {
	   for (int i=0; i<8; i++){
       digitalWrite(ledPin[i], LOW);
    }
  }
  lastButtonState = buttonState;
}
//RING COUNTER
  buttonState2 = digitalRead(buttonPin2);
  if (buttonState2 != lastButtonState2) {
    if (buttonState2 == LOW) {  
       while(true) {
       		for (int i=0; i<8; i++){
       		digitalWrite(ledPin[i], HIGH);
  	   		delay(d);
       		digitalWrite(ledPin[i], LOW);
      		delay(d);
            }
            for (int i=0; i<8; i++){
            digitalWrite(ledPin[i], LOW);
       }
      }
      } 
    else {
	   for (int i=0; i<8; i++){
       digitalWrite(ledPin[i], LOW);
    }
  }
  lastButtonState2 = buttonState2;
}  
//JOHNSON COUNTER
  buttonState3 = digitalRead(buttonPin3);
  if (buttonState3 != lastButtonState3) {
    if (buttonState3 == LOW) {  
      while(true) {
       		for (int i=0; i<8; i++){
      		digitalWrite(ledPin[i], HIGH);
      		delay(500);
     		}
      		for (int i=0; i<8; i++){
      		digitalWrite(ledPin[i], LOW);
      		delay(500);
      		}  
            for (int i=0; i<8; i++){
            digitalWrite(ledPin[i], LOW);
       }
      }
      } 
    else {
	   for (int i=0; i<8; i++){
       digitalWrite(ledPin[i], LOW);
    }
  }
  lastButtonState3 = buttonState3;
}    
}

First thought…
Using while(true); is a huge mistake if you want to maintain control of the program.

Restructure your code to use the implicit looping of loop() { }, then you can not only control your existing code, but you can add features without digging a deeper hole.

1 Like

Agree with @lastchancename.

You need to restructure things so that you only have a single loop running, or you will never be able to do things in parallel.

You also can't use delay() as this blocks all subsequent code while it does nothing. You need to keep track of timing with millis() instead.

Have a look at the Blink Without Delay and Button examples.

1 Like
void loop() {
  //BLINKING LIGHTS
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == LOW) {

      while (true) { // your code will NEVER NEVER NEVER exit this loop
        for (int i = 0; i < 8; i++) {
          digitalWrite(ledPin[i], HIGH);
        }
        delay(d);
        for (int i = 0; i < 8; i++) {
          digitalWrite(ledPin[i], LOW);
        }
        delay(d);
      } // <== this is the closing curly brace of your NEVER-ending while(true)-loop

in addition:
Are you ready to learn a

very different way

of coding?

it is very different because this different way of coding uses

  • zero times function delay()
  • zero times for-loops
  • zero times while-loops

all looping is done by

void loop()

.
.
I'm sure this creates a big

?

inside your head.
Huh ? What? How should I ever code .... ???

If you want a functionality like you have describes above. There are two ways:

Way 1:
don't learn anything new and have to write a lot of code for it.
which means adding a lot of things to each and every single for-loop and each and every single while-loop

Way 2:
learn something new

only once

and then write much shorter code

which way do you prefer?

best regards Stefan

Studying state machine would help

1 Like

Hello eschewer
The ussage of a home brewed botton manager and the design of a structured array for the led states would be help too.
Have a nice day and enjoy coding in C++.
Дайте миру шанс!

More code means greater opportunity for bugs.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.