A program uses the millis() function on a regular basis as a means to keep track of an output being set during a few hours. However, when the output is actually being tracked while the millis() overflows (after 50 days of consecutive running program) the consequence is that the amount of time this particular output is set is not known anymore.
How is this overflow issue addressed when the time from millis() is used to measure some specific action performed by the program?
For example: a valve is opened (and later closed) due to a specific digital input (say a low level sensor), and the amount of time this valve is opened is to be monitored so that in due time an alarm can be triggered. Now when the millis() overflow during a time when the valve is opened, how do you take that eventuality into account to make sure that the actual time that this valve is opened is never lost?
Have you done the arithmetic using unsigned longs?
Provided you don't try to measure times greater than 49 days, the rollover is not a problem, and even if you do want longer times, it is very simply handled.
TolpuddleSartre:
Have you done the arithmetic using unsigned longs?
Provided you don't try to measure times greater than then 49 days, the rollover is not a problem, and even if you do want longer times, it is very simply handled.
I am not sure I understand: suppose the last milli is 9999, and the valve opens at 9990, and startTime = millis(); which will then be 9990. A few moments later millis() reverts back to 0 counting upwards.
If I set an alarm that depens on the time difference between millis() and startTime then I will never be able to set the alarm? No?
Just do the arithmetic for yourself, using unsigned variables.
Unsigned shorts will demonstrate the solution if you don't want to wait even 70 minutes for micros to rollover.
void setup ()
{
Serial.begin (115200);
unsigned int end = 0x10; // after rollover
unsigned int start = 0xfff0; // before rollover
unsigned int diff = end - start;
Serial.println (diff); // prints 32.
}
void loop ()
{
}