我从代码开始,在按下按钮时打开 LED。那行得通。但后来我尝试调整它,使按钮的作用类似于“开-关”开关,您只需按一次即可在状态之间切换。
LED 适用于较旧的代码(如下),所以我认为这不是接线问题。仅供参考,我跳过了设置功能,它与我让它闪烁时相同。
// 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 switcher = 0;
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) {
if (switcher = 0) {
switcher = 1;
delay(500);
}
else if (switcher = 1) {
switcher = 0;
delay(500);
}
}
if (switcher == 1) {
digitalWrite(ledPin, HIGH);
}
/*
else {
digitalWrite(ledPin, LOW);
}
*/
}