# Multitasking while flashing an LED using Using millis()

**URL:** <https://forum.arduino.cc/t/multitasking-while-flashing-an-led-using-using-millis/937037>\
**Category:** Programming\
**Created:** [December 18, 2021, 5:05am UTC](https://forum.arduino.cc/t/multitasking-while-flashing-an-led-using-using-millis/937037 "2021-12-18T05:05:17Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [December 20, 2021, 8:06am UTC](https://forum.arduino.cc/t/multitasking-while-flashing-an-led-using-using-millis/937037/14 "2021-12-20T08:06:29Z")

</div>

Hi gold523,

as an everyday example with easy to follow numbers  
delay() is blocking. As long as the delay is "delaying" nothing else of the code can be executed.  
Now there is a technique of non-blocking timing.  
The basic principle of non-blocking timing is **fundamental different** from using delay()

**You have to understand the difference first** and then look into the code.

otherwise you might try to "see" a "delay-analog-thing" in the millis()-code **which it really isn't**  
Trying to see a "delay-analog-thing" in millis() makes it hard to understand millis()  
Having understood the basic principle of non-blocking timing based on millis() makes it easy to understand.

imagine baking a frosted pizza  
the cover says for preparation heat up oven to 200°C  
then put pizza in.  
Baking time 10 minutes

You are estimating heating up needs 3 minutes  
You take a look onto your watch it is 13:02 (snapshot of time)  
You start reading the newspaper and from time to time looking onto your watch  
watch shows 13:02. 13:02 - 13:02 = 0 minutes passed by not yet time  
watch shows 13:03. 13:03 - 13:02 = 1 minute passed by not yet time  
watch shows 13:04. 13:04 - 13:02 = 2 minutes passed by not yet time

watch shows 13:05 when did I start 13:02? OK 13:05 - 13:02 = 3 minutes time to put pizza into the oven

New basetime 13:05 (the snapshot of time)  
watch 13:06 not yet time  
watch 13:07 not yet time  
watch 13:08 not yet time (13:08 - 13:05 = 3 minutes is less than 10 minutes  
watch 13:09 not yet time  
watch 13:10 not yet time  
watch 13:11 not yet time  
watch 13:12 not yet time  
watch 13:13 not yet time  
watch 13:14 not yet time (13:14 - 13:05 = 9 minutes is less than 10 minutes  
watch 13:15 when did I start 13:05 OK 13:15 - 13:05 = 10 minutes time to eat pizza (yum yum)

You did a repeated comparing how much time has passed by  
This is what non-blocking timing does

In the code looking at "How much time has passed by" is done

currentTime - startTime \>= bakingTime

bakingTime is 10 minutes

13:06 - 13:05 = 1 minute \>= bakingTime is false  
13:07 - 13:05 = 2 minutes \>= bakingTime is false  
...  
13:14 - 13:05 = 9 minutes \>= bakingTime is false  
13:15 - 13:05 = 10 minutes \>= bakingTime is TRUE time for timed action!!

So your loop() is doing

```arduino
void loop()
  // doing all kinds of stuff like reading the newspaper
  
  if (currentTime - previousTime >= period) {
    previousTime = currentTime; // first thing to do is updating the snapshot of time 
    // time for timed action
  }

```

it has to be coded exactly this way because in this way it manages the rollover from Max back to zero  
of the function millis() automatically

And here is a demo code that shows the software-technique of state-machines

> [@A Demo-Code explaining the switch-case state-machine and how to do things (almost) in parallel](https://forum.arduino.cc/t/a-demo-code-explaining-the-switch-case-state-machine-and-how-to-do-things-almost-in-parallel/888172):
>
> Hi everybody, if you look at the end of this first posting you see a list of links that show where this tutorial was used as a link. Go through this list to find additional information on how to apply this code This democode requires only the pure microcontroller without any hardware. You just open the serial monitor to watch the behaviour of the code. From time to time I enjoy writing demo-codes that want to explain programming-functionalities. There are a lot of different approachs to expl…

best regards Stefan

---

_[View the full topic](https://forum.arduino.cc/t/multitasking-while-flashing-an-led-using-using-millis/937037)._
