I need to read a sensor every five minutes, but since my sketch also has
other tasks to do, I cannot just delay()
between the readings. There
is the Blink without
delay tutorial
suggesting I code along these lines:
void loop()
{
unsigned long currentMillis = millis();
// Read the sensor when needed.
if (currentMillis - previousMillis >>= interval) {
previousMillis = currentMillis;
readSensor();
}
// Do other stuff...
}
The problem is that millis()
is going to roll over back to zero after
roughly 49.7 days. Since my sketch is intended to run for longer
than that, I need to make sure the rollover does not make my sketch
fail. I can easily detect the rollover condition(currentMillis < previousMillis
), but I am not sure what to do then.
Thus my question: what would be the proper/simplest way to handle the
millis()
rollover?
I need to read a sensor every five minutes, but since my sketch also has
other tasks to do, I cannot just delay()
between the readings. There
is the Blink without
delay tutorial
suggesting I code along these lines:
void loop()
{
unsigned long currentMillis = millis();
// Read the sensor when needed.
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
readSensor();
}
// Do other stuff...
}
The problem is that millis()
is going to roll over back to zero after
roughly 49.7 days. Since my sketch is intended to run for longer
than that, I need to make sure the rollover does not make my sketch
fail. I can easily detect the rollover condition(currentMillis < previousMillis
), but I am not sure what to do then.
Thus my question: what would be the proper/simplest way to handle the
millis()
rollover?
I need to read a sensor every five minutes, but since my sketch also has
other tasks to do, I cannot just delay()
between the readings. There
is the Blink without
delay tutorial
suggesting I code along these lines:
void loop()
{
unsigned long currentMillis = millis();
// Read the sensor when needed.
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
readSensor();
}
// Do other stuff...
}
The problem is that millis()
is going to roll over back to zero after
roughly 49.7 days. Since my sketch is intended to run for longer
than that, I need to make sure the rollover does not make my sketch
fail. I can easily detect the rollover condition(currentMillis < previousMillis
), but I am not sure what to do then.
Thus my question: what would be the proper/simplest way to handle the
millis()
rollover?
How can I handle the millis() rollover?
I need to read a sensor every five minutes, but since my sketch also has
other tasks to do, I cannot just delay()
between the readings. There
is the Blink without
delay tutorial
suggesting I code along these lines:
void loop()
{
unsigned long currentMillis = millis();
// Read the sensor when needed.
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
readSensor();
}
// Do other stuff...
}
The problem is that millis()
is going to roll over back to zero after
roughly 49.7 days. Since my sketch is intended to run for longer
than that, I need to make sure the rollover does not make my sketch
fail. I can easily detect the rollover condition (currentMillis < previousMillis
), but I am not sure what to do then.
Thus my question: what would be the proper/simplest way to handle the
millis()
rollover?