Page 1 of 1

Question about millis() going back to 0. Small u int minus big u int

Posted: Mon Jul 01, 2019 9:38 am
by GeorgeFlorian1
Hello !

I am trying to toggle two relays after a set period of time and I can't use delay() (for obvious reasons = because if one relay should be toggled the delay starts, blocks the code and the second relay can't be toggled even if its conditions were met). This means I need to use millis() so that the execution won't stop.

I've read some things about millis() and the biggest "fear" I have is related to it going back to zero after it reaches 2^32-1 = 49,7 days.

Here is my current code (I will write the code only for one relay, because both bits of code are similar):
Delay is a String because I use it on the WebServer.

Code: Select all

// #include ...
bool offFlagRelayOne = false;
bool offFlagRelayTwo = false;
unsigned int startTimeRelayOne = 0;
unsigned int startTimeRelayTwo = 0;
unsigned int currentTimeRelayOne = 0;
unsigned int currentTimeRelayTwo = 0;
String Delay = "10"; // in seconds

void setup() {
// ...
            server.on("/relay1_on", HTTP_POST, [](AsyncWebServerRequest * request){
        int params = request->params();
        String values_button = "";
        for(int i=0;i<params;i++){
          AsyncWebParameter* p = request->getParam(i);
          if(p->isPost()){
              logOutput((String)"POST[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");            
              values_button = p->name();            
            } else {
                logOutput((String)"GET[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");
              }
        } // for(int i=0;i<params;i++)
        values_button.trim();
        if(values_button == "relay1_on") {
          request->redirect("/relay1/on");
          logOutput((String)"Closing " + relay1 + " in: " + Delay + "seconds !");
          startTimeRelayOne = millis();
          offFlagRelayOne = false;
          Serial.println("Do I get here ? relay1_on POST");
        }   
      });
      server.on("/relay1_off", HTTP_POST, [](AsyncWebServerRequest * request){
        int params = request->params();
        String values_button = "";
        for(int i=0;i<params;i++){
          AsyncWebParameter* p = request->getParam(i);
          if(p->isPost()){
              logOutput((String)"POST[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");            
              values_button = p->name();            
            } else {
                logOutput((String)"GET[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");
              }
        } // for(int i=0;i<params;i++)
        values_button.trim();
        if(values_button == "relay1_off") {
          offFlagRelayOne = true;
          request->redirect("/relay1/off");
        }  
      });
// ...
}

void loop() {
  currentTimeRelayOne = millis();
  currentTimeRelayTwo = millis();

  if(startTimeRelayOne != 0 && !offFlagRelayOne) {
    if ((currentTimeRelayOne - startTimeRelayOne) > (Delay.toInt()*1000)) {
      digitalWrite(RELAY1,LOW);
      status1 = "OFF";
      logOutput((String)relay1 + " closed");
      startTimeRelayOne = 0;
    }
  }
}