Switch a relay on for 10 minutes off for 20 and repeat

Hello guys, im struggling to create a code with that turns ON a relay for 10 minutes and OFF for 20 and repeats over time, i can do it easily with delay function but millis is braking my brain :smile:

Can someone give me a hand ?

Hi @eldor2011
We can help, but I don't write it totally for you.
Describe your difficulty and using tags </>post the code what you wrote.

ArduinoForum

RV mineirin

Start with the 02.Digital->BlinkWithoutDelay example. Make the interval time a variable. Set it to 10 minutes or 20 minutes depending on the state of the output (10 minutes for ON and 20 minutes for OFF).

const unsigned long SECONDS = 1000;
const unsigned long MINUTES = 60 * SECONDS;
const unsigned long ON_INTERVAL = 10 * MINUTES;
const unsigned long OFF_INTERVAL = 20 * MINUTES;

Should be something like this?
`if(currentMillis - previousMillis > interval) {

if (ledState == LOW)
  ledState = HIGH
  interval=20;
else
  ledState = LOW;
  interval=10;`

Remember that C++ is NOT like Python. It doesn't care about your indentation. It only cares about your curly braces. You also forgot one of the semicolons.

What you should have written is:

if (ledState == LOW)
{
  ledState = HIGH;  // Relay OFF
  interval=20;
}
else
{
  ledState = LOW;  // Relay ON
  interval=10;
}

That can work, as long as the rest of the code is expecting 'interval' to be in minutes instead of the usual milliseconds.