That won't work because there is no way for buttonState to change once in the loop. In other words you have to read buttonState each time through the loop to detect the change.
buttonState = digitalRead(buttonPin);
while(buttonState == HIGH)
{
// do something
buttonState = digitalRead(buttonPin); // read the button
}
or something like thiswhile(digitalRead(buttonPin) == HIGH)
{
// do something
}
I have a similiar question. I want to control a led with an button. If i use the "if/else" function it works fine.
Here is the "if/else" code:
void loop() {
int status_taster01 = digitalRead(taster01);
if (status_taster01 == 1){
digitalWrite(blau, HIGH);
}
else {
digitalWrite(blau, LOW);
}
}
the other code with the "while" function doesn't work fine:
void loop() {
int status_button_01 = digitalRead(button_01);
while(status_button_01 == HIGH) {
digitalWrite(blue_led, HIGH);
status_button_01 = digitalRead(button_01);
}
}
I don't understand why the led just keep working with the while-code.
I hope someone can see the error. Thank u very much.