-1

我试图允许一个按钮覆盖我设置为反复淡入淡出的 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);
}
4

1 回答 1

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);
}
于 2018-04-16T01:34:57.960 回答