Create Makers

Arduino Functions Battle: delay() or millis()

delay VS millis Banner

Disclosure: Some of the links below are affiliate links and we only earn a small commission if you purchase through our links at no additional cost to you. The earning mainly used for maintaining the website.

The very first time you learned Arduino programming, “delay()” function is a very common code you used to make a delay or time interval. And nothing is wrong with that. But, there is a time when someone tells you, “Hey! Using delay() function is a very bad practice.” Is it?

How delay() and millis() functions work?

Both functions are time function but they work in a complete different way.

delay() function

Items

millis() function

delay()

Function

millis()

 ms: milliseconds to pause the program.

Parameters

 N/A

 N/A

Returns

 Time in milliseconds has elapsed since the program started.

 Pause the program in milliseconds before moving to the next line of code.

Description

 Return the time has passed without pausing the program and proceed to next line of code.

delay() function
millis() function

How millis() function changes the Way of Programming

The delay () function is well known for blocking the program from doing other tasks. Let’s say we use “delay(1000)”, the program is paused for 1 second before executing the next line of code. During this 1 second, no other tasks are running, literally doing nothing at all.

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("I'M BLOCKING YOUR WAY");
  Serial.println("Hello World");
  delay(1000);
}

From the code above, every 1 second will print “Hello World” (12:27:51.559 – 12:27:52.585). In between that time, there is “I’M BLOCKING YOUR WAY” and only appears one time in that 1 second. Seem legit and nothing wrong with that.

If you look at the millis() function code below, it is lengthy but here is the magic. Still the same, every 1 second will print “Hello World” (12:35:50.544 – 12:35:51.544). However, in between that time, “I’M DOING SOMETHING HERE” appears many times.

unsigned long previousMillis = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("I'M DOING SOMETHING HERE");
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis >= 1000){
    previousMillis = currentMillis;
    Serial.println("Hello World");
  }
}

If you are working on a simple project, it is absolutely fine to use the delay() function. When multiple inputs are required at the same time, the millis() function is the preferable way. Reading temperature, humidity, distance, and so on are not quite possible using the delay() function.

When the Arduino is reading an input, the delay() function just stops there until the input is done. Other inputs are paused and waiting for one another to be done. This not only blocks other inputs from being read but also offsets any timing in the program.

Your Thought

The delay() function is a simple and easy function to use. But the problem with this function is:

  • Blocking other tasks to be performed until one task is completed.
  • Any outputs rely on timing are difficult to be predicted.

 

If you are doing multitasking program, millis() function can solve most of your headache. The millis() function may not straight forward to code, but it is definitely a good bet rather than delay() function.

In your next project, which time functions you would consider to use? Comment below and let us know.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments