|
。。。算了 我给加上注释吧,
[mw_shl_code=c,true]void setup ()
{
pinMode(2,INPUT);
pinMode(13,OUTPUT);
Serial.begin(9600);
}
void loop ()
{
if (digitalRead(2)==HIGH&&digitalRead(13)==LOW) //同时检测开关是否按下和led是否点亮(LOW为未点亮)
{
digitalWrite(13,HIGH); //点亮led
Serial.print("Turn on the LED"); //串口发出字符串
while (digitalRead(2)==HIGH) //消抖,检测按钮是否一直按下,如是测无限延迟,如不加此行代码按下按钮则会不停开关,松开时led状态不可控
{
delay(1);
}
}
else if (digitalRead(2)==HIGH&&digitalRead(13)==HIGH) //同时检测开关是否按下和led是否点亮(high为点亮)
{
digitalWrite(13,LOW); //关灯
Serial.print("Turn off the LED");
while (digitalRead(2)==HIGH)
{
delay(1);
}
}
}[/mw_shl_code]
次代码的作用是 按下按钮灯泡亮,再按下灯泡灭 |
|