Using millis over long time

Hello,

I have read the topic about how to use millis, but I wonder what happen in the code if the controller has been running for many days.

The reference say that the returned value from millis() go back to zero after about 50 days. Will this cause problem? If yes, how to avoid it?

It will cause problems if you do not do the math in the right order. When checking for elapsed time always use the construct "millis() - lastTimeChecked >= elapsed time". This will make sure that the point at which millis() has rolled over and lastTimeChecked was before the rollover is worked out properly in the arithmetic.

There are lots of times this question has been asked so for a more technical description, search for those previous answers.

Check this out.


I have a 3V ProMini with NRF905 radio that I built 5 years ago, uses millis timer to transmit temperature and pump status every 15 seconds from my well house 25 meters away. Only time it stops is loss of mains power. I know that millis() has rolled over several dozen times without fault.

The reference say that the returned value from millis() go back to zero after about 50 days. Will this cause problem? If yes, how to avoid it?

When you read the topic on using millis() for timing did you miss this part ?

Using unsigned variables and subtraction for the elapsed period the comparison will work even if/when millis() rolls over to zero when the program is running. This is not the place to have a diversion into the reasons why this works but trust me, it does.

Will this cause problem?

The other thing to consider is would it cause a problem in your particular application if once every 50 days or so one timing event was skipped? Only you can answer that.

There is, of course, a real problem with using miilis() for timing long periods and that is one of the accuracy of the timing. Let's assume that the Arduino oscillator is 99.9% accurate and that we are timing a period of say 7 days ie about 10,000 minutes. 0.1% of 10,000 is 10, so the timing could be out by 10 hours in a week which is probably not acceptable

There is no doubt that for accurate time of long periods then it is not appropriate to use millis() and that an RTC is a better alternative

Thanks for the reply. I guess in all the text about how to use millis, I did miss an important part :wink:

About RTC. I have tested several and they all miss a few seconds during 24 hours. But I see that the RTCLib has a function which can be used to adjust this (adjustDrift).

I only use DS3231 RTCs here, these are good for +- a few seconds in a month.

So the DS1307 is less accurate in general?

The 1307 a very poor RTC :frowning:

UKHeliBob:
There is, of course, a real problem with using miilis() for timing long periods and that is one of the accuracy of the timing. Let’s assume that the Arduino oscillator is 99.9% accurate and that we are timing a period of say 7 days ie about 10,000 minutes. 0.1% of 10,000 is 10, so the timing could be out by 10 hours in a week which is probably not acceptable

?

Don’t tell me that my calculator has made a mistake :slight_smile:

Old age :wink:

While millis() in itself has limitations, over many days it’s unlikely your timing needs to be millisecond accurate.

Initialise an maintain a counter that uses millis() to count minutes or seconds.
In the long term, that will be independent of the millis rollover, so you’ll still have the long term counter/timer capability.

However as pointed out, for very long term timing, you need to find another time source (RTC, NTP, cellular etc)

I had a board set up that was running for 3 or 4 years. Im an amateur at best when it comes to programming, but I enabled the watchdog function and was using the millis to make decisions on when to call functions, and used an RTC. At a certain hours/minutes/seconds on the first day of each month I would force a delay so the watchdog function reset the board.

I dont know if thats the best option but it never allowed the variables to get 'full' and the board always functioned properly. So it worked for me. But I dont know if its the right way to handle it or if it was even required....