我正在尝试允许按钮覆盖我的LED,该LED设置为反复淡入淡出。相反,按钮只是关闭微控制器Adafruit Huzzah ESP8266本身的LED,对引脚13 LED没有任何影响。
代码:
const int buttonPin = 2; // the number of the pushbutton pin
int ledPin = 13; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, HIGH);
}
analogWrite(ledPin, brightness);
buttonState = digitalRead(buttonPin);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
答案 0 :(得分:1)
如下所示更改循环并尝试
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(ledPin, LOW);
} else {
analogWrite(ledPin, brightness);
}
buttonState = digitalRead(buttonPin);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}