Addition operations involving millis() should not be performed. There is almost always a way to write the code using subtraction operations, instead, which will work when millis() rolls over. Adding to the output of millis() just before it rolls over will result in a small number that you are then testing with respect to a large number.
timeOut = 1000; // 1 second
unsigned long startTime = millis();
while(waiting for interrupt)
{
if(millis()-startTime >= timeOut){break;}
}
As PaulS says, ( (later time) - (earlier time) ) will yield a valid result if the times occur across the FFFF FFFF to 0000 0000 rollover.
Try it using windows calculator in scientific view mode, ignoring anything above the 8 characters, just like the Arduino does for an unsigned long.
Do you think there would be any concerns with the amount of time that the global interrupts are disabled from using millis? I realize that they're only disabled for a few clock cycles but since it's a large portion of the code in the loop I worry that the short time will add up over time.
wayneft:
Do you think there would be any concerns with the amount of time that the global interrupts are disabled from using millis? I realize that they're only disabled for a few clock cycles but since it's a large portion of the code in the loop I worry that the short time will add up over time.
It won't make any difference unless interrupts are disabled for a long time - this is because millis () uses overflow interrupts on the clock timer. Whether or not interrupts are enabled it will be accurate. The only problem would be if interrupts are disabled for so long that two (timer) interrupts were missed.
However for your application (eg. in an inner loop in the I2C library) just a simple counter should help you recover from an indefinite loop.