С Arduino я беру аналоговый вход с потенциометра. С помощью входа я регулирую, насколько мигает моя лампа. Проблема в том, что когда я пытаюсь включить или выключить лампу, она мигает или не мигает. Я могу заставить кнопку включить лампу, но не могу заставить ее выключить лампу.
Мой l oop, который заставляет лампу мигать, имеет переменную, которая должна быть 1, чтобы он работал. Однако, когда я изменяю переменную на 0 с помощью оператора if, мигание l oop не прекращается, и лампа продолжает мигать.
Вот мой код:
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int buttonPin = 11; //select the pin for the button
int buttonState = 0; //variable to start and stop the led
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
if(digitalRead(buttonPin)==HIGH){
buttonState = 1;
delay(1000) //So that buttonState does not instantly change back
}
if(digitalRead(buttonPin)==HIGH && buttonState == 1;){
buttonState = 0;
delay(1000) //So that buttonState does not instantly change back
}
while(buttonState == 1){
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
}
Спасибо за помощь !