I needed a way of pausing my code for X amount of milliseconds but couldn't afford to use the delay() function so I did something like this:
void loop() {
unsigned long JasonB = millis();
Serial.println("Going to delay for 5 seconds");
while (mPause(JasonB,5000) == false) {
}
Serial.println("OutPut Here");
}
boolean mPause(unsigned long lStart, unsigned long lDelay) {
unsigned long cTime = millis();
if (cTime < lStart) {
// apparently millis() has rolled over and not sure how to handle it??
} else {
//this part of my code works exactly as needed
if ((lStart + lDelay) <= cTime) { return true; } else {return false; }
}
}
My function works great but I want to know how to handle it in case of a rollover. I am a newbie so my methods may not be most efficient so please feel free to criticize constructively. Again, I do not want to use the delay() function because I have other things that need to run at the same time.
Yeah, I found that and edited my code accordingly:
boolean mPause(unsigned long lStart, unsigned long lDelay) {
if ((millis() - lStart) >= lDelay) { return true; } else { return false; }
}
I'm just not understanding if that would work in the event of rollover though.
For example's sake, say millis() rolled over at 999 so at 1000 it would actually read 0. Say I started the pause at 995 and wanted to delay for 10ms which would end up being at 5ms (after rollover). The code would function like so:
[for example, current millis() is = 0]
millis()-995 = -955 which I believe is interrupted as a positive number, right? and so it would return as true 5ms before it's supposed to. And if it's not read as a positive, it would take TOO long to return as true.
Either way, I'm not seeing how this will return correctly. Am I missing something?
Because the values are all unsigned longs, and the second value is greater than the first value, and the overflow flag is set on the first value, 0 - 995 is 5, just as 1000 - 995 is 5.