执行动作:
按一次按钮并松开:led灯亮1s后灭掉
连续按两次按钮并松开:led灯由灭到亮,再由亮到灭(呼吸灯)
这两个动作可以不连续
int ledPin = 3;
int buttonPin = 5;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(buttonPin) == HIGH) {
while (digitalRead(buttonPin) == HIGH);
delay(200);
if (digitalRead(buttonPin) == HIGH) {
for (int fadeValue = 0; fadeValue <= 255; fadeValue += 1) {
analogWrite(ledPin, fadeValue);
delay(5);
}
for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 1) {
analogWrite(ledPin, fadeValue);
delay(5);
}
}
else {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
}
}
}