Arduino开关按钮 [英] Arduino switch button

查看:297
本文介绍了Arduino开关按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Arduino项目,有2个按钮,面对一个奇怪的情况,即按钮的开始状态为0,但是单击按钮并释放后,状态很长一段时间变成1,然后又回到0,请问是错的吗?

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??

代码:

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);

 delay(250);
}

在一开始的值是:
BTN1状态:0
BTN2状态:0
.
.

in the begining, the values is :
BTN1 Status :0
BTN2 Status :0
.
.

但是在单击button1并释放button1的状态后,需要很长时间才能恢复为0,输出如下:
BTN1状态:1
BTN2状态:0
BTN1状态:1
BTN2状态:0
BTN1状态:1
BTN2状态:0
BTN1状态:1
BTN2状态:0
BTN1状态:1
BTN2状态:0
BTN1状态:1
BTN2状态:0
BTN1状态:1
BTN2状态:0
BTN1状态:1
BTN2状态:0
BTN1状态:0
BTN2状态:0
BTN1状态:0
BTN2状态:0
BTN1状态:0
BTN2状态:0
BTN1状态:0
BTN2状态:0
BTN1状态:0
BTN2状态:0

But after click on the button1 and release the status of button1 take long time to back to 0, the output like:
BTN1 Status :1
BTN2 Status :0
BTN1 Status :1
BTN2 Status :0
BTN1 Status :1
BTN2 Status :0
BTN1 Status :1
BTN2 Status :0
BTN1 Status :1
BTN2 Status :0
BTN1 Status :1
BTN2 Status :0
BTN1 Status :1
BTN2 Status :0
BTN1 Status :1
BTN2 Status :0
BTN1 Status :0
BTN2 Status :0
BTN1 Status :0
BTN2 Status :0
BTN1 Status :0
BTN2 Status :0
BTN1 Status :0
BTN2 Status :0
BTN1 Status :0
BTN2 Status :0

推荐答案

设计的问题在于,当不按任何按钮时,您的I/O引脚均未连接任何东西.这将导致它们的值在某种程度上浮动",这意味着它们在1到0之间跳跃.通常,您可以通过一个高阻值电阻(例如10K欧姆)将I/O引脚直接连接到+ 5v,然后再连接I/O通过按钮接地.这样,当您在未按下按钮的情况下读取引脚时,会获得+ 5v的稳定电流(由于电阻而几乎不会产生任何电流),但是当您按下按钮时,则会短路(通过电阻)接地并得到一个稳定0v.这样,您按下的按钮状态为0v时,可以很干净地打开"和关闭".

The problem with your design is that, when no push button is pressed, your I/O pins are not connected to anything. This causes their values to kind of "float" around meaning they jump between 1 and 0. Usually you would connect the I/O pin directly to +5v via a high value resistor (i.e. 10K ohms) and then also connect the I/O pin to ground through a push button. This way, when you read the pin without the push button pressed, you get a solid +5v (and hardly any current because of the resistor), but when you press the button, you short to ground (through the resistor) and get a solid 0v. This gives you a very clean "on" and "off" where your pressed button state is 0v.

Arduino很酷,因为它们在板本身中内置了这些+ 5v电阻.您只需要使用pinMode(pinBUTTON, INPUT_PULLUP);启用它们即可.我已经对上面的电路布局进行了设计.

Arduinos are cool because they have these resistors to +5v built into the board itself. You just have to turn them on using pinMode(pinBUTTON, INPUT_PULLUP);. I have included a design of what your circuit layout should be above.

使用arduinos要记住的重要一件事是,您必须始终设置pinModes.这是一个容易忘记的步骤,如果没有它,arduino将有点"工作,但这是项目中奇怪结果的常见来源.

An important thing to remember with arduinos is you ALWAYS have to set your pinModes. This is an easy step to forget, and the arduino will "kind of" work without it, but it is a common source of odd results in your projects.

const int pinBUTTONONE = 2;
const int pinBUTTONTWO = 4;

void setup(){
  pinMode(pinBUTTONONE, INPUT_PULLUP);
  pinMode(pinBUTTONTWO, INPUT_PULLUP);
}

void setup(){
  if(digitalRead(pinBUTTONONE) == LOW){
    // Execute button one pressed code.
  }

  if(digitalRead(pinBUTTONTWO) == LOW){
    // Execute button two pressed code.
  }
}

这篇关于Arduino开关按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