0% found this document useful (0 votes)
43 views4 pages

Milis Multitasking

The document discusses using millis() instead of delay() for multitasking in Arduino. Delay() pauses program execution for a set time, preventing other tasks from running simultaneously. Millis() returns the number of milliseconds since startup without pausing execution, allowing multiple tasks to run concurrently. It describes storing the start time, comparing to the current time, and only taking action when the interval has passed to trigger events periodically without delaying the whole program. Millis() allows 100% CPU usage and avoids issues caused by delays interrupting other tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views4 pages

Milis Multitasking

The document discusses using millis() instead of delay() for multitasking in Arduino. Delay() pauses program execution for a set time, preventing other tasks from running simultaneously. Millis() returns the number of milliseconds since startup without pausing execution, allowing multiple tasks to run concurrently. It describes storing the start time, comparing to the current time, and only taking action when the interval has passed to trigger events periodically without delaying the whole program. Millis() allows 100% CPU usage and avoids issues caused by delays interrupting other tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 4

• The 

multitasking has led the computers to a  Arduino Multitasking Tutorial -


revolution where one or more programs can
run simultaneously which increases efficiency, How to use millis() in Arduino Code
flexibility, adaptability and productivity. In
embedded systems, microcontrollers can also
handle Multitasking and performs two or more
tasks simultaneously without halting the
current instructions.
• Generally a delay() function is used in Arduino
for a periodic task like LED Blinking but this
delay() function halt the program for some
definitive time and don’t allow other
operations to perform.
• What is Multitasking?
• Multitasking simply means executing more
than one task or program simultaneously at
the same time. Almost all operating systems
feature multitasking. This kind of operating
systems are known as MOS (multitasking
operating system). The MOS can be mobile or
desktop PC Operating System.
• If the reference documentation of Arduino is considered then
there is two type of delay functions, the first one is delay() and
second is delayMicroseconds().
• delay(1000) then the delay will be of 1000 milliseconds i.e. 1
second.
• delayMicroseconds(1000), then the delay will be of 1000
microseconds i.e. 1 milliseconds.

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

delay() in giving a delay of 1 second then the processor cannot go to


next instruction until 1 second passed.

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.

You might also like