风筝
发表于: 2016-7-6 22:04:45 | 显示全部楼层

按键防抖(Debounce)

按键在按下时,由于机械和物理特定的原因,经常会产生一些开关变换,而这些变换会让程序误认为是短时间内进行了多次按键。本示例展示了如何对输入信号进行防抖,也就是在一段短时间内进行两次检查来确保按键确实被按下。如果没有防抖的话,按下一次按键会产生很多不可预知的结果。示例代码使用了mills()函数记录按键按下经过的时间。


所需硬件

-    Arduino或者Genuino开发板

-    按钮开关

-    10K电阻

-    导线

-    面包板


电路连接方式

button.png


原理图

button_sch.png

代码

以下代码基于Limor Fried的防抖方法,但是逻辑与她的示例相反。她的示例中,按键在闭合时返回低电平LOW,断开时返回高电平HIGH。而在本例中,按键按下时返回高电平,未按下时返回低电平LOW。

  1. /*
  2. Debounce

  3. Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  4. press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
  5. a minimum delay between toggles to debounce the circuit (i.e. to ignore
  6. noise).

  7. The circuit:
  8. * LED attached from pin 13 to ground
  9. * pushbutton attached from pin 2 to +5V
  10. * 10K resistor attached from pin 2 to ground

  11. * Note: On most Arduino boards, there is already an LED on the board
  12. connected to pin 13, so you don't need any extra components for this example.


  13. created 21 November 2006
  14. by David A. Mellis
  15. modified 30 Aug 2011
  16. by Limor Fried
  17. modified 28 Dec 2012
  18. by Mike Walters

  19. This example code is in the public domain.

  20. http://www.arduino.cc/en/Tutorial/Debounce
  21. */

  22. // constants won't change. They're used here to
  23. // set pin numbers:
  24. const int buttonPin = 2;    // the number of the pushbutton pin
  25. const int ledPin = 13;      // the number of the LED pin

  26. // Variables will change:
  27. int ledState = HIGH;         // the current state of the output pin
  28. int buttonState;             // the current reading from the input pin
  29. int lastButtonState = LOW;   // the previous reading from the input pin

  30. // the following variables are long's because the time, measured in miliseconds,
  31. // will quickly become a bigger number than can be stored in an int.
  32. long lastDebounceTime = 0;  // the last time the output pin was toggled
  33. long debounceDelay = 50;    // the debounce time; increase if the output flickers

  34. void setup() {
  35.   pinMode(buttonPin, INPUT);
  36.   pinMode(ledPin, OUTPUT);

  37.   // set initial LED state
  38.   digitalWrite(ledPin, ledState);
  39. }

  40. void loop() {
  41.   // read the state of the switch into a local variable:
  42.   int reading = digitalRead(buttonPin);

  43.   // check to see if you just pressed the button
  44.   // (i.e. the input went from LOW to HIGH),  and you've waited
  45.   // long enough since the last press to ignore any noise:

  46.   // If the switch changed, due to noise or pressing:
  47.   if (reading != lastButtonState) {
  48.     // reset the debouncing timer
  49.     lastDebounceTime = millis();
  50.   }

  51.   if ((millis() - lastDebounceTime) > debounceDelay) {
  52.     // whatever the reading is at, it's been there for longer
  53.     // than the debounce delay, so take it as the actual current state:

  54.     // if the button state has changed:
  55.     if (reading != buttonState) {
  56.       buttonState = reading;

  57.       // only toggle the LED if the new button state is HIGH
  58.       if (buttonState == HIGH) {
  59.         ledState = !ledState;
  60.       }
  61.     }
  62.   }

  63.   // set the LED:
  64.   digitalWrite(ledPin, ledState);

  65.   // save the reading.  Next time through the loop,
  66.   // it'll be the lastButtonState:
  67.   lastButtonState = reading;
  68. }
复制代码


跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 705 | 回复: 1492



手机版|

GMT+8, 2024-11-8 23:46 , Processed in 0.047720 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

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

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