Arduino millis() vs delay() - what is the difference?

Arduino millis() vs delay()

Arduino is a hardware & software based platform, which gives a great opportunity to the hobbyist, students to learn and create interesting projects.

However, by being expert with Arduino you can make advance projects as well.

 

In this article I am going to discuss about delay and millis() functions.

When we can use these functions to generate delay.

 

What is the millis() & delay() function in Arduino?

This function gives the time value in milliseconds since board was started.

Advantage of using this function is program does not stop during delay.

Other task continue to take place.

While if you use ‘delay’ function, then program is paused for the time set in delay function.

For example. If I use delay (5000)

Then program will be paused for 5000 milliseconds or 5 seconds.

To save the value of millis(), you should use variables of ‘unsigned long’ type because the value is in milliseconds and it increases very fast.

For example.

unsigned long current_time = millis();


While, to use the ‘delay()’ function, you just need to put a value as argument in delay() function parenthesis.

For example

delay(1000);

It creates delay of 1000 ms or 1 second.

You can use delay() function for delay when, the delay is very little or only single task is being performed, so that other task cannot be impacted due to this delay or pause.

Comments

Popular posts from this blog

How to Make Automatic Room Light Controller Without Microcontroller

How to drive high voltage/current load by small voltage signal from a microcontroller?

How to Read Analog Input & Use PWM pin as Analog Output