Help using Millis()

I am trying to switch a relay for 20 seconds then off but only when called from a html post. I have this working great when using delays however that then blocks LED pattern.

So my question is, I have a void relay1 i wont that to run for 20 seconds then and only after the time call void relay2

Thanks in advance!

void relay_1() {
  currentMillis = millis();
  startMillis = currentMillis;
  if (currentMillis - startMillis >= period) {
    digitalWrite(LED_BUILTIN, LOW);
  }
  server.sendHeader("Location", "/");        // Add a header to respond with a new location for the 
}


void sTop() {
  digitalWrite(LED_BUILTIN, HIGH);
  browser to go to the home page again
  server.send(303);
}

If currentMillis and startMillis are the same how will subtracting one from the other yield a result other than 0?

 currentMillis = millis();
  startMillis = currentMillis;
  if (currentMillis - startMillis >= period)

This test is never going to return true because currentMillis will always equal startMillis

You need to save the value of millis() as the start time when an action occurs then with loop() running freely, check each time through loop() whether the current time minus the start time equals or is greater than the required period

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Thank, that article helped alot! :slight_smile:

They’re called “functions” not “voids”.