Go Down

### Topic: Millis overflow (Read 46869 times)previous topic - next topic

#### aabdelrahman_12

##### Sep 11, 2012, 10:00 pm
Please i would like to know does millis overflow (go back to zero), after approximately 50 days as i found here
http://arduino.cc/en/Reference/millis
or it resets after 9 hours and 32 minutes. as i found here
http://www.faludi.com/2007/12/18/arduino-millis-rollover-handling/

Thanks

#### AWOL

#1
##### Sep 11, 2012, 10:02 pm
49 days odd.
The other figure is a historical quirk, and as it says in the second thread, has been 49 days since IDE 0012
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#2
##### Sep 11, 2012, 10:49 pm
millis takes 49+_days to rollover (and so what if it does?) = 2^32mS * 1sec/1000mS * 1min/60sec * 1hr/60min * 1day/24 hrs
micros takes some number of hours to rollover = 2^32mS * 1sec/1000000uS * 1min/60sec * 1hr/60min * 1day/24 hrs

if all your time calculations are done as:
if  ((later_time - earlier_time ) >=duration ) {action}
then the rollover does generally not come into play.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#3
THanks so much

#### aabdelrahman_12

#4
##### Sep 16, 2012, 01:11 am
but i have another question is there a way to use miilis to calculate hrs of the day without problems

#### Jimmy60

#5
##### Sep 16, 2012, 01:41 am
By "calculate hrs of the day" do you mean tell the time?

#6
##### Sep 16, 2012, 01:41 am
Sure:

currentMillis = millis();
if ( (currentMillis - previousMillis) >= 1000){
previousMillis = previousMillis + 1000;
seconds = seconds +1;
if (seconds == 60){
seconds = 0;
minutes = minutes +1;
if(minutes == 60){
minutes = 0;
hours = hours +1;
if (hours == 24){
hours = 0;
} // end hrs check
} // end minutes check
} // end seconds check
} // end time check

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### PeterH

#7
##### Sep 16, 2012, 03:23 am

but i have another question is there a way to use miilis to calculate hrs of the day without problems

No, millis() tells you how long the sketch has been running, it does not tell you what the real-world time is. If you had an external source that you can get real world time from, for example a network interface or real time clock, then you could use millis() to extrapolate from that. However it doesn't directly tell you what the time is.

#### aabdelrahman_12

#8
##### Sep 16, 2012, 09:50 pm
I don't know how to thank you @crossroad

@ peter thanks .i know and my idea is based on set-up the current time using serial communication when my arduino start working

#### holmes4

#9
##### Sep 16, 2012, 10:16 pm
You could use the abs function which returns the +ve differance between the two numbers.

Eg

a=27;
b=30;

abs(a-b) and abs(b-a) will both give 3.

Mark

#### michael_x

#10
##### Sep 16, 2012, 10:45 pm
millis() returns an unsigned long.

Whenever you subtract an older time from a newer one, you get the correct unsigned result. No matter if there was an overflow.
You never need abs() for such calculations.

For ease of understanding think in bytes ( 0.. 255 ), instead of unsigned longs.
Example: If your old time was at 250 and now you're at 5 , you calculate (5 - 250) and interpret the result as an unsigned byte, the result is 11.
abs() might just make it worse (if you are in the wrong data types for intermediate values).
abs() of an unsigned value should not do any harm, but if you think it might help, you should rethink

#### Nick_Pyner

#11
##### May 08, 2013, 04:18 pm

if all your time calculations are done as:
if  ((later_time - earlier_time ) >=duration ) {action}

then the rollover does generally not come into play.

Thanks

(No harm in resurrecting this, I guess)

Are there instances where the rollover does come into play?

#### MarkT

#12
##### May 08, 2013, 04:24 pm

if all your time calculations are done as:
if  ((later_time - earlier_time ) >=duration ) {action}

then the rollover does generally not come into play.

Thanks

(No harm in resurrecting this, I guess)

Are there instances where the rollover does come into play?

When duration is >= the rollover period.  (Or if you use signed comparison, >= 1/2 the rollover period).
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### polymorph

#13
##### Sep 25, 2013, 04:40 pm
I found this link, it avoids the problem of rollover.

http://www.baldengineer.com/blog/2012/07/16/arduino-how-do-you-reset-millis/

Quote
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.  Instead of focusing on resetting millis(), here is how to use it correctly.
Avoiding rollover and checking how much time as passed is done in a single line:
if ((unsigned long)(millis() - waitUntil) >= interval)
That single line of code is all that is really needed, to avoid rollover!  Pretty simple, huh?  So let's go into more detail about how this works and what each of those variables do.
Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - https://tinyurl.com/Technote8

#14
##### Sep 25, 2013, 04:50 pm
Quote
if ((unsigned long)(millis() - waitUntil) >= interval)

Better I think to capture the time
Code: [Select]
`currentTime = millis();currentTime = micros();`
and then use that in your calculations.
This allows the same currentTime value to be used several times in blink without delay sketches:
Code: [Select]
`void loop():currentTime - micros();elapsedTime1 = currentTime - previousTime1;elapsedTime2 = currentTime - previousTime2;elapsedTime3 = currentTime - previousTime3;if (elapsedTime1 >= duration1){    previousTIme1 = previousTime1 + duration1 // set up for next time interval   // event 1 action(s)   }if (elapsedTime2 >= duration2){    previousTIme2 = previousTime2 + duration2 // set up for next time interval   // event 2 action(s)   }if (elapsedTime3 >= duration3){    previousTIme3 = previousTime3 + duration3 // set up for next time interval   // event 3 action(s)   }} // end loop[//code]`
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up