Hi,
im a little worried about the effects of millis() rollover when im trying to calculate the time that has elapsed during a process. Perhaps im a little over worried???
Basically what id like to do is:
- initialize a timer.
- wait for serial input to match some given criteria.
- if criteria matches then reset the timer,
else if the timer is greater then some interval (say 10 seconds) then flip the state on a "timeout" flag.
So thought of doing something like this (non-functional code ahead):
#define timeout 10000
int timer = millis();
boolean connectFlag = false;
void loop() {
if (Serial.available() != 0) {
int myChar = Serial.read();
appendBuffer(myChar); // builds a buffer from serial input
if (myChar == 13) {
if (checkBuffer() == true) { // check the buffer after char 13 received
timer = millis(); // SUCCESS, so we now need to reset the timer.
connectFlag = true; // sets a flag to indicate the device connected
}
clearBuffer(); // clears the buffer after char 13 received
}
} else if (millis() >= timer + timeout) { // device did not respond in a timely manor so change the connectFlag to false
timer = millis(); // FAILED, we will reset the timer anyways.
connectFlag = false;
}
}
could this method pose a vulnerability to millis() rollover? and if so, how could one rectify this to be a little safer??
Thanks!!