arduino 按钮读取,Arduino开关按钮

博主在使用Arduino时遇到一个问题,按钮在未按下时显示状态为0,但按下并释放后状态长时间保持1,然后才返回0。通过在代码中加入`INPUT_PULLUP`模式解决了这个问题,确保了干净的‘开’和‘关’状态,避免了由于输入引脚悬空导致的不稳定性。
摘要由CSDN通过智能技术生成

bb64647f75c69a6982db0b2c2bb613c6.png

I have a simple Arduino Project, 2 buttons and face a strange case that in the beginning status of buttons are 0, but after click on the button and release the status becomes 1 for long a time then back to 0, please what is the wrong??

Code:

int const BTN1_PIN=2;

int const BTN2_PIN=4;

void setup(){

pinMode(BTN1_PIN, INPUT);

pinMode(BTN2_PIN, INPUT);

Serial.begin(9600);

}

void loop(){

int status1=digitalRead(BTN1_PIN);

Serial.print("BTN1 Status :");

Serial.println(status1);

int status2=digitalRead(BTN2_PIN);

Serial.print("BTN2 Status :");

Serial.println(status2);

d

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值