Slight error found in BlinkWithoutDelay tutorial

Hello everyone,

I was looking at the BlinkWithoutDelay tutorial and tried it on my project to replace some slopy code and it worked great. I had no clue what millis() was used for till now. I am glad I found it.

A slight problem I found in the example coding is in this section:

if(currentMillis - previousMillis > interval) {

This works great, but the millis() function will eventually overflow (at I think is 45 days?) and will eventually render the code useless after that time. Here is why I believe this is true:

Since currentMillis = millis(), then after 2,147,483,647 it will reset to 0. If the code for the led was at 2,147,483,000 and set previousMillis to that number, the code would no longer work after the reset. This would be because currentMillis - previousMillis would not be less that interval (2147483000 - 0 < interval).

A solution I fould to this is to reset the long variable (previousMillis) along with the currentMillis when millis = 0.

unsigned long currentMillis = millis();
if (millis == 0){
previousMillis = 0;

Let me now if I am missing something or if there is a better way for the reset. I no that with long intervals (over 5 min) this would not work well because the delay would be longer when the millis is reset to 0.

If previousMillis was just before the rollover, 0xFFFFFFF0, and currentMillis is after the rollover, say 0x0000000F, then fire up your calculator and do the math: 0x0000000F - 0xFFFFFFF0 = 0xFFFFFFFF0000001F. Now throw away the upper 32bits, and you get 0x0000001F, which is what you'd expect. Unsigned long math does the same since there are only 32 bits, so the rollover is not an issue except in the case where the values were like 24+ days apart. But you should be tracking the time more often than that.

There is an error in this lineif(currentMillis - previousMillis > interval), but it is that the comparison should be ">=".