Go Down

### Topic: Handling millis rollover (Read 1 time)previous topic - next topic

#### AJ Weber

##### Apr 13, 2012, 03:02 pmLast Edit: Apr 13, 2012, 03:57 pm by AWOL Reason: 1
I don't wish to "hijack" this thread, but since it was brought-up in a reply, and I have the question...

Does anyone have a tested function or methodology to work with the millis() - total elapsed time since reset - as it rolls-over the max value (every 50 days or so is what the Reference material says)?

Specifically, I have one "test" that I need to conduct to see if something happens WEEKLY, so it's very possible, if the unit is running a decent amount of time, that I would record a value close to ULONG_MAX, then during the following days, the value would be significantly less; messing-up my delta-calculation.

I'm assuming others have already devised a method or strawman to get-around this limitation and was just looking to "not reinvent the wheel".

Moderator edit: Topic split and renamed.
AJ

#### Morris Dovey

#1
##### Apr 13, 2012, 03:42 pm
I've extended both the millis() and micros() functions to 64 bits and posted an example in one of the files attached to my post at http://arduino.cc/forum/index.php/topic,93959.msg705701.html#msg705701
There's always a better way!

#2
##### Apr 13, 2012, 05:01 pm
Make sure your calculations all use unsigned long variables (32bit, 0 to FFFFFFFF)
and the math is always currentmilis - previous millis.

Then even at rollover, if currentmillis = 0x00001000, and older (previous) is 0xFFFFFFF8 for instance, a correct result is returned:
0x00001000 - 0xFFFFFFF8 = 0x00001008
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.

#### Jack Christensen

#3
##### Apr 13, 2012, 09:18 pm

Make sure your calculations all use unsigned long variables (32bit, 0 to FFFFFFFF)
and the math is always currentmilis - previous millis.

Then even at rollover, if currentmillis = 0x00001000, and older (previous) is 0xFFFFFFF8 for instance, a correct result is returned:
0x00001000 - 0xFFFFFFF8 = 0x00001008

Nominated for a sticky topic!

Has there even been any thought given to a FAQ forum section that could be posted to perhaps only by moderators after some sort of consensus process? Or maybe it'd be better as a section in the Playground?

#4
##### Apr 13, 2012, 09:31 pm
Probably Jack - getting folks to read it before asking is the tricky bit.
Often folks don't even know what it is they should be looking for.
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.

#### GoForSmoke

#5
##### Apr 13, 2012, 09:41 pm
Still happier just knowing.
Nick Gammon on multitasking Arduinos:
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### n1spx

#6
##### Apr 13, 2012, 10:21 pm

Probably Jack - getting folks to read it before asking is the tricky bit.
Often folks don't even know what it is they should be looking for.

It would at least make it easier for the people replying to find the link they need to point at

#7
##### Apr 13, 2012, 10:42 pm
You haven't seen the blink without delay rewrite thread, went on for many pages. I suppose it ended up with a nicer page when all done, but took quite a bit of discussion to get there.
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.

#### Jack Christensen

#8
##### Apr 13, 2012, 11:08 pm

Probably Jack - getting folks to read it before asking is the tricky bit.
Often folks don't even know what it is they should be looking for.

It would at least make it easier for the people replying to find the link they need to point at

True, and true!

A central resource page on a given topic would also benefit from various folks reviewing it, and so hopefully be clearer, more complete, etc. Often I'll dash off a reply and maybe it's not as good as it could have been had I engaged the brain a bit more

Go Up

Please enter a valid email to subscribe