I've done the math and the Using millis() for timing sticky post is only somewhat correct in stating that it will work through a rollover. It depends on your definition of what will work.
Essentially the program will not crash. However if the currentMillis - ULONG_MAX (4294967295) < period when the code section is run then when the rollover happens for sure currentMillis - startMillis >= period. The problem is the elapsed time is actually less than the period.
So if your program is expecting exact timing then this will not work. Am I on the right track? Did I do the math right? Is there a way to check for this and make it exact. Of course this exactness depends on the other sections of code that run in between which could have variable amounts of execution time. But assuming all else equal. Thanks.
To make it easier to understand let's use a byte variable instead of unsigned long as the numbers are easier to get your head round.
Example: If your old time was at 250 and now you're at 5 , you calculate (5 - 250) and interpret the result as an unsigned byte, the result is 11, which is correct
It includes a technique for testing a solution without waiting 56 days.
Thanks for the link to the explanation that is given regarding modular arithmetic. Initially I was doing the math by hand wrong. By exact timing I meant that my test condition would not be true until AT LEAST the period had elapsed. Okay so thats not exact timing. Sorry. I was concerned that at rollover the condition would become true. My test condition is in the program I wrote to simulate a timer rollover: