各位版上的大大晚上好。
我正在練習一個LED自保持,直到Button 再次Taggle時才改變LED狀態的程式
目前遇到的問題如下,如果按鈕按的時間比較長,動作就會異常。
目前推估應該是Loop持續執行,導致變數一直被更新,所以動作異常。
所以想問一下有沒有哪一個IDE可以查看變數變化?
我的環境及程式如下。
HW:Arduino UNO version 3
IDE: Arduino 1.8.5 for windwos
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int LEDstatus = 0; //if LEDstaus = 0, LED off, LEDstatu = 1, LED on
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
LEDstatus = 1 - LEDstatus;
}
if (LEDstatus == 1){
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}