Industrial Arduino Millis () vs Delay ()

Difference between Arduino Time functions
September 28, 2020 by
Industrial Arduino Millis () vs Delay ()
Boot & Work Corp. S.L., Marti Guillem Cura

Index

Your Dynamic Snippet will be displayed here... This message is displayed because you did not provided both a filter and a template to use.

Introduction of Industrial Arduino Millis () vs Delay ()

It is very common in industrial automation projects to program repetitive sequences in specific time intervals. Arduino programming language provides some time functions to control the Arduino board of your industrial PLC controller and perform computations to accomplish this. The most common functions to work with time are millis() and delay(), but what are the differences between them and which function is better to use in each case? In this post, we will talk about them and we will see some examples of their use.

Requirements to program Millis () and Delay () functions

To execute this program, we will need the following:

Time functions of Millis () and Delay ()

Using Arduino IDE there are functions defined by default as the time functions such as Millis() and Delay(). They will allow you to control.

Millis (function)

First of all, you need to know what the millis() function does. It will return the number of milliseconds that have passed since the PLC Arduino board started running the current program. It has a time limit of approximately 50 days, after this time, it will overflow and go back to zero. If the program needs to run longer than this, an extra counter might be required. If a more precise amount of time is needed, there is a function called micros(), which has the same functionality as the millis() function but works with microseconds. 

Delay (function)

On the other hand, the delay() function will stop the program for a specific amount of milliseconds that you will have specified on the parameters. Although it is easy to use this function, it has the disadvantage that any other action can be performed during its use. If a more accurate amount of time is needed, there is a function called DelayMicroseconds(), which has the same functionality as delay() but works with microseconds

Delay vs Millis (function)

The first difference you can see is that millis() has no parameter but returns the amount of time that has passed; while the delay() will require the number of milliseconds we want to pause the program but will not return anything.

Even though both functions will work with milliseconds and could be used to control time, unlike millis(), the delay() is a blocking function. A blocking function is a function that prevents a program from doing anything else until that task has been completed. This means that by using delay() you cannot execute any other tasks during the time that you have specified. 

Which function should I use?

Both functions can be used in most cases, but sometimes one is better than the other. For example, if you want to print a message every 5 seconds without any other conditions, both can work perfectly:

Millis() example

 unsigned long time;
void setup() {
  Serial.begin(9600);
}
void loop() {
  time = millis();
  Serial.println("Hello World");
  while(millis() < time+5000);
}

Delay() example

void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.println("Hello World");
  delay(5000);
}

In case you only want to perform an action, delay() is easier to implement, as you can see on the above codes. You will only have to call the function, blocking the program for the specified time. 

On the other hand, if you want to perform an operation in which two actions must be executed simultaneously, the delay() function should not be used since the program will be blocked on its call. If this happens, both actions will be stopped for the amount of time specified in the function. In a complex program, this could cause a significant error that could spoil it. In the following example, you can see how to blink two LEDs using different time intervals using the millis() function. Using the delay() function, it will not be possible to do it simultaneously.

Did you like this post?

If you are looking for more info about industrial controllers and Arduino automation, we recommend you read about Increasing/Decreasing Timer.


​Search in our Blog

Industrial Arduino Millis () vs Delay ()
Boot & Work Corp. S.L., Marti Guillem Cura September 28, 2020
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>