# Millis() reset?

**URL:** <https://community.particle.io/t/millis-reset/68501>\
**Category:** Firmware\
**Tags:** boron\
**Created:** [October 20, 2024, 11:12am UTC](https://community.particle.io/t/millis-reset/68501 "2024-10-20T11:12:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![blshaw45](https://avatars.discourse-cdn.com/v4/letter/b/9f8e36/32.png) [@blshaw45](https://community.particle.io/u/blshaw45)\
**Post date:** [October 20, 2024, 11:12am UTC](https://community.particle.io/t/millis-reset/68501/1 "2024-10-20T11:12:42Z")

</div>

Quick question: does millis() reset when flashing a firmware update?

I have a routine that is supposed to execute every 24 hours under certain circumstances but it doesn't. I do a lot of firmware tweaks and suspect that that may be the cause. If it does reset with every flash, would I be correct that using System.millis() would solve the problem?

Also, the documentation suggests that System.millis() doesn't reset even after a power outage and subequent reboot; is that correct?

unsigned long lastTankUpdate = millis();

## if ( millis() - lastTankUpdate \> 86400000){ // If it's been more than 24 hours since last message from tank

- 
-

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [October 20, 2024, 12:21pm UTC](https://community.particle.io/t/millis-reset/68501/2 "2024-10-20T12:21:13Z")

</div>

That code ~~will work~~ will not work (see below). The millis counter resets on every reset, including reset button, System.reset, OTA reset, and wake from hibernate sleep.

---

<div class="post-metadata">

**Author:** ![blshaw45](https://avatars.discourse-cdn.com/v4/letter/b/9f8e36/32.png) [@blshaw45](https://community.particle.io/u/blshaw45)\
**Post date:** [October 20, 2024, 12:44pm UTC](https://community.particle.io/t/millis-reset/68501/3 "2024-10-20T12:44:29Z")

</div>

Confused Rick. The sample code I included in my post is what I have in production but which does not execute. Logic suggests that it doesn't execute because millis() gets reset somehow before the 24-hour period expires. Nowhere in my code do I reset the 'lastTankUpdate' variable except in the aforementuoned routine that never executes. So the implication is that my frequent flashing of modified code causes millis() to reset.

When you say "The millis counter resets on every reset...", are you saying that flashing new code qualifies as a 'reset'. If so, that's clearly my issue.

It's not yet clear to me that System.reset behaves differently in that regard.

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [October 20, 2024, 12:46pm UTC](https://community.particle.io/t/millis-reset/68501/4 "2024-10-20T12:46:57Z")

</div>

Sorry, I misread your initial post. That code will not work, because there are a large number of things that reset millis. You should use the actual Unix date from Time.now().

---

<div class="post-metadata">

**Author:** ![blshaw45](https://avatars.discourse-cdn.com/v4/letter/b/9f8e36/32.png) [@blshaw45](https://community.particle.io/u/blshaw45)\
**Post date:** [October 20, 2024, 12:50pm UTC](https://community.particle.io/t/millis-reset/68501/5 "2024-10-20T12:50:46Z")

</div>

Perfect!! I'll do that!

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [October 20, 2024, 12:53pm UTC](https://community.particle.io/t/millis-reset/68501/6 "2024-10-20T12:53:30Z")

</div>

Make sure that the time has been set using Time.isValid() before using it. It will be set a few seconds after connecting to the cloud.

---

<div class="post-metadata">

**Author:** ![blshaw45](https://avatars.discourse-cdn.com/v4/letter/b/9f8e36/32.png) [@blshaw45](https://community.particle.io/u/blshaw45)\
**Post date:** [October 20, 2024, 1:00pm UTC](https://community.particle.io/t/millis-reset/68501/7 "2024-10-20T13:00:54Z")

</div>

Time.now() is in milliseconds, right? So this is the modification.

if ( _Time.now_() - lastTankUpdate \> 86400000){ // If it's been more than 24 hours since last message from tank

Where does the Time.isValid() go, in setup()?

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [October 20, 2024, 1:11pm UTC](https://community.particle.io/t/millis-reset/68501/8 "2024-10-20T13:11:16Z")

</div>

Time.now() is in seconds since January 1, 1970, like Unix.

It's not in milliseconds like millis() or Javascript getTime().

When the system first boots the time won't be valid because the RTC won't be set yet. Before you rely on it, check Time.isValid(). You usually do this before you check to see if it's time to do something.

---

<div class="post-metadata">

**Author:** ![blshaw45](https://avatars.discourse-cdn.com/v4/letter/b/9f8e36/32.png) [@blshaw45](https://community.particle.io/u/blshaw45)\
**Post date:** [October 20, 2024, 1:12pm UTC](https://community.particle.io/t/millis-reset/68501/9 "2024-10-20T13:12:26Z")

</div>

Got it. But why are you working on a Sunday? Glad you are though!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/6/4/64635adccc35238caa19e9b553ec3e98b163d026.png) [@system](https://community.particle.io/u/system)\
**Post date:** [April 21, 2025, 1:12am UTC](https://community.particle.io/t/millis-reset/68501/10 "2025-04-21T01:12:56Z")

</div>

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