millis() overflow will not hurt you...

Using millis() to determine when an event should happen is definitely the preferred approach over using delay(), because using millis() is non-blocking. In general:

unsigned long currentTime = 0;
unsigned long previousTime = 0;
unsigned long interval = 10000;
...

void loop() {
    currentTime = millis();
    if (currentTime - previousTime >= interval) {
      previousTime = currentTime;
      // perform the event
    }
    // And now we can check if it's time for other events
    // because we aren't blocked by delay()
}

But notice the comparison is subtracting previousTime from currentTime on every iteration of loop. Instead we coould compute the next time the event should happen, and just compare that with currentTime:

unsigned long currentTime = 0;
unsigned long nextTime = 0;       // next scheduled event time
unsigned long interval = 10000;   // milliseconds between events
...

void loop() {
    currentTime = millis();
    if (currentTime >= nextTime) {
      nextTime += interval;
      // perform the event
    }
}

This is slightly more efficient because it eliminates the subtraction on every loop. The problem, as our friend Nick explains (see OP's link) is that when nextTime overflows it becomes a small number, much smaller than currentTime, so the event will happen immediately on the next loop even though the interval has not transpired.

Nick says this is why you should use subtraction. However, the overflow problem can be fixed if you initialize nextTime = interval instead of 0, and add a second condition in loop:

if (currentTime >= nextTime && nextTime >= interval) {

The second condition ensures the event doesn't happen when nextTime is small because of overflow. Adding this condition doesn't slow down execution, because an AND clause stops being evaluated when the first false condition is found, and currentTime >= nextTime will be false until it's time for the event or during the interval when nextTime overflows every 49 days.

This may seem like a ridiculous obsession with clock cycles, and for only one or a few timer events I'm sure it is - tbh I haven't done any benchmarking. But hey, if the subtraction isn't necessary why do it?

/r/arduino Thread