防抖
    当按下时,按键经常产生错误的开/关变迁,这是机械物理的问题:这些变迁可能被读取为在短时间内多次按下,从而使程序变笨。这个例子示范了怎样使一个输入防抖,这意味着在一个短时间内检查两次来确保这个按键是确实被按下了。如果没有防抖,按一次这个按键可能会引起不可预测的结果。这个程序利用millis()函数来保持按下的时间间隔。
    硬件要求

    • Arduino or Genuino 开发板
    • 即时按键或者开关
    • 10k ohm 电阻
    • 连接线
    • 面包板

    电路
    数字-防抖 - 图1
    原理图
    数字-防抖 - 图2
    样例代码
    以下程序基于Limor Fried的防抖版本,但是她的例子的逻辑是反过来的。在她的例子里,当闭合时开关为低电平,而断开时开关未高电平。这里当按下开关时为高电平,没有按下时为低电平。

    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. }

    [Get Code]
    更多

    • pinMode()
    • digitalWrite()
    • digitalRead()
    • if()
    • millis()
    • DigitalReadSerial - 读取一个开关,然后打印其状态到串口监视器
    • Blink - 使LED灯开和关
    • Button State Change - 记录按键按下的次数