I have this kind of question. Is there any limitation about max millis() counter? I mean does it matter if currenttime in millis counts up certain value and the whole loop stops?
I'm using millis() in order to set one counter to 0. At the moment ESP32 plugged to serial monitor about 23hours ticking, the millis() was working fine.
If the counter have not been activated, the currenttime=millis() always ticking.
Here is the part of the code which I'm using for the timer:
// Flag variable to keep track if alert SMS was sent or not
bool smsSent = false;
bool timer = false;
bool smscountermax=false;
unsigned long smscounter= 0;
unsigned long previousMillistemprst = 0;
// constants won't change:
const int tempresetinterval = 3600000;
void_loop()
if((t > temperatureThresholdHI) && !smsSent && !smscountermax){
String smsMessage = String("Temperature above threshold: ") +
String(t) + String("C") + String("\nHumidity=") + String(h) +String("%");
modem.sendSMS(SMS_TARGET, smsMessage);
SerialMon.println("\nSending SMS:");
SerialMon.println(smsMessage);
smsSent = true;
smscounter++;
}
if((t < temperatureThresholdLO) && !smsSent && !smscountermax){
String smsMessage = String("Temperature below threshold: ") +
String(t) + String("C")+ String("\nHumidity=") + String(h) +String("%");
modem.sendSMS(SMS_TARGET, smsMessage);
SerialMon.println("\nSending SMS:");
SerialMon.println(smsMessage);
smsSent = true;
smscounter++;
}
unsigned long currenttime=millis();
if (t < temperatureThresholdHI && t > temperatureThresholdLO && smsSent && !timer) {
// save the last time when the timer was activated
previousMillistemprst=currenttime;
timer = true;
}
// if t values are going over certain values the counter will be set to false again.
else if(t>temperatureThresholdHI || t < temperatureThresholdLO && smsSent && timer)
{
timer = false;
}
// if the t values have been in the right place the counter will be set false again.
if(currenttime-previousMillistemprst >= tempresetinterval && timer)
{
SerialMon.println("Setting SMS sent to false");
smsSent = false;
timer = false;
delay(1000);
}
Serial.println("Current milliseconds:"); Serial.println(currenttime-previousMillistemprst);//Print the time from the reset
Serial.println("SMS is sent (1) or not (0):"); Serial.println(smsSent);
Serial.println("Timer is active (1) or not (0):"); Serial.println(timer);
Serial.println("SMS sent counter is:"); Serial.println(smscounter);
delay(2500);
}
The examples were written for processors where millis() returns an unsigned long and rolls over after 49 and a bit days, but the principles used are the same no matter what the type of variable as long as it is unsigned