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


 Introduction

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 in 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 ()

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.

Blink LEDs using millis() function

int Led1,Led2;
int Period1, Period2;
unsigned long Time1, Time2;

void setup() {
    pinMode(13,OUTPUT);
    pinMode(12,OUTPUT);
    Time1 = Time2 = millis();
    Period1 = 1000;
    Period2 = 2000;
}

void loop() {
  if(millis()-Time1>=Period1){
    Led1=!Led1;
    digitalWrite(13,Led1);
    Time1=millis();
  }
  if(millis()-Time2>=Period2){
    Led2=!Led2;
    digitalWrite(12,Led2);
    Time2=millis();
  }
}

In conclusion, the millis() function is better in general and it is highly recommended to use before the delay() function. It will allow us to program using different threads at the same time and is more accurate. The delay() is only recommended to be used in simple programs if a program blocking action is needed.

Did you like this post?

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


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

Looking for your ideal PLC?

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 >>

Do you want more information?

Just fill the form!

Tell me more!