初学Arduino——按钮控制(四)
程序员文章站
2022-07-12 10:18:09
...
执行动作:
采用两个按钮控制,按下一个按钮时:led变为呼吸灯(先由灭到亮,再从亮到灭);
当按下另外一个按钮并松开时,led灯瞬间灭掉。
int led = 3;
int button1 = 5;
int button2 = 8;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(button1) == HIGH) {
while (digitalRead(button1) == HIGH || digitalRead(button2) == LOW)
{
for (int fade = 0; fade <= 255; fade += 1) {
if (digitalRead(button2) == HIGH) {
digitalWrite(led, LOW);
break;
}
analogWrite(led, fade);
delay(5);
}
for (int fade = 255; fade >= 0; fade -= 1) {
if (digitalRead(button2) == HIGH) {
digitalWrite(led, LOW);
break;
}
analogWrite(led, fade);
delay(5);
}
}
}
}
上一篇: 嵌入式基础总结1
下一篇: html meta标签使用总结