Pages: [1]   Go Down
Author Topic: Handling millis rollover  (Read 845 times)
0 Members and 1 Guest are viewing this topic.
USA
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't wish to "hijack" this thread, but since it was brought-up in a reply, and I have the question... smiley

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
« Last Edit: April 13, 2012, 08:57:02 am by AWOL » Logged

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

There's always a better way!

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 27363
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4092
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 27363
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Still happier just knowing.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 27363
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4092
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink

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
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Pages: [1]   Go Up
Jump to: