Arduino内置教程-数字-不用delay的闪烁

不用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引脚。如图所示
    请输入图片描述

  • 大部分Arduino开发板上面就有一个LED灯连接到PIN13。如果你不连接硬件就运行这个例子,你应该也可以看到LED闪烁。

原理图

请输入图片描述

你插好电路板,连上电脑之后,打开Arduino IDE软件,输入以下代码。

样例代码

以下代码用 millis()函数来闪烁LED灯,如果开发板开始运行,会返回微秒的数值

/* Blink without Delay

 Turns on and off a light emitting diode (LED) connected to a digital
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.

 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.

 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 modified 11 Nov 2013
 by Scott Fitzgerald


 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to set a pin number :
const int ledPin =  13;      // the number of the LED pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

[Get Code]

更多

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

标签: arduino内置教程, arduino没有延迟的闪烁