监测按钮的状态变化(边沿监测)
    如果你让按键工作,你经常会想做一些要按下很多次按钮的才有响应的动作。这时你需要知道按钮的从闭合到断开的变化状态,然后记录这个状态发生了多少次。这叫状态变化检测或者边沿检测。在这个教程里我们学习怎样检查状态变化,然后我们把相关信息发送到串口监视器里,并记录4次LED灯的开和关。
    硬件要求

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

    电路
    数字-侦察状态改变 - 图1

    • 连接3根线到开发板。最开始两根,红和黑,连接到面包板上的两个长垂直行来提供5V电源电压和地。第三根线从数字引脚pin2连接到按钮的一个引脚。按钮的同一个引脚连接下拉电阻(10k ohm)到地。按钮的另一个引脚连接到5V电源。
    • 按钮或者开关连接电路的两点。按钮是断开的(未按),按钮两个引脚是没有接通的,所以这个引脚连接到地(通过一个下拉电阻),读取为低电平或者0。当如果按钮是闭合的(未按),按钮两个引脚是接通的,所以这个引脚连接到5V,读取为高电平,或者1。
    • 如果你没有连接到数字I/O口到任何地方,LED灯可能会不规则闪烁。这是因为输入引脚处于悬浮状态——它没有固定连接到电源或者地,并且它会随机在高电平和低电平之间切换。这是你需要下拉电阻的原因。

    原理图
    数字-侦察状态改变 - 图2
    样例代码
    下面的代码连续读取按钮的状态。然后通过循环来对比这个按钮的状态和它上一段时间的状态。如果当前按键的状态和之前的状态不一样并且当前状态是高电平,那么这个按键刚从关变为开。然后程序增加按键按下的计数器。
    这个程序也检查按键按下次数的值,并且如果它是4的倍数,它会打开pin13的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. }

    [Get Code]
    更多

    • pinMode()
    • digitalWrite()
    • digitalRead()
    • millis()
    • if
    • BlinkWithoutDelay - 不用delay()函数,使LED灯闪烁
    • Button - 用一个按钮来控制LED灯
    • Debounce - 读取一个按钮,并滤掉噪音
    • DigitalInputPullup - 示范怎么用pinMode()来上拉引脚
    • toneKeyboard - 一个用压力传感器和压电扬声器的三键音乐键盘
    • toneMelody - 用压电扬声器弹奏一个旋律
    • toneMultiple - 用tone()命令在多个扬声器上发音
    • tonePitchFollower - 用模拟输入在压电扬声器上弹奏高音