millis() and micros() overflow periodically. However, this is not a problem: as long as you compare durations instead of timestamps you can forget about the overflows.

The liked answer also gives the trick for resetting millis(). Can be handy for testing purposes, but you do not need this to handle the millis() rollover problem. Notice that there is no clean way to reset micros(): it relies on a static variable, i.e. a variable private to the file defining it.

Answer from Edgar Bonet on Stack Exchange
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Reset millis(); - Programming - Arduino Forum
May 28, 2020 - Good morning, I use a push button... I'd like if we press the first the millis() counter starts. If I press a second time reset millis ( millis() counter becomes = 0 ) If I press it a third time, millis() starts agai…
Top answer
1 of 3
6

millis() and micros() overflow periodically. However, this is not a problem: as long as you compare durations instead of timestamps you can forget about the overflows.

The liked answer also gives the trick for resetting millis(). Can be handy for testing purposes, but you do not need this to handle the millis() rollover problem. Notice that there is no clean way to reset micros(): it relies on a static variable, i.e. a variable private to the file defining it.

2 of 3
5

Overflow is never really an issue if you always calculate time difference. (Unless the time difference is more that 50 days.)

unsigned long previousTime = millis();
... wait for some event to happen ...
unsigned long elapsedTime =  millis() - previousTime;

Even if previousTime was before the overflow, and millis() is after the overflow (so essentially millis()<previousTime), elapsedTime will still be the correct value.

This is because the calculation itself will also overflow. Since millis()<previousTime, the result would be negative, but the type of the variable is unsigned, so it wraps around. I hope that makes sense.

Just don't ever do something like if( millis() > (previousTime+1000) )

If you still want to reset millis, you can use the following:

extern volatile unsigned long timer0_millis;
unsigned long new_value = 0;

void setup(){
  //Setup stuff
}

void loop(){
  //Do stuff
  //--------

  //Change Millis
  setMillis(new_value);
}

void setMillis(unsigned long new_millis){
  uint8_t oldSREG = SREG;
  cli();
  timer0_millis = new_millis;
  SREG = oldSREG;
}
Discussions

