Go Down

Topic: Handling millis rollover (Read 938 times) previous topic - next topic

AJ Weber

Apr 13, 2012, 03:02 pm Last 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".

Thanks in advance,

Moderator edit: Topic split and renamed.
AJ

Morris Dovey

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!

CrossRoads

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


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?
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

CrossRoads

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

Still happier just knowing.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

n1spx


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 ;)

CrossRoads

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



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 :smiley-red:
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Go Up