BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Automatically turn off output after set time

I have an Arduino UNO with an ethernet shield running a very simple blink program. It has 8 buttons that control relays. I want the relays to automatically turn off after a set amount of time from when they got turned on. what is the best way to do this? if it uses millis it needs to handle rollover perfectly and I don’t know how to do that.

It’s fairly easy to avoid millis rollover issues if you write your comparison test correctly, and there are plenty of examples how to do that, and explanations of why they work, on the internet.

However, for this type of thing using millis isn’t usually the nest approach, as you need a routine to constantly do your millis comparisons, and with 8 relays that gets messy.

The simplest approach is to use a lambda timeout timer like this…

  digitalWrite(relay1, LOW);       // Turn relay1 On
  
  timer.setTimeout(5000L, []() 
  {  // Wait 5 seconds then...
     digitalWrite(relay1, HIGH);   // Turn relay1 Off again
  });  // END of Lambda Function

You’ll need to declare a timer object as part of your initialisation code near the top of your sketch…

// Initialise the timer object
BlynkTimer timer;

and service the timer object in your void loop…

  timer.run();

Pete.