我嘗試了arduino並編程了一些按鈕與狀態切換。如果它是「開」,那麼它會變成「關」,反之亦然。Arduino Serial.println奇怪的錯誤
#include <Bounce.h>
const int buttonPin = 2;
const int ledPin = 6;
int ledState = HIGH;
int a = LOW;
int b = LOW;
Bounce push1 = Bounce(buttonPin,5);
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
push1.update ();
int x = digitalRead(push1.read());
if (x != b) {
if (x == HIGH) {
if (a == HIGH) {
a = LOW;
}
else {
a = HIGH;
}
}
else {
}
}
digitalWrite(ledPin, a);
Serial.println(a); // Weird thing
b = x;
}
它運行良好,但奇怪的是,當我編程時,我添加了一些串行打印來監視通過COM輸出。然後,它一切運作良好,我想消除Serial.println(a);
,但它不工作,然後!
循環不響應我的按鈕。 我錯過了什麼嗎?什麼會導致這種事情? 也許我錯過了什麼,所以新鮮的眼睛會很好:)
非常感謝!
我懷疑'Serial.println'是一個延遲。嘗試用'delay'替換'Serial.println',例如'延遲(100);'。 – mttrb