0

I'm developing a arduino based system, which includes a alarm system.

Searching on the Internet, I found these lybraries "Time.h" and "TimeAlarms.h".

In the requirements, it says: "Time does not require any special hardware. Internally, Time depends upon Arduino's millis() function to keep track to elasped time."

Considering Arduino's millis() rollover after 49.7 days, how can I ensure these lybraries are handling the rollover well?

I don't think that waiting 49.7 days to test it is the better way.

3
  • 1
    See this answer to a related question. Specifically, the section titled How do I test my sketch against the millis rollover. Commented Oct 4, 2016 at 13:27
  • 2
    Have you considered using a RTC? Commented Oct 4, 2016 at 13:31
  • As this is a duplicate of the question and answer Edgar Bonet linked to I'm going to close this question.
    – Nick Gammon
    Commented Oct 4, 2016 at 20:13

3 Answers 3

2

As long as the date is before 2038, and your code calls one of the time functions at least once every 49 days, rollover won't affect you.

Internally the library uses Unix epoch time. Every time you call a function it checks to see if 1 or more seconds have past since the last time it was called and then updates the epoch time accordingly. Only for this check it uses millis(). And in only checks the difference between the last call's millis-value and the current millis-value, and rollover has no effect on time differences (as long as they are less that 49 days).

1

You should simply buy a real time clock such as the common DS3231. They work well and don't have any problems with resting or continuous data reading. It also dictates the date!

Hope I helped :)

1

I have a lengthy post about this.

To avoid going to the link if you don't want to, the simple answer is to subtract when calculating time intervals, eg.

unsigned long startTime = millis ();
unsigned long interval = 60000;

...

if (millis () - startTime >= interval)
  {
  // do something
  }

This always works! - even after 49.7 days - because of the way that unsigned arithmetic works.

The only time it would fail is if you needed to time an interval of over 49.7 days - in which case a real-time clock is an obvious answer.

Another example:

startedFeedingFish = millis ();

...

if (millis () - startedFeedingFish >= 20000)  // feed them for 20 seconds
  {
  // stop feeding the fish
  }

Not the answer you're looking for? Browse other questions tagged or ask your own question.