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.

 

Ähnliche Beiträge

Neue Beiträge

Leider ergab Ihre Suche kein Ergebnis

Aktuelles über Elektronikkomponenten­

Wir haben unsere Datenschutzbestimmungen aktualisiert. Bitte nehmen Sie sich einen Moment Zeit, diese Änderungen zu überprüfen. Mit einem Klick auf "Ich stimme zu", stimmen Sie den Datenschutz- und Nutzungsbedingungen von Arrow Electronics zu.

Wir verwenden Cookies, um den Anwendernutzen zu vergrößern und unsere Webseite zu optimieren. Mehr über Cookies und wie man sie abschaltet finden Sie hier. Cookies und tracking Technologien können für Marketingzwecke verwendet werden.
Durch Klicken von „RICHTLINIEN AKZEPTIEREN“ stimmen Sie der Verwendung von Cookies auf Ihrem Endgerät und der Verwendung von tracking Technologien zu. Klicken Sie auf „MEHR INFORMATIONEN“ unten für mehr Informationen und Anleitungen wie man Cookies und tracking Technologien abschaltet. Das Akzeptieren von Cookies und tracking Technologien ist zwar freiwillig, das Blockieren kann aber eine korrekte Ausführung unserer Website verhindern, und bestimmte Werbung könnte für Sie weniger relevant sein.
Ihr Datenschutz ist uns wichtig. Lesen Sie mehr über unsere Datenschutzrichtlinien hier.