millis ( ) overflow ... a bad thing?

I have done a write-up on the vexed issue of millis() and micros() overflow.

Please read and advise of any typos, misleading information, etc.

Example of subtracting - recommended:
The subtraction "wrapped around" and gave us a large number.

It didn't wrap around - you subtracted a small number from a large number and you still had a large number. It would be a wrap around if millis() was less than 110,000. And why are you subtracting 60,000 anyway? It should be the start time you're subtracting.

2^32 - 50000 - 60000 = 4294857296 (0xFFFE5250)

That whole subtraction section doesn't read right to me.

I think you need to write it more like:

"The timer will roll over in 50 seconds. That is, millis() is umptyumpty. We make a note of that as our start time.

Now, a minute has gone by. millis() has rolled over, and is now reporting 10,000. So what do we get if we subtract our recorded start time from the current time? i.e., 10,000 - umptyumpty. Oh, it's blah blah."

Well explained.

Might consider mentioning you could have a unsigned variable overFlows.
Increment overFlows every time millis() resets.
Do this at the start of loop().

Therefore millis() resets every 49.71 days and overFlowsreset every 232 close to the age I am :wink:

majenko:

Example of subtracting - recommended:
The subtraction "wrapped around" and gave us a large number.

It didn't wrap around - you subtracted a small number from a large number and you still had a large number. It would be a wrap around if millis() was less than 110,000. And why are you subtracting 60,000 anyway? It should be the start time you're subtracting.

Thanks for looking at it.

You are right, I confused myself, it's no wonder others get confused. I've reworded the "subtracting" example part.

LarryD:
Therefore millis() resets every 49.71 days and overFlowsreset every 232 close to the age I am :wink:

I added a paragraph about that, however as I said, probably for that interval a proper clock would be better.

Bookmarked - Gammon on millis()

Added a graphic to show what happens to the subtraction:

Well now there is an idea whose time (in milliseconds) has certainly come! Good job, and thank you, Nick!

That reads much better now. Good explanation.