# Arduino wave diod with working button

Me and my friend are working in school trying to make it so when the button is being pressed it creates a wave motion with the diods, we managed to do that but no we're trying to make it so when the button isn't being pressed anymore the sequence stops.

``````int LB = 0;
int RB = 0;
int BB = 0;
int Button_state = 0;

void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(11, INPUT);
pinMode(12, INPUT);

}

void loop() {
while (Button_state == LOW)
{
digitalWrite(7, 1);
delay(200);
if (Button_state == HIGH){break; }
digitalWrite(7, 0);
delay(200);
if (Button_state == HIGH){break; }
digitalWrite(6, 1);
delay(200);
if (Button_state == HIGH){break; }
digitalWrite(6, 0);
delay(200);
if (Button_state == HIGH){break; }
digitalWrite(5, 1);
delay(200);
if (Button_state == HIGH){break; }
digitalWrite(5, 0);
delay(200);
if (Button_state == HIGH){break; }
digitalWrite(4, 1);
delay(200);
if (Button_state == HIGH){break; }
digitalWrite(4, 0);
delay(200);
if (Button_state == HIGH){break; }
digitalWrite(3, 1);
delay(200);
if (Button_state == HIGH){break; }
digitalWrite(3, 0);
delay(200);
if (Button_state == HIGH){break; }
digitalWrite(2, 1);
delay(200);
if (Button_state == HIGH){break; }
digitalWrite(2, 0);
delay(200);
if (Button_state == HIGH){break; }
}
``````

This code is far from what the original one was but I don't see how it isn't working? If the button state is low it should do the sequence but if i stop pressing the button and the state is high shouldn't it stop the entire thing?

Instead of doing anything right now it just keeps going, even after i stop pressing it it just keeps going without stop.

Hmm, let's see where `Button_state` is used (either it is updated or its value is read).

``````int Button_state = 0;

void setup() {
pinMode(12, INPUT);
}

void loop() {
while (Button_state == LOW)
{
if (Button_state == HIGH){break; }
if (Button_state == HIGH){break; }
if (Button_state == HIGH){break; }
if (Button_state == HIGH){break; }
if (Button_state == HIGH){break; }
if (Button_state == HIGH){break; }
if (Button_state == HIGH){break; }
if (Button_state == HIGH){break; }
if (Button_state == HIGH){break; }
if (Button_state == HIGH){break; }
if (Button_state == HIGH){break; }
if (Button_state == HIGH){break; }
}
}
``````

I just removed any line not related to it from your code - I hope this helps you to spot the problem!

I see what the issue might be. In your code, the `Button_state` is only read once at the beginning of each loop iteration. This means that once it's set to LOW, the loop will continue to execute the sequence of LED changes without checking the button state again until the sequence is complete.

To make it stop immediately when the button is released, you need to continuously check the `Button_state` within the loop. Here's a modified version of your code to achieve this:

``````int LB = 0;
int RB = 0;
int BB = 0;
int Button_state = 0;

void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(11, INPUT);
pinMode(12, INPUT);
}

void loop() {
while (Button_state == LOW) {
digitalWrite(7, 1);
delay(200);
digitalWrite(7, 0);
delay(200);
digitalWrite(6, 1);
delay(200);
digitalWrite(6, 0);
delay(200);
digitalWrite(5, 1);
delay(200);
digitalWrite(5, 0);
delay(200);
digitalWrite(4, 1);
delay(200);
digitalWrite(4, 0);
delay(200);
digitalWrite(3, 1);
delay(200);
digitalWrite(3, 0);
delay(200);
digitalWrite(2, 1);
delay(200);
digitalWrite(2, 0);
Button_state = digitalRead(12); // Check the button state in the loop
}
}
``````

By adding `Button_state = digitalRead(12);` inside the loop, you continuously check the button state while executing the LED sequence. This should make the sequence stop immediately when the button is released.

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