Why is the while-loop stopping when you didn't read the button again inside the loop???
@jasonericson26786 ай бұрын
Yes, to get this to work: void loop() { int buttonState = digitalRead(buttonPin); while (buttonState == LOW) { digitalWrite(LEDPin, HIGH); delay(200); digitalWrite(LEDPin, LOW); delay(200); buttonState = digitalRead(buttonPin); } }
@hetori9918 Жыл бұрын
so in the first example showcasing the while loop command, line10 declares "int buttonState = digitalRead(buttonPin)" to store the pin#7's high or low signal. Using while (buttonState == LOW) to trigger the LED to be ON and OFF codes using button here at 4:17, I found out that the "while" loop command only needs one condition check, after that It will repeatedly run codes. so the button became a onetime trigger. so I changed the "while" to "if", The problem was solved, and now the button can control the LED to be ON and OFF. guess the difference between "while" and "if" is, the "if" command checks the condition true or false every time, "while" only checks once.
@zirconiumvetsel3 ай бұрын
or you can still use "while", but add one line of statement at the end. " buttonState = digitalRead(buttonPin); "