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 › international › español › software
Reiniciar Millis() - Software - Arduino Forum
February 27, 2019 - Existe alguna forma, aparte de resetear el micro , de reinicializar el valor de millis() ? Por software digo. Gracias
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

Reset millis();
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 again over from 0; Wiring in attachment. Thank you very much. More on forum.arduino.cc
🌐 forum.arduino.cc
5
0
May 28, 2020
arduino - How to reset a millis() variable back to zero - Stack Overflow
Your code is not working because the millis is updating the sec variable. 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
How to reset millis()
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 ... More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
February 27, 2019
🌐
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.
🌐
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.
🌐
YouTube
youtube.com › watch
How to reset the mills() counter in Arduino, why not to and what to do instead (in under 2 minutes) - YouTube
In this video I will demonstrate two different ways to reset the value of the mills() counter that counts milliseconds from the start of Arduino and a librar...
Published   August 18, 2016
🌐
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…
🌐
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
Find elsewhere
🌐
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…
🌐
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...
🌐
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 › 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 › international › español › software
REINICIAR MILLIS CADA CAMBIO DE ESTADO - Software - Arduino Forum
October 10, 2015 - Buen día colegas! Estoy haciendo un proyecto donde por medio de una perilla de 4 posiciones, pretendo controlar el encendido y tiempo de encendido de una salida la cual llamé "reac". Las condiciones de la perilla son: 1…
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Resetting millis - Programming - Arduino Forum
January 20, 2024 - My little code got massive with a lot of help from this forum. Thanks everyone that helps out. My code turns on and off relays that control dew point and temp. It now runs one setting for four days then changes the dew…
🌐
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.
🌐
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 - Right now in your code currentMillis ... once in setup. Set currentMillis when you want to start your 5 second timer, when the user presses the button see if millis() - currentMillis <= 5000 then do your stuff....
🌐
Arduino Forum
forum.arduino.cc › projects › general guidance
Resetting millis() Counter - Page 2 - General Guidance - Arduino Forum
November 20, 2018 - 123Splat: Well Perry, since you want to learn ways to reset 'millis()', as I recall, there is a little button on most of the Arduino boards called 'Reset'. Removing power also works. LMAO! 🙂
🌐
Arduino Forum
forum.arduino.cc › international › deutsch
reset des millis() Wertes - Deutsch - Arduino Forum
January 4, 2012 - Nach einigen (mehr oder weniger) sinnvollen Kommentaren, will ich nun auch mal eine Frage in die Runde werfen. Es geht um den Null-Durchlauf des "millis()" Wertes. Da ich in einem Projekt mit einem Unix Timestamp arbeite, kann ich recht einfach mit dem Wert den millis() liefert rechnen.
🌐
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...