Using Arduino Delay and Millis Commands: Arduino for Beginners

Arduino beginners will probably spend a lot of time on the Arduino Playground, exploring and experimenting with the sketches others have written. As you gain experience and begin to write your own sketches, understanding the difference between the delay() and millis() commands is essential.

Despite sharing some superficial commonalities, these two commands are quite different and useful for different kinds of applications. Here, we’ll clarify the difference and examine how to start using the Arduino millis() command effectively.

Arduino Commands: Millis vs. Delay 

Let’s start with the similarities:

1. You can use both delay() and millis() commands to regulate the timing of operations.

2. We measure both in milliseconds.

The differences, however, are what make the millis() command really shine. Simply put, the primary difference is that the delay() command regulates the timing of an activity (such as blinking an LED) by temporarily suspending nearly all of the Arduino’s functions for a specified amount of time. The millis() command, on the other hand, bases its timing on changes in a timer that starts at 0 and continues to advance, unrelated to other activities, then pauses and begins again.

In practical terms, while the delay() function interrupts the other processes – essentially putting everything on hold – the millis() command can establish timing without interrupting other functions, enabling a sort of “multitasking” effect.

For a very simple sketch, like making an LED blink without other functionality, the delay() command may be sufficient. After all, the unit doesn’t need to accomplish anything else in the time that the LED is on or off. If, however, your task involves more “moving parts” (such as blinking the LED and periodically printing out a counter), you’ll need millis() to do that.

In such a case, you can use the millis() command to set each task to occur when the counter reaches a certain difference (such as blinking every 500ms and printing out every 1500ms).

Millis () Code Example:

const byte ledPin =  13; //the number of the LED pin
byte ledState = 0; //ledState used to set the LED
unsigned long previousMillis1 = 0; //will store last time LED was updated
unsigned long interval1 = 500; //interval at which to blink (milliseconds)
unsigned long previousMillis2 = 0;
unsigned long interval2 = 1500;
unsigned int counter = 0;

void setup() {
  Serial.begin(19200);
  delay(2000);
  pinMode(ledPin, OUTPUT); //set the digital pin as output:
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis1 > interval1) {
    previousMillis1 = currentMillis; // save the last time I blinked the LED
    //if the LED is off turn it on and vice-versa:
    ledState ^= 1;
    digitalWrite(ledPin, ledState);
  }
  currentMillis = millis();
  if (currentMillis - previousMillis2 > interval2) {
    previousMillis2 = currentMillis; // save the last time I printed on the serial
    Serial.println(++counter);
  }
}

Looking at the code above, which we developed by slightly modifying this code, you can see that the command doesn’t stop the total functioning of the Arduino. Therefore, both tasks can occur on their own schedules without interruption.

How to Use Arduino Millis 

The delay() function is a great starting point, and you can use it to accomplish many simple tasks. It’s one of the first commands you’re likely to learn, but as you move forward and develop more complicated sketches, you should get familiar with the millis() command, which lets you run more complicated sketches with more tasks without the stops that delay() causes. For more detailed explanations of these commands and their differences, take a look at this post on the Arduino Forum or the tutorial by Norwegian Creations.

 

相关新闻文章

最新消息

Sorry, your filter selection returned no results.

请仔细阅读我们近期更改的隐私政策。当按下确认键时,您已了解并同意艾睿电子的隐私政策和用户协议。

本网站需使用cookies以改善用户您的体验并进一步改进我们的网站。此处阅读了解关于网站cookies的使用以及如何禁用cookies。网页cookies和追踪功能或許用于市场分析。当您按下同意按钮,您已经了解并同意在您的设备上接受cookies,并给予网站追踪权限。更多关于如何取消网站cookies及追踪的信息,请点击下方“阅读更多”。尽管同意启用cookies追踪与否取决用户意愿,取消网页cookies及追踪可能导致网站运作或显示异常,亦或导致相关推荐广告减少。

我们尊重您的隐私。请在此阅读我们的隐私政策。