哎,別笑,總比刷抖音強點吧
1、效果
2、代碼
const int buttonPin=2;
const int ledPin=13;int buttonState=0;void setup() {// put your setup code here, to run once:pinMode(buttonPin,INPUT);pinMode(ledPin,OUTPUT);
}void loop() {// put your main code here, to run repeatedly:buttonState=digitalRead(buttonPin);if(buttonState==HIGH){digitalWrite(ledPin,HIGH);}else{digitalWrite(ledPin,LOW);}
}
3、效果
4、代碼
const int buttonPin=2;
const int ledPin=13;int buttonState=0;
int ledState=0;void setup() {// put your setup code here, to run once:pinMode(buttonPin,INPUT);pinMode(ledPin,OUTPUT);
}void loop() {// put your main code here, to run repeatedly:while(digitalRead(buttonPin)==LOW){}if(ledState==0){digitalWrite(ledPin,HIGH);ledState=1;}else{digitalWrite(ledPin,LOW);ledState=0;}delay(500);
}
5、總結
1、重新復習了下拉電阻的概念。
2、while(digitalRead(buttonPin)==LOW){}
這句代碼,Arduino系統內部是有中斷器的,可以監聽外部的輸入