一个低级的按键消抖
用着没发现什么问题
:lol
boolean A=false;
void setup()
{
Serial.begin(115200);
//初始化引脚
pinMode(LED_BUILTIN,OUTPUT);
pinMode(D1,INPUT);
digitalWrite(LED_BUILTIN,HIGH);
}
void loop()
{
if (digitalRead(D1)==HIGH&&A==false)
{
A=true;
Serial.println("开关动作");
if(digitalRead(LED_BUILTIN)==HIGH)
{
Serial.println("打开");
digitalWrite(LED_BUILTIN,LOW);
}
else
{
Serial.println("关闭");
digitalWrite(LED_BUILTIN,HIGH);
}
}
else if(digitalRead(D1)==LOW){A=false;}
}
果断收藏,一直都在关注消抖的文章 本帖最后由 BLACKBLUE 于 2021-11-20 16:10 编辑
#include <PinChangeInt.h>
uint32_t xxx_ ISR_time;
bool LED_BUILTIN_state ;
......
void setup(){
pinMode(xxx_pin, INPUT_PULLUP); //内部上拉,按下是低电平
pinMode(yyy_pin, INPUT); //悬空或者有物理下拉,按下高电平
digitalWrite(yyy_pin, LOW); //确定初始态
attachPinChangeInterrupt(xxx_pin,xxx_fun,FALLING);//
attachPinChangeInterrupt(yyy_pin,yyy_fun, RISING);
.....
}
void loop(){
digitalWrite(LED_BUILTIN,LED_BUILTIN_state);
}
void xxx_fun() {
if (millis() - xxx_ISR_time > 200)
{
xxx_ ISR_time = millis();
LED_BUILTIN_state = true;}
}
.....
页:
[1]