不用delay的闪烁

    • 有时候你需要同时做两件事。如你可能想闪烁一个LED灯,同时读取一个按键。这种情况下你不能使用delay()。如果Arduino因为delay()函数被暂停时有按键按下,你的程序会错过这次的按键按下。
    • 这个程序示范怎样不用delay()来闪烁一个LED灯。它打开LED灯,并标注一个时间。然后每次运行完loop(),它都会查一下有没有超过需要的闪烁时间。如果它超过了,它就会切换LED灯为打开或者关闭状态,然后标注新的时间。在这种方法里LED灯不断闪烁,同时这个程序的执行从来没有漏掉一个指令。
    • 你在等一些重要邮件时,你可能要微波炉加热一个比萨。你把比萨放到微波炉,然后设置10分钟。用delay()就是坐在微波炉前面看着时间度过10分钟。如果重要邮件在这段时间到了,你会错过它。
    • 而现实中你能做到边加热比萨,边检查邮件,或者做其他事情,只要等到定时器到0(意味着比萨已经完成)时你回到微波炉前面。
    • 在这个教程你将会学习怎样设置一个简单的定时器。

    硬件要求

    • Arduino or Genuino开发板
    • LED
    • 220 ohm电阻

    电路

    • 先连接电阻的一端到Arduino的PIN13引脚。再连接LED灯的长引脚(正脚,也叫阳极)到电阻的另一端。最后连接LED灯的短引脚(负脚,也叫负极)到Arduino的GND引脚。如图所示
      数字-不用delay的闪烁 - 图1
    • 大部分Arduino开发板上面就有一个LED灯连接到PIN13。如果你不连接硬件就运行这个例子,你应该也可以看到LED闪烁。

    原理图
    数字-不用delay的闪烁 - 图2
    你插好电路板,连上电脑之后,打开Arduino IDE软件,输入以下代码。
    样例代码
    以下代码用 millis()函数来闪烁LED灯,如果开发板开始运行,会返回微秒的数值

    1. /* Blink without Delay
    2. Turns on and off a light emitting diode (LED) connected to a digital
    3. pin, without using the delay() function. This means that other code
    4. can run at the same time without being interrupted by the LED code.
    5. The circuit:
    6. * LED attached from pin 13 to ground.
    7. * Note: on most Arduinos, there is already an LED on the board
    8. that's attached to pin 13, so no hardware is needed for this example.
    9. created 2005
    10. by David A. Mellis
    11. modified 8 Feb 2010
    12. by Paul Stoffregen
    13. modified 11 Nov 2013
    14. by Scott Fitzgerald
    15. This example code is in the public domain.
    16. http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
    17. */
    18. // constants won't change. Used here to set a pin number :
    19. const int ledPin = 13; // the number of the LED pin
    20. // Variables will change :
    21. int ledState = LOW; // ledState used to set the LED
    22. // Generally, you should use "unsigned long" for variables that hold time
    23. // The value will quickly become too large for an int to store
    24. unsigned long previousMillis = 0; // will store last time LED was updated
    25. // constants won't change :
    26. const long interval = 1000; // interval at which to blink (milliseconds)
    27. void setup() {
    28. // set the digital pin as output:
    29. pinMode(ledPin, OUTPUT);
    30. }
    31. void loop() {
    32. // here is where you'd put code that needs to be running all the time.
    33. // check to see if it's time to blink the LED; that is, if the
    34. // difference between the current time and last time you blinked
    35. // the LED is bigger than the interval at which you want to
    36. // blink the LED.
    37. unsigned long currentMillis = millis();
    38. if (currentMillis - previousMillis >= interval) {
    39. // save the last time you blinked the LED
    40. previousMillis = currentMillis;
    41. // if the LED is off turn it on and vice-versa:
    42. if (ledState == LOW) {
    43. ledState = HIGH;
    44. } else {
    45. ledState = LOW;
    46. }
    47. // set the LED with the ledState of the variable:
    48. digitalWrite(ledPin, ledState);
    49. }
    50. }

    [Get Code]
    更多

    • setup()
    • loop()
    • millis()
    • Button - 用一个按钮来控制LED灯
    • Debounce - 读取一个按钮,并滤掉噪音
    • DigitalInputPullup - 示范怎么用pinMode()来上拉引脚
    • StateChangeDetection - 记录按键按下的次数
    • toneKeyboard - 一个用压力传感器和压电扬声器的三键音乐键盘
    • toneMelody - 用压电扬声器弹奏一个旋律
    • toneMultiple - 用tone()命令在多个扬声器上发音
    • tonePitchFollower - 用模拟输入来操作压电扬声器弹奏一个高音