int8_t led_pin=13;int8_t led2_pin=12;
void setup() {// put your setup code here, to run once:pinMode(led_pin, INPUT);//输入pinMode(led2_pin, OUTPUT);//输出
}void loop() {// put your main code here, to run repeatedly:if(digitalRead(led_pin)==1){//digitalRead(引脚号)digitalWrite(led2_pin, LOW);}if(digitalRead(led_pin)!=1){digitalWrite(led2_pin, HIGH);}}
digitalRead(引脚号) //检测引脚电平 高为1 低为 0 或 HIGH // 1 //高 LOW // 0//低
按键结构
去抖动
#define DEBOUNCE_DELAY 20 // 延时等待的时间(单位:毫秒)bool debounce(int pin) {if (digitalRead(pin) == HIGH) {delay(DEBOUNCE_DELAY); // 等待if (digitalRead(pin) == HIGH) {return true; // 按键稳定为高电平}}return false; // 按键不是稳定的高电平
}