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

I need to stop function after X seconds

Hi,

I already set up wifi connection to my shutters at home. And for controlling i’m using voice control via Tasker app. So when i said to google assistant to close my shutters it works fine. But after all closed i have to say “shutters okay” to switch off the relays to prevent any motor damage in shutters. I just want to add 15 seconds to the end of relay on position code. With that when i said “close the shutters” it will be closed and after 15 seconds relay will be switched the close position automaticly. Is there a any way to solve that problem.

Hardware is ESP8266-01

You need to use a BlynkTimer in timeout mode within your sketch. The simplest way to do this is as a Lambda function.

Read the timeout Timers section of this tutorial…

Pete.

Thank you Pete.

I just read that article and solved. For other person who need helps on that topic, here is my code. I solved it with adding new “void” function.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char auth[] = "*******************************";


char ssid[] = "*********";
char pass[] = "***********************";

char server[] = "blynk-cloud.com";
int port = 8080;



int out1=0; 
int before1; 
int out2=2;  
int before2; 

SimpleTimer timer;

void setup(){
  Serial.begin(9600);
  before1=1; 
  pinMode(out1,OUTPUT);
  before2=1; 
  pinMode(out2,OUTPUT);
  digitalWrite(out1, HIGH); //Making the pins high to keep the relay board off (for active low relays)
  digitalWrite(out2, HIGH); // make it LOW for active high relay boards.

 Blynk.begin(auth, ssid, pass, server, port);    
}

void before11(){
  digitalWrite(out1,HIGH);
  before1=1;
  Blynk.virtualWrite(V5,0);

}

void before21(){
  digitalWrite(out2, HIGH);
  before2=1;
  Blynk.virtualWrite(V6,0);
}

BLYNK_WRITE(V5)    // Virtual pin 1 to contlor relay 1  
{
  
  int pinData = param.asInt(); 
  if (pinData==1){
      digitalWrite(out1,LOW); //relay1 is now turned on
      before1=0;
      timer.setTimeout(15000L,before11);
      }
    if (pinData==0){ 
      digitalWrite(out1,HIGH); //relay1 is now turned off
      before1=1; 
      }
   }

BLYNK_WRITE(V6) // Virtual pin 2 to contlor relay 2 
{
  int pinData = param.asInt(); 
  if (pinData==1){ 
      digitalWrite(out2,LOW);
      before2=0; 
      timer.setTimeout(15000L,before21);
     }
    if (pinData==0){ 
      digitalWrite(out2,HIGH);
      before2=1; 
      }
  }
  
void loop() {

 Blynk.run();
  timer.run(); 
}

‘’’

@CaptainOnedin please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Done :innocent:

The Lambda function is usually much neater.

Pete.

Yeap, it seems like. I’ll try that also. Thank you @PeteKnight

Also, anyone planning on using this sketch should be aware that it’s a Blynk Legacy sketch and will need modification before it can be used with Blynk IoT.

Pete.