arduino - How to reset a millis() variable back to zero - Stack Overflow
After the sec reaches the 59 value, your if statement changes the sec to 0. However the millis will again put the latest time value i.e. 60 and lcd.print(sec) will print it. And the cycles continues, without ever resetting the sec value because millis is updating it. More on stackoverflow.com
🌐 stackoverflow.com
Resetting Millis() to zero, reset clock
This topic is a little summary of the research I did this morning on the unsigned long millis(). If it doesn't add any existing knowledge, then let the post be for reference purposes only. Millis() function itself Unsigned long 32bit variable. Meaning 2^32-1 milliseconds range (no negative ... More on forum.arduino.cc
🌐 forum.arduino.cc
19
1
August 26, 2013
Problem in Resetting millis() back to 0
Hi! Beginner here so pls bear with me. So Im having a hardtime adding a code that puts the states(i.e., Case 2 , Case 3 and Case 4) back to accessory… More on reddit.com
🌐 r/arduino
9
1
March 18, 2021
resetting millis()
Hi I want to reset the millis() function to zero each 24 hours. I cant find a post in the forum that describes this, is there anyone that can help me? best regards Fredrik More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
October 11, 2016
🌐
Bald Engineer
baldengineer.com › home › arduino: how do you reset millis() ?
Arduino: How do you reset millis() ? - Bald Engineer
March 31, 2022 - The quick answer to “How do you reset millis()” is: You Don’t! And here’s why: if you did, it would potentially break most libraries and functions that rely on it. Generally the reason people want to reset it, is that they are concerned about rollover.
🌐
Arduino Forum
forum.arduino.cc › community › general discussion
Resetting Millis() to zero, reset clock - General Discussion - Arduino Forum
August 26, 2013 - This topic is a little summary of the research I did this morning on the unsigned long millis(). If it doesn't add any existing knowledge, then let the post be for reference purposes only. Millis() function itself Unsi…
🌐
Reddit
reddit.com › r/arduino › problem in resetting millis() back to 0
r/arduino on Reddit: Problem in Resetting millis() back to 0
March 18, 2021 - You don't reset millis(). What you do is capture and save the value from into a variable.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
resetting millis() - Programming - Arduino Forum
October 11, 2016 - Hi I want to reset the millis() function to zero each 24 hours. I cant find a post in the forum that describes this, is there anyone that can help me? best regards Fredrik
Find elsewhere
🌐
Arduino Forum
forum.arduino.cc › projects › programming
How to reset millis() - Programming - Arduino Forum
February 27, 2019 - Hi I need help please, I need to reset the period in order to restart the code #include // Declaramos la variable para controlar el servo Servo servoMotor; unsigned long startMillis; //some global variables available anywhere in the program unsigned long currentMillis; const unsigned long period = 60000; void setup() { // Iniciamos el monitor serie para mostrar el resultado Serial.begin(9600); // Iniciamos el servo para que empiece a trabajar con el pin 9 servoMotor.attac...
🌐
Tom Blanchard
tomblanch.wordpress.com › 2013 › 07 › 27 › resetting_millis
Resetting the Arduino millis() count | Tom Blanchard
September 9, 2019 - Millis uses Timer0 which is configured to “tick” every 64 clock cycles. Every time it ticks, the register containing the number of times Timer0 has ticked is incremented. Since the register is only 1 byte, after 256 ticks the register overflows. When this happens, an interrupt is triggered and the associated interrupt handling code is run. To find out how this is implemented, you need to look in wiring.c which is found in the arduino folder “hardware/arduino/cores/arduino” or thereabouts.
🌐
YouTube
youtube.com › watch
Arduino Millis( ) Function | Reset to Zero(0) | Best Video - YouTube
In this video you'll learn about how to reset millis() function of arduino......................................................................................
Published   March 25, 2020
🌐
Arduino Forum
forum.arduino.cc › projects › programming
How can I Reset millis()? - Programming - Arduino Forum
July 9, 2019 - I am a beginer for programing language so I tried to write a program count 7segment 3digit in STM32 and I want to use the 7 segment to count the time but I don't know how to make the 7 segment start when I push the button 32 and reset the millis when I use push the button 32 again here is my code #define pinA 6 #define pinB 10 #define pinC 14 #define pinD 12 #define pinE 11 #define pinF 7 #define pinG 13 #define DG1 5 #define DG2 8 #define DG3 9 #define DG4 4 #define But 32 int Timedelay =...
🌐
Arduino Forum
forum.arduino.cc › projects › general guidance
Reset millis when button is not pressed - General Guidance - Arduino Forum
May 14, 2020 - Hey, Snoop the noob dev here. Ive read many topics and im aware that reseting millis() is a bad option. However im trying to make a program that starts a timer when the button is pressed and reset the timer when its not pressed. Example: Holding button* if (time_now == 1000) { do something } if (time_now == 2000) { do something } if (time_now == 4000) { do something } My code: const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the num...
🌐
Arduino Forum
forum.arduino.cc › other hardware › device hacking
Reset Arduino millis() and micros() - Device Hacking - Arduino Forum
September 20, 2013 - I chose to post this in the hacking ... the source code for millis() and friends. I've been searching high and low on the Internet on how to reset millis() on Arduino, and I came up empty.......
🌐
Arduino Forum
forum.arduino.cc › projects › general guidance
Resetting millis() Counter - General Guidance - Arduino Forum
November 20, 2018 - Is there a way to rest the millis() counter? Or is there another background timer? I have a project in the back of my mind for someday, that turns off a lamp an hour after it was last touched (and incorporates touch on/off and dimming.) This would sit running its code for months at a time, so millis() will overflow and the controller could, through foreseeable bad luck, run afoul of the overflow and fail.
🌐
EEVblog
eevblog.com › forum › microcontrollers › i-made-a-video-on-resetting-millis()-in-arduino
How to reset millis() in Arduino, why not to do it, and what ...
EEVblog Captcha · We have seen a lot of robot like traffic coming from your IP range, please confirm you're not a robot · This security check has been powered by · CrowdSec
🌐
Arduino Forum
forum.arduino.cc › projects › general guidance
Solving the millis() reset problem - General Guidance - Arduino Forum
July 22, 2015 - So this issue came up on my other project thread and I would like a good answer if I can find one. I started out with code that would fail when millis() resets automatically at about 50 days. I have added the code (first if statement) below. I believe this will correct the issue.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Reset millis(); - #5 by alto777 - Programming - Arduino Forum
May 29, 2020 - aarg: It is not possible to change the value of millis(). Of course it is possible. No need to lie about it, it's not like a secret or anything. However, I do agree generally that it is unnecessary.
🌐
Arduino Forum
forum.arduino.cc › projects › general guidance
How do i reset Millis to 0? - General Guidance - Arduino Forum
January 18, 2012 - Hi i did a little searching and all i could find is: timer0_overflow_count = 0; This does not work in my code i get errors. I need my code to run for 90days min and i have read millies will overflow after 49 days> this will crash my code. My code is below and the millisecond have been lowered ...
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Reset millis - #7 by Robin2 - Programming - Arduino Forum
July 8, 2019 - Hello, I have created this program and I would like to reset the time to zero each time the loop is complete 'Case 1'. Thanks, Thomas. #include <Servo.h> Servo s2; int BUTTON = 11; int COUNT = 1; int buzzerPin = 8; i…