风筝
发表于: 2016-7-18 09:41:39 | 显示全部楼层

按键状态变化检测(State Change Detection)

在使用按键的过程中,经常会遇到要根据按键按下的次数来做一些处理。要做到这一点,我们需要知道按键的状态何时从关断变成闭合,并且记录下这样的状态变化发生的次数。这就是所谓的状态变化检测或者是边沿检测。本示例展示了如何来检测状态变化,将相关信息发送到串行监视器( Serial Monitor),并且在四次状态变化时来点亮或熄灭LED指示灯。


所需硬件

-    Arduino或者Genuino开发板

-    按钮开关

-    10k电阻

-    面包板

-    导线


电路连接方式

button.png

将三根导线连接到开发板。第一根导线将按键的一个引脚通过下拉电阻(用的是10k)连接到地。第二根导线将按键的另一个引脚连接到5V电源。第三根导线连接到数字IO引脚(2脚),通过该引脚来读取按键的状态。


当按钮处于开路状态(未按下)时,按键的两个引脚之间没有连接,所以该引脚连接到地(通过下拉电阻),并且读取时为低电平,或者0。当按键关闭(按下)时,两个引脚短接,使得该引脚连接到5V,因此读取时为高电平,或者1。


如果您断开数字I/O引脚的所有连接时,LED会无规律的闪烁。这是因为输入引脚处于悬浮状态,也就是说该引脚与电源或地之间没有固定的连接,并且会随机返回高电平或低电平。这就是为什么在电路中需要下拉电阻。


原理图

button_sch.png


代码

下面的程序不断读取按键的状态。然后在主循环中与上次读取的按键状态进行对比。如果当前值与上次的按键状态不同,并且当前按键的状态为高电平,则表示按键从关断变成闭合。然后程序中增加一次按键按下的计数。


程序也检测按键按下的次数。如果该值是4的倍数,那么就会就将13脚的LED灯点亮。否则,LED熄灭。

  1. /*
  2.   State change detection (edge detection)

  3. Often, you don't need to know the state of a digital input all the time,
  4. but you just need to know when the input changes from one state to another.
  5. For example, you want to know when a button goes from OFF to ON.  This is called
  6. state change detection, or edge detection.

  7. This example shows how to detect when a button or button changes from off to on
  8. and on to off.

  9. The circuit:
  10. * pushbutton attached to pin 2 from +5V
  11. * 10K resistor attached to pin 2 from ground
  12. * LED attached from pin 13 to ground (or use the built-in LED on
  13.    most Arduino boards)

  14. created  27 Sep 2005
  15. modified 30 Aug 2011
  16. by Tom Igoe

  17. This example code is in the public domain.

  18. http://www.arduino.cc/en/Tutorial/ButtonStateChange

  19. */

  20. // this constant won't change:
  21. const int  buttonPin = 2;    // the pin that the pushbutton is attached to
  22. const int ledPin = 13;       // the pin that the LED is attached to

  23. // Variables will change:
  24. int buttonPushCounter = 0;   // counter for the number of button presses
  25. int buttonState = 0;         // current state of the button
  26. int lastButtonState = 0;     // previous state of the button

  27. void setup() {
  28.   // initialize the button pin as a input:
  29.   pinMode(buttonPin, INPUT);
  30.   // initialize the LED as an output:
  31.   pinMode(ledPin, OUTPUT);
  32.   // initialize serial communication:
  33.   Serial.begin(9600);
  34. }


  35. void loop() {
  36.   // read the pushbutton input pin:
  37.   buttonState = digitalRead(buttonPin);

  38.   // compare the buttonState to its previous state
  39.   if (buttonState != lastButtonState) {
  40.     // if the state has changed, increment the counter
  41.     if (buttonState == HIGH) {
  42.       // if the current state is HIGH then the button
  43.       // wend from off to on:
  44.       buttonPushCounter++;
  45.       Serial.println("on");
  46.       Serial.print("number of button pushes:  ");
  47.       Serial.println(buttonPushCounter);
  48.     } else {
  49.       // if the current state is LOW then the button
  50.       // wend from on to off:
  51.       Serial.println("off");
  52.     }
  53.     // Delay a little bit to avoid bouncing
  54.     delay(50);
  55.   }
  56.   // save the current state as the last state,
  57.   //for next time through the loop
  58.   lastButtonState = buttonState;


  59.   // turns on the LED every four button pushes by
  60.   // checking the modulo of the button push counter.
  61.   // the modulo function gives you the remainder of
  62.   // the division of two numbers:
  63.   if (buttonPushCounter % 4 == 0) {
  64.     digitalWrite(ledPin, HIGH);
  65.   } else {
  66.     digitalWrite(ledPin, LOW);
  67.   }

  68. }
复制代码


跳转到指定楼层
liuvz
发表于: 2020-6-3 21:07:55 | 显示全部楼层

你好, 我问一下, 按下开关后为什么不是会短路啊? IO接口后面我看官网的结构图没有电阻啊, 是我看错了还是其他什么原因啊? 麻烦指导下吧, 或者在哪能找到资料呢? 目前找到的都没有说短路的问题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-19 06:58 , Processed in 0.185866 second(s), 8 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表