# Running in background timer

**URL:** <https://forum.arduino.cc/t/running-in-background-timer/1201205>\
**Category:** Programming\
**Created:** [December 17, 2023, 7:30am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205 "2023-12-17T07:30:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon65951220](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@anon65951220](https://forum.arduino.cc/u/anon65951220)\
**Post date:** [December 17, 2023, 7:30am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/1 "2023-12-17T07:30:46Z")

</div>

Hi there.

I've seen this type of feature for micro: bit. Just wondering if Arduino has it.

Feature: Get the number of milliseconds of time passed since the board was turned on.

I want to make a timer for the project.

Thanks.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 17, 2023, 7:32am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/2 "2023-12-17T07:32:33Z")

</div>

Take a look at the millis() and micros() functions

---

<div class="post-metadata">

**Author:** ![anon65951220](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@anon65951220](https://forum.arduino.cc/u/anon65951220)\
**Post date:** [December 17, 2023, 7:34am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/3 "2023-12-17T07:34:48Z")

</div>

Thanks for the immediate response. Do I need to add any extension for this?

---

<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:** [December 17, 2023, 7:35am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/4 "2023-12-17T07:35:10Z")

</div>

Take a look at the IDE's BlinkWithoutDelay example to gain the knowledge.

---

<div class="post-metadata">

**Author:** ![anon65951220](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@anon65951220](https://forum.arduino.cc/u/anon65951220)\
**Post date:** [December 17, 2023, 7:35am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/5 "2023-12-17T07:35:46Z")

</div>

Under what section?

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [December 17, 2023, 7:38am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/6 "2023-12-17T07:38:42Z")

</div>

> [@anon65951220](#):
>
> Do I need to add any extension for this?

No

> [@anon65951220](#):
>
> Under what section?

Examples menu

These are incredibly basic questions, have you done some arduino beginner tutorials yet? (No, I can't recommend any, perhaps other forum members who were beginners more recently, or have contributed to or written tutorials themselves, can suggest)

---

<div class="post-metadata">

**Author:** ![anon65951220](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@anon65951220](https://forum.arduino.cc/u/anon65951220)\
**Post date:** [December 17, 2023, 7:44am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/7 "2023-12-17T07:44:37Z")

</div>

Ok, thanks. BTW, I'm just a beginner.

---

<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 17, 2023, 7:56am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/8 "2023-12-17T07:56:58Z")

</div>

Here is my personal variant that reduces the number of lines of code to write a timer  
including an explanation how it works based on an everyday example

> [@Example-code for timing based on millis() easier to understand through the use of example-numbers / avoiding delay()](https://forum.arduino.cc/t/example-code-for-timing-based-on-millis-easier-to-understand-through-the-use-of-example-numbers-avoiding-delay/974017/1):
>
> non-blocking timing. Execute code only from time to time. There are a lot of different ways to learn programming. This thread wants to add another approach that is different to the yet existing ones. UPDATE 06.01.2023 if you are mainly interested in applying non-blocking timing you can do a quick read of this short tutorial / demonstration If you are intersted in understanding the details how it works go on reading here. I use an everyday analogon in this post to explain the basic principle …

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [December 17, 2023, 10:30am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/9 "2023-12-17T10:30:33Z")

</div>

> [@anon65951220](#):
>
> I want to make a timer for the project.

i just post this in another thread. the OP is working on an automatic bird feeder

```arduino
# include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd (0x27, 16, 2);

const byte On = LOW;
const byte Off = HIGH;

const byte IndicatorPin = 10;
int indicatorSec;

unsigned long MsecSec = 100; // set low for testing 1000
unsigned long msecLast;

const long TimerSec = 10 * 60;
      long cntSec = TimerSec;

char s [90];

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    if (msec - msecLast >= MsecSec) {
        msecLast += MsecSec;

        int hour = cntSec / 3600;
        int mins = (cntSec % 3600) / 60;
        int secs = (cntSec % 60);

        sprintf (s, "%2d:%02d:%02d\n", hour, mins, secs);
        lcd.print (s);

        cntSec--;
        if (0 > cntSec) {
            cntSec = TimerSec;
            indicatorSec = 2;
            digitalWrite (IndicatorPin, On);
        }
        else if (0 == --indicatorSec)
            digitalWrite (IndicatorPin, Off);
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    pinMode (IndicatorPin, OUTPUT);
    digitalWrite (IndicatorPin, Off);

    lcd.init ();
    lcd.backlight ();
    lcd.setCursor (4, 0);
}

```

---

<div class="post-metadata">

**Author:** ![anon65951220](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@anon65951220](https://forum.arduino.cc/u/anon65951220)\
**Post date:** [December 18, 2023, 9:06am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/10 "2023-12-18T09:06:18Z")

</div>

Okay, that's nice @gcjr! I will try it out later.

---

<div class="post-metadata">

**Author:** ![odometer](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/odometer/32/1013044_2.png) [@odometer](https://forum.arduino.cc/u/odometer)\
**Post date:** [December 18, 2023, 1:21pm UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/11 "2023-12-18T13:21:42Z")

</div>

Just beware of the limitations of Arduino/C++ arithmetic.

> [@gcjr](#):
>
> `const long TimerSec = 10 * 60;`

Perhaps better practice is to make this be  
`const long TimerSec = 10UL * 60UL;`

That way, if you change it to be (say) 24 hours, you can just do this  
`const long TimerSec = 24UL * 60UL * 60UL;`

and you will not run into the bug that would be created if you do this instead  
`const long TimerSec = 24 * 60 * 60;`  
because if you leave off the `UL` suffix, the arithmetic will overflow at 32767, and last time I checked, there were 86400 seconds in a day.

Also, be aware that `millis()` overflows and resets to zero after about 49 or 50 days, because an `unsigned long` can only count up to 4,294,967,295.

> **[millis() - Arduino Reference](https://www.arduino.cc/reference/en/language/functions/time/millis/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

---

<div class="post-metadata">

**Author:** ![anon65951220](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@anon65951220](https://forum.arduino.cc/u/anon65951220)\
**Post date:** [December 19, 2023, 3:38am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/12 "2023-12-19T03:38:54Z")

</div>

Okay. What if I don't want to use `millis()`? Perhaps like seconds? Or minutes?

---

<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 19, 2023, 5:13am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/14 "2023-12-19T05:13:30Z")

</div>

> [@anon65951220](#):
>
> Okay. What if I don't want to use `millis()`? Perhaps like seconds? Or minutes?

millis() counts up to 2^32 - 1 = 4294967295 milliseconds

4294967295 milliseconds are 4294967 seconds

4294967 seconds are 71582.79 minutes

71582.79 minutes are 1193 hours

1193 hours are 49.7 days

millis() can count up and up for 49.7 **days**

This should be long enough for almost any application

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [December 19, 2023, 5:39am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/15 "2023-12-19T05:39:45Z")

</div>

> [@anon65951220](#):
>
> What if I don't want to use `millis()`? Perhaps like seconds? Or minutes?

```arduino
uint32_t seconds = millis() / 1000UL;
uint32_t minutes = millis() / (1000UL*60);

```

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [December 19, 2023, 5:52am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/16 "2023-12-19T05:52:18Z")

</div>

The official BWD uses mixed type math. Time variables should at least all be unsigned and best all the same length.

---

<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:** [December 19, 2023, 5:59am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/17 "2023-12-19T05:59:22Z")

</div>

> [@anon65951220](#):
>
> What if I don't want to use `millis()`? Perhaps like seconds? Or minutes?

> [@anon65951220](#):
>
> Get the number of milliseconds of time passed since the board was turned on.

You asked for _milliseconds_ of time passed. Why have you chnaged the mind and now asking for _seconds_ or _minutes_?

Do you have an Arduino UNO Board? If yes, then you may learn the operation/application of the _millis()_ function knowing its following definition. The function is widely used in UNO Platform for concurrent execution of many tasks.

When the uploading of a sketch is done in the Ardduino UNO, a 32-bit conuter (composed of four consecutive memory locations, which I call _millisCounter_) begins accmulating evevry millisecond passed on interrupt basis. You can use the _millis()_ function at any time to read the content of the _millisCounter_ on the fly (without disturbing the counting process) and save in an _unsigned long_ type variable for later use.

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [December 19, 2023, 6:09am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/18 "2023-12-19T06:09:29Z")

</div>

const long is a signed integer.

const unsigned long MillisPerSec = 1000UL;  
const unsigned long MillisPerMin = 60UL \* MillisPerSec;  
const unsigned long MillisPerHr = 60UL \* MillisPerMin;  
const unsigned long MillisPerDay = 24UL \* MillisPerHr;

OP, with unsigned math this formula works across rollover, which ceases to be a problem. This works

elapsed time = end time - start time

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [December 19, 2023, 6:15am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/19 "2023-12-19T06:15:10Z")

</div>

With the caveat that due to the way millis is counted, the value is always +/-1.

When close timing really counts, use micros() and be aware that the micros clock 'ticks' every 4 microseconds, not every 1. Is close!

---

<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:** [December 19, 2023, 6:28am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/20 "2023-12-19T06:28:27Z")

</div>

The _micros()_ function rollovers after about 70 minutes; whereas, _millis()_ function rollovers after about 49+ days.

The _micros()_ function provides more accurate timing than the _millis()_ function. However, the majority of Arduino people are found to use _millis()_ function, probably, to avoid unnecessary frequent interrupts.

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [December 19, 2023, 6:55am UTC](https://forum.arduino.cc/t/running-in-background-timer/1201205/21 "2023-12-19T06:55:53Z")

</div>

Using micros() does not generate extra interrupts.  
Without the micros counter, millis() can't run.

Yup, using 32-bit micros the longest interval that can be measure is a bit over 71 minutes. Most people don't delay as long as a minute!

If close timing matters, use micros(). Mostly, it doesn't.  
Millis is fine for most uses.

[Next page](https://forum.arduino.cc/t/running-in-background-timer/1201205.md?page=2)
