Timed events and millis()

Thank you fellow programers for stopping by to read my post.

As I may have mentioned before I have a vision for computerising my fish tank. Some of these items will run using real time clock but some will use the millis() command. however i have read that after 50 days or so millis() overflows. Is it possible to reset millis() before overflow occurs ie on a daily basis?

All explained here: if you do the arithmetic the right way you'll be ok.

It is all in the math. Done correctly using subtraction overflow is calculated correctly.

I have been using these two timer as my BWD timers:

  static unsigned long ECounter;
  static unsigned long ACounter;
  unsigned long t = 1000 // one second

  static unsigned long ExactTimer;
  if ((millis() - ExactTimer) >= (t)) {
    ExactTimer += (t);
    ECounter++
  }

  static unsigned long AfterTimer;
  if ((millis() - AfterTimer) >= (t)) {
    AfterTimer = millis();
    ACounter++
  }

Running this in a loop you will see the exact timer holds true to the millis count but the after timer will loose time and when either rolls over the subtraction math will prevent the error.

Now why use the after timer? When any delay occurs that exceeds the time interval the exact timer will trigger rapidly to catch up. The after timer is immune to this.

Z

Your watch overflows twice a day. When was the last time that caused you a problem?

jon_a:
Thank you fellow programers for stopping by to read my post.

As I may have mentioned before I have a vision for computerising my fish tank. Some of these items will run using real time clock but some will use the millis() command. however i have read that after 50 days or so millis() overflows. Is it possible to reset millis() before overflow occurs ie on a daily basis?

You might be misunderstanding what "overflow" means. "Resetting" (presumably making it start from 0 again) is exactly the same thing as what happens when it overflows, so however you would handle timing during the reset is exactly how you handle it to prevent reset from being a problem.

In other words, use subtraction.

if( millis() - beginningOfInterval > intervalLength )
{
  beginningOfInterval += intervalLength;

  // whatever you want to do.
}

As long as the interval you're timing is less than the overflow period (~50 days), you can forget about overflow. It won't be a problem.

And honestly, anything you're timing on a 2 month interval should be using the RTC anyway.