Milis Multitasking
Milis Multitasking
Why to skip • Here comes the point, both functions pause the program for
the amount of time passed in delay function. So if we are
Arduino? • Similarly if the delay is 10 seconds then program will stop for
10 seconds and processor will not allow to go for the next
instructions until the 10 seconds passed. This hampers the
performance of the microcontroller in terms of speed and
executing the instructions.
• Consider we want to toggle two LEDs using two push buttons.
So if one push button is pushed then the corresponding LED
should glow for 2 seconds, similarly if second is pushed then
LED should glow for 4 seconds. But when we use delay(), if
the user is pressing the first button then the program will stop
for 2 seconds and if the user presses the second button before
2 seconds delay, then the microcontroller won’t accept the
input as the program is in halt stage.
• To overcome the problem caused by using delay, a developer should
use millis() function which is easy to use once you become habitual
and it will use 100% CPU performance without generating any delay
in executing the instructions.
Why to use • millis() is a function that just returns the amount of milliseconds
that have elapsed since the Arduino board began running the current
millis() ?
program without freezing the program. This time number will overflow
(i.e go back to zero), after approximately 50 days.
• Just like Arduino have delayMicroseconds(), it also has the micro
version of millis() as micros(). The difference between micros and
millis is that, the micros() will overflow after approximately 70
minutes, compared to millis() which is 50 days. So depending upon
the application you can use millis() or micros().
• To use the millis() for timing and delay, you need to record and store
the time at which the action took place to start the time and then
check at intervals whether the defined time has passed. So as stated,
store the current time in a variable.
• We need two more variables to find out if the required time has
passed. We have stored the current time in currentMillis variable, but
we also need to know that when did the timing period start and how
long is the period. So the Interval and previousMillis is declared. The
interval will tell us the time delay and previosMillis will store the last
time the event has occurred.