# How to Code Millis

**URL:** <https://forum.arduino.cc/t/how-to-code-millis/1277516>\
**Category:** Programming\
**Created:** [July 2, 2024, 9:49am UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516 "2024-07-02T09:49:37Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![hyperjragon](https://avatars.discourse-cdn.com/v4/letter/h/cdc98d/32.png) [@hyperjragon](https://forum.arduino.cc/u/hyperjragon)\
**Post date:** [July 2, 2024, 9:49am UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516/1 "2024-07-02T09:49:37Z")

</div>

I am relatives new to arduino and dont understand how to Code millis

---

<div class="post-metadata">

**Author:** ![AresXT](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aresxt/32/1201192_2.png) [@AresXT](https://forum.arduino.cc/u/AresXT)\
**Post date:** [July 2, 2024, 9:55am UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516/2 "2024-07-02T09:55:01Z")

</div>

> [@Using millis() for timing. A beginners guide](https://forum.arduino.cc/t/using-millis-for-timing-a-beginners-guide/483573):
>
> Part 1 It is not usually long before new Arduino users discover that although the delay() function is easy to use it has side effects, the main one of which is that its stops all activity on the Arduino until the delay is finished (not quite true, I know, but that is usually how the problem presents itself). When this occurs the new user is usually directed to the BlinkWithoutDelay example sketch (BWoD) and/or the excellent thread entitled Several things at the same time. [Several things at the…](http://forum.arduino.cc/index.php?topic=223286.0)

Check out this guide

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 2, 2024, 10:00am UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516/3 "2024-07-02T10:00:26Z")

</div>

**1.** The _millis()_ is a function that you can use to know the elapse time since you have RESET the Arduino UNO to begin the execution of a sketch.

**2.** The _millisCounter_ is a 32-bit unsigned counter that continues recording of the elapse time at 1 ms interval on interrupt basis in the background.

**3.** You can use the following code to know and record the time accumulated by the _millisCounter_ by executing he following code:

`unsigned long int presentMillis = millis();`

If you have understood the above three theory, then you can easily apply _millis()_ function to compute the time gap between the occurrence of two events.

**Example:**  
Assume you have turned on the onBoard LED-L of Arduino UNO board now at time _t1_. You want to turn off LED-L exactly after 1-sec (1000 ms) without blocking the MCU (Microcontroller).

If you propose the following solution that will be wrong as the _delay(1000);_ function blocks the MCU which means that the MCU cannot do any other job until the declared 1-sec time has elapsed. The MCU is blocked.

You can use _mills()_ function as illustrated below to see when the desired 1-sec (1000 ms) time has gone; at the same time, you can also assign some othet task to the MCU for execution like checking if a button has been closed.

```arduino
unsigned long int presentMillis = millis(); //record pres time t1

void setup()
{
  pinMode(13, OUTPUT); //LED-1 is connected with DPin-13
  digitalWrite(13, HIGH); //LED-1 is on at time t1 = n ms
}

void loop()
{
  if (millis() - presentMillis >= 1000) //1-sec = 1000 ms time has elapsed
  {
    digitalWrite(13, LOW); //LED-L off; time gap = millis - t1 = n+1000 - n = 1000 ms 
    while (true); //wait here for ever
  }
  else
  {
    //code to check if a Button is closed and then take action
  }
}

```

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [July 2, 2024, 11:10am UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516/4 "2024-07-02T11:10:00Z")

</div>

As this is not a question about the IDE, I have moved your post to this section. Please be more careful where you post in future.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 2, 2024, 11:31am UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516/5 "2024-07-02T11:31:19Z")

</div>

> [@hyperjragon](#):
>
> dont understand how to Code millis

For extra information and examples look at

- [Using millis() for timing. A beginners guide](https://forum.arduino.cc/index.php?topic=503368.0)
- [Several things at the same time](https://forum.arduino.cc/index.php?topic=223286.0)
- [Flashing multiple LEDs at the same time](https://forum.arduino.cc/t/flashing-multiple-leds-at-the-same-time/1065564)
- 

EDIT @AresXT had already posted those

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 2, 2024, 12:48pm UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516/6 "2024-07-02T12:48:12Z")

</div>

> [@J-M-L](#):
>
> Several things at the same time

If three LEDs are contiuously blinked at different rates using _millis()_ function in Arduino UNO Platform, can it be termend as "multi-tasking"?

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [July 2, 2024, 3:16pm UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516/7 "2024-07-02T15:16:58Z")

</div>

> [@hyperjragon](#):
>
> dont understand how to Code millis

Hello [hyperjragon](https://forum.arduino.cc/u/hyperjragon)

Welcome to the best Arduino forum ever 🙂

You can select a project such as a traffic light system or similar to start with.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 2, 2024, 3:35pm UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516/8 "2024-07-02T15:35:12Z")

</div>

> [@GolamMostafa](#):
>
> If three LEDs are contiuously blinked at different rates using _millis()_ function in Arduino UNO Platform, can it be termend as "multi-tasking"?

I would say it depends at what level you look

For an observer looking at the leds, it seems that the arduino is doing multiple tasks at once, so colloquially you could say it’s multitasking

For a developer looking at the code, he will see only one process/thread/task as commonly referred to in software engineering, so it’s not.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 2, 2024, 3:50pm UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516/9 "2024-07-02T15:50:37Z")

</div>

I apologize to OP for hijacking the thread for a while.

**1.** If I blink those three LEDs at different rates using RTOS, then the process is really a multi-tasking process - am I correct?

**2.** Now when a developer looks at the codes, he observes that there are three tasks running concurrently -- is it correct?

---

<div class="post-metadata">

**Author:** ![bitherder\_57](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bitherder_57/32/1126810_2.png) [@bitherder\_57](https://forum.arduino.cc/u/bitherder_57)\
**Post date:** [July 2, 2024, 6:14pm UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516/11 "2024-07-02T18:14:31Z")

</div>

> [@GolamMostafa](#):
>
> I apologize to OP for hijacking the thread for a while.

Perhaps start a new thread with your new question.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 2, 2024, 6:32pm UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516/12 "2024-07-02T18:32:05Z")

</div>

Thank you for the advice.

I don't have enough queries so as to justify a new Thread. I have just wanted to know yes/no answer from the veteran poster on-the-fly.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 3, 2024, 5:52am UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516/13 "2024-07-03T05:52:42Z")

</div>

it all depends on how you define multi-tasking or the word process

In a general Operating System, a **process** is a program in execution, consisting of code, data, and resources. A **thread** is a subset of a process that can run independently, allowing for **multitasking within a single process** and we use **Tasks** (colloquially) to refer to the units of work that need to be executed, which can be processes or threads.

With this definition, tasks are scheduled by the OS based on their priority and timing requirements and are the individual units of work, processes represent the general program, threads enable concurrent execution within a process,.

But not everyone goes with that definition, in FreeRTOS, a task is the units of execution that can run independently and concurrently within the operating system. Each task in FreeRTOS has its own stack and program counter, similar to threads in other operating systems.

so you need to clarify the definitions of the words you use for "multi-tasking process"

In a nutshell, if you have one unit of work for each led to blink and let the OS schedule them, then you are colloquially multi-tasking or multi-threading or multi-processing.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 3, 2024, 6:10am UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516/14 "2024-07-03T06:10:11Z")

</div>

Plain and popular English for the conceptual subject. Your write-up has worked as a strong reinforcement and add-on for what I have already known on multi-tasking playing with FreeRTOS in Arduino UNO and ESP32 Platforms. I am adding your post #13 in my Class Lecture Sheets.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 30, 2024, 6:10am UTC](https://forum.arduino.cc/t/how-to-code-millis/1277516/15 "2024-12-30T06:10:56Z")

</div>

Closed on December 30, 2024, 6:10am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
