开关,下拉电阻,板载小灯,按开关灯亮
const int ledPin=13; const int inputPin=2; void setup() { // put your setup code here, to run once: pinMode(ledPin, OUTPUT); pinMode(inputPin, INPUT); //digitalWrite(inputPin, HIGH); Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: int val=digitalRead(inputPin); Serial.println(val); if(val==HIGH){ digitalWrite(ledPin, HIGH); }else{ digitalWrite(ledPin, LOW); } }
下拉电阻作用,开关不按时,引脚2读的是接地
开关按下,电流从5v,走过开关,走到电阻,走到接地
标签:val,arduino,int,inputPin,开关,digitalWrite,ledPin From: https://www.cnblogs.com/alexhjl/p/17602835.html