|
本次實(shí)驗(yàn)要實(shí)現(xiàn)按鍵控制LED亮滅效果。按下點(diǎn)亮,再次按下熄滅。
1.原理圖:
Arduino按鍵實(shí)驗(yàn).jpg (212.19 KB, 下載次數(shù): 96)
下載附件
xk100com
2018-7-27 10:07 上傳
2.代碼如下:
int ButtonState;
int ButtonLastState;
int ButtonCounter;
void setup() {
// put your setup code here, to run once:
pinMode(13,OUTPUT);
pinMode(11,INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
ButtonState = digitalRead(11);
if(ButtonState != ButtonLastState)
{
if(ButtonState)
{
Serial.println("on");
ButtonCounter++;
}
else
{
Serial.println("off");
}
delay(100);
}
ButtonLastState = ButtonState;
if(ButtonCounter%2)
{
//Serial.println(ButtonCounter);
digitalWrite(13,1);
}
else
{
digitalWrite(13,0);
}
}
若想學(xué)習(xí)更多Arduino知識(shí),可以搜查創(chuàng)客集結(jié)號(hào)
|
|