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() {
Button_state = digitalRead(12);
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.
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:
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.