Lag response using the blynk app as well as the physical buttons

HI to all developers

I’m a Beginner in blynk world

I have this code which I tested using the blynk app as well as the physical button on my ESP32 development board and there was a lag or slaw response in turning on and off the relays, please help me if you can by revising such code and to by pass any delays or lag issues

Regards

  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - ESP32 board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "settings.h"           
#include "secret.h" 
BlynkTimer timer;
#define MQ2 34

void checkPhysicalButton();
int relay1State = HIGH;
int pushButton1State = HIGH;

int relay2State =HIGH;
int pushButton2State = HIGH;
boolean state = false;
int sensorValue = 0;


// Every time we connect to the cloud...
// Every time we connect to the cloud...

BLYNK_CONNECTED() {

  // Request the latest state from the server

  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);


  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
  //Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
  //Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
  //Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);

}

// When App button is pushed - switch the state

BLYNK_WRITE(VPIN_BUTTON_1) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
  digitalWrite(RELAY_PIN_2, relay1State);
  delay(2000);
  digitalWrite(RELAY_PIN_2, HIGH);
 
}

void checkPhysicalButton()
{
  if (digitalRead(PUSH_BUTTON_1) == LOW) {
    // pushButton1State is used to avoid sequential toggles
    if (pushButton1State != LOW) {

      // Toggle Relay state
      relay1State = !relay1State;
      digitalWrite(RELAY_PIN_1, relay1State);
      digitalWrite(RELAY_PIN_2, relay1State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
      Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
      delay(2000);
      digitalWrite(RELAY_PIN_2, HIGH);


    }
    pushButton1State = LOW;
  } else {
    pushButton1State = HIGH;
  }

  }

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "---------";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "----------";
char pass[] = "----------";

void setup()
{
  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_1, relay1State);
  pinMode(RELAY_PIN_2, OUTPUT);
  digitalWrite(RELAY_PIN_2, relay1State);
  pinMode(MQ2, INPUT);

  // Setup a function to be called every 100 ms
  timer.setInterval(500L, checkPhysicalButton);
  timer.setInterval(1000L, sendUptime);

  // Debug console
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}
void sendUptime()
{
  
  sensorValue = analogRead(MQ2);
  Blynk.virtualWrite(V1, sensorValue);
  Serial.println(sensorValue);

  if (sensorValue > 1000)
  {
    Blynk.notify("Gas Detected!");
    relay1State = HIGH;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);

    }
}
void loop()
{
  Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
  timer.run();


}

void loop()

The delay(2000) commands in your code are blocking commands - all code execution stops at that point until the 2 second delay has completed.

With your BLYNK_WRITE callback functions this happens regardless of whether it was an on or an off event from the button widget.

You need to replace these with BlynkTimer timeout timers, preferably as lambda functions.
A bit of searching g of the forum will explain exactly how to do this.

Pete.

Thank you for your help, please can you rewrite the code based on your solution?

I’m afraid I don’t have time to do that. As I suggested, if you do a search you’ll find the info you need to do it yourself.

Pete.

HI again
this is how I improve the code after a lot of searching

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - ESP32 board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
#define MQ2 34
#define RELAY_PIN_1      12
#define RELAY_PIN_2      13
#define PUSH_BUTTON_1    19 
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "LZn00S06P6uCHmk1BXA2uTzFlkz1KQf3";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "afif.khoury";
char pass[] = "jaad2013";
// Every time we connect to the cloud...
// Every time we connect to the cloud...
void checkPhysicalButton();
int relay1State = LOW;
int pushButton1State = HIGH;
int relay2State =LOW;
int pushButton2State = HIGH;
boolean state = false;
int sensorValue = 0;
int fire_sensor = 4;    // used for ESP32

BLYNK_CONNECTED() {

  // Request the latest state from the server

  //Blynk.syncVirtual(V12);
  //Blynk.syncVirtual(V13);


  // Alternatively, you could override server state using:
  Blynk.virtualWrite(V12, relay1State);
  Blynk.virtualWrite(V13, relay2State);
  //Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
  //Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);

}

// When App button is pushed - switch the state

BLYNK_WRITE(V12) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
  digitalWrite(RELAY_PIN_2, relay1State);
  //delay(500);
  //digitalWrite(RELAY_PIN_2, LOW);
}
void checkPhysicalButton()
{
  if (digitalRead(PUSH_BUTTON_1) == LOW) {
    // pushButton1State is used to avoid sequential toggles
    if (pushButton1State != LOW) {

      // Toggle Relay state
      relay1State = !relay1State;
      digitalWrite(RELAY_PIN_1, relay1State);
      digitalWrite(RELAY_PIN_2, relay1State);

      // Update Button Widget
      Blynk.virtualWrite(V12, relay1State);
      Blynk.virtualWrite(V13, relay2State);
      //delay(500);
      //digitalWrite(RELAY_PIN_2, LOW);
    }
    pushButton1State = LOW;
  } else {
    pushButton1State = HIGH;
  }
}
void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_1, relay1State);
  pinMode(RELAY_PIN_2, OUTPUT);
  digitalWrite(RELAY_PIN_2, relay1State);
  pinMode(MQ2, INPUT);
  pinMode(fire_sensor, INPUT);
  // Setup a function to be called every 100 ms
  timer.setInterval(10L, checkPhysicalButton);
  timer.setInterval(1000L, sendUptime);
  timer.setInterval(10L, fire);
  timer.setInterval(500L, RELAYPIN2);

}
void sendUptime()
{
  
  sensorValue = analogRead(MQ2);
  Blynk.virtualWrite(V1, sensorValue);
  Serial.println(sensorValue);

  if (sensorValue > 1000)
  {
    Blynk.notify("Gas Detected!");
    relay1State = LOW;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(V12, relay1State);

    }
}
void fire()
{
int firesensor = digitalRead(fire_sensor);
Serial.print(firesensor);

if (firesensor == 0)
 {
    Blynk.notify("Fire!");
    relay1State = LOW;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(V12, relay1State);

    }  
}
void RELAYPIN2()
{
if (digitalRead(RELAY_PIN_2) == HIGH)
{
digitalWrite(RELAY_PIN_2, HIGH);
timer.setTimeout(5000L, []() //5 secs on
{digitalWrite(RELAY_PIN_2, LOW);
  
});
}
}
void Blynk_Delay(int milli)
{
  int end_time = millis() + milli;
  while (millis() < end_time)
  {
    if (Blynk.connected())
    {
      Blynk.run();
    }
    yield();
  }
}
void loop()
{
  Blynk.run();
  timer.run();
}

@Afif_Khoury 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.

So you didn’t go down the lambda timer route then.

Your current code has some major issues. Calling two separate timers at exactly the same time, 100 times per second, isn’t good practice and I guess that it will cause you issues in future - probably in the form of unwanted disconnections or lock-ups.

Also, there is no need to debounce switch contacts if you poll them with a timer, especially if the polling frequency is in the 50-100ms range.

Pete.

ok what about this small modification I remove one timer I found out that when I push the button again the other timer running with 1 second instead of 5 second
this is my code now

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - ESP32 board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
#define MQ2 34
#define RELAY_PIN_1      12
#define RELAY_PIN_2      13
#define PUSH_BUTTON_1    19 
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "LZn00S06P6uCHmk1BXA2uTzFlkz1KQf3";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "afif.khoury";
char pass[] = "jaad2013";
// Every time we connect to the cloud...
// Every time we connect to the cloud...
void checkPhysicalButton();
int relay1State = LOW;
int pushButton1State = HIGH;
int relay2State =LOW;
int pushButton2State = HIGH;
boolean state = false;
int sensorValue = 0;
int fire_sensor = 4;    // used for ESP32

BLYNK_CONNECTED() {

  // Request the latest state from the server

  //Blynk.syncVirtual(V12);
  //Blynk.syncVirtual(V13);


  // Alternatively, you could override server state using:
  Blynk.virtualWrite(V12, relay1State);
  //Blynk.virtualWrite(V13, relay2State);
  //Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
  //Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);

}
void RELAYPIN2()
{
if (digitalRead(RELAY_PIN_2) == HIGH)
{
//digitalWrite(RELAY_PIN_2, HIGH);
//timer.setTimeout(5000L, []() //5 secs on
digitalWrite(RELAY_PIN_2, LOW);
 
}
}
// When App button is pushed - switch the state

BLYNK_WRITE(V12) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
  digitalWrite(RELAY_PIN_2, relay1State);
  //delay(500);
  //digitalWrite(RELAY_PIN_2, LOW);
}
void checkPhysicalButton()
{
  if (digitalRead(PUSH_BUTTON_1) == LOW) {
    // pushButton1State is used to avoid sequential toggles
    if (pushButton1State != LOW) {

      // Toggle Relay state
      relay1State = !relay1State;
      digitalWrite(RELAY_PIN_1, relay1State);
      digitalWrite(RELAY_PIN_2, relay1State);

      // Update Button Widget
      Blynk.virtualWrite(V12, relay1State);
      Blynk.virtualWrite(V13, relay2State);
      //delay(500);
      //digitalWrite(RELAY_PIN_2, LOW);
    }
    pushButton1State = LOW;
  } else {
    pushButton1State = HIGH;
  }
}
void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_1, relay1State);
  pinMode(RELAY_PIN_2, OUTPUT);
  digitalWrite(RELAY_PIN_2, relay1State);
  pinMode(MQ2, INPUT);
  pinMode(fire_sensor, INPUT);
  // Setup a function to be called every 100 ms
  timer.setInterval(10L, checkPhysicalButton);
  timer.setInterval(1000L, sendUptime);
  timer.setInterval(10L, fire);
  timer.setInterval(5000L, RELAYPIN2);

}
void sendUptime()
{
  
  sensorValue = analogRead(MQ2);
  Blynk.virtualWrite(V1, sensorValue);
  Serial.println(sensorValue);

  if (sensorValue > 1000)
  {
    Blynk.notify("Gas Detected!");
    relay1State = LOW;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(V12, relay1State);

    }
}
void fire()
{
int firesensor = digitalRead(fire_sensor);
Serial.print(firesensor);

if (firesensor == 0)
 {
    Blynk.notify("Fire!");
    relay1State = LOW;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(V12, relay1State);

    }  
}
void Blynk_Delay(int milli)
{
  int end_time = millis() + milli;
  while (millis() < end_time)
  {
    if (Blynk.connected())
    {
      Blynk.run();
    }
    yield();
  }
}
void loop()
{
  Blynk.run();
  timer.run();
}

What about it. How does it address the issues that I highlighted?

Pete.

Dear Sir
Greetings
Is there any improvement in my new code? are those the correct Lambda functions or timers that I should be using in my code ? please help me to improve my code
thank you in advance.
Best regard

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
#define MQ2 34
//#define RELAY_PIN_1      12
//#define RELAY_PIN_2      13
//#define PUSH_BUTTON_1    19 
char auth[] = "LZn00S06P6uCHmk1BXA2uTzFlkz1KQf3";
char ssid[] = "afif.khoury";
char pass[] = "jaad2013";
void checkPhysicalButton();
int relay1State = LOW;
int relay2State = LOW;
int pushButton1State = HIGH;
boolean state = false;
int sensorValue = 0;
int fire_sensor = 4;    // used for ESP32
int RELAY_PIN_1 = 12;
int RELAY_PIN_2 = 13;
int PUSH_BUTTON_1 = 19;
void checkPhysicalButton();


BLYNK_CONNECTED() {

  // Request the latest state from the server

  //Blynk.syncVirtual(V12);
  //Blynk.syncVirtual(V13);


  // Alternatively, you could override server state using:
Blynk.virtualWrite(V12, relay1State);
Blynk.virtualWrite(V13, relay2State);
  //Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
  //Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
}
// When App button is pushed - switch the state

BLYNK_WRITE(V12) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
  digitalWrite(RELAY_PIN_2, relay1State);

}
BLYNK_WRITE(V13) {
relay2State = param.asInt();
digitalWrite(RELAY_PIN_2, relay2State);
   }
//BLYNK_WRITE(V14) {
  //relay2State = param.asInt();
  //relay1State = param.asInt();
  //digitalWrite(RELAY_PIN_1, relay1State);
  //digitalWrite(RELAY_PIN_2, relay2State);
 // }
void setup()
{
  // Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(RELAY_PIN_1, OUTPUT);
pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
digitalWrite(RELAY_PIN_1, relay1State);
digitalWrite(RELAY_PIN_2, relay2State);
pinMode(RELAY_PIN_2, OUTPUT);
pinMode(MQ2, INPUT);
pinMode(fire_sensor, INPUT);
  // Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalButton);
timer.setInterval(1000L, sendUptime);
timer.setInterval(1000L, fire);
timer.setInterval(5000L, delay50sec);


}
void checkPhysicalButton()
{
  if (digitalRead(PUSH_BUTTON_1) == LOW) {
    // pushButton1State is used to avoid sequential toggles
    if (pushButton1State != LOW) {
      relay1State = !relay1State;
    digitalWrite(RELAY_PIN_1, relay1State);
    digitalWrite(RELAY_PIN_2, relay1State);
    //Blynk.virtualWrite(V12, relay1State);

        }
    pushButton1State = LOW;
  } else {
    pushButton1State = HIGH;
  }
}

void sendUptime()
{
  sensorValue = analogRead(MQ2);
  Blynk.virtualWrite(V1, sensorValue);
  Serial.println(sensorValue);

  if (sensorValue > 1000)
  {
    Blynk.notify("Gas Detected!");
    relay1State = LOW;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(V12, relay1State);

    }
}
void fire()
{
int firesensor = digitalRead(fire_sensor);
Serial.print(firesensor);

if (firesensor == 0)
 {
    Blynk.notify("Fire!");
    relay1State = LOW;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(V12, relay1State);

    }  
}
      
void delay50sec()      
     {
      if (digitalRead(RELAY_PIN_2) == HIGH) {
       digitalWrite(RELAY_PIN_2, LOW);
 }
     }



void loop()
{
  Blynk.run();
  timer.run();
}

As far as I can see, the code you’ve posted doesn’t contain any lambda functions, and the only Blynk timers you are using are Interval timers, not Timeout timers.

Pete.

what do you think so far is there any improvement?

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
char auth[] = "LZn00S06P6uCHmk1BXA2uTzFlkz1KQf3";
char ssid[] = "afif.khoury";
char pass[] = "jaad2013";
void checkPhysicalButton();
int relay1State = LOW;
int relay2State = LOW;
int pushButton1State = HIGH;
boolean state = false;
int RELAY_PIN_1 = 12;
int RELAY_PIN_2 = 13;
int PUSH_BUTTON_1 = 19;
void checkPin()
{
 if (digitalRead(relay1State) == LOW) {
       digitalWrite(RELAY_PIN_2, LOW);
       digitalWrite(relay2State, LOW);
    }
if (digitalRead(RELAY_PIN_2) == HIGH) {
timer.setTimeout(5000L, [=]()  {
      digitalWrite(RELAY_PIN_2, LOW);
      digitalWrite(relay2State, LOW);

    } );
      }
     }
BLYNK_CONNECTED() {
Blynk.virtualWrite(V12, relay1State);
Blynk.virtualWrite(V13, relay2State);
Blynk.virtualWrite(V1, relay1State);
}

// When App button is pushed - switch the state

BLYNK_WRITE(V12) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
  digitalWrite(RELAY_PIN_2, relay1State);

}
BLYNK_WRITE(V13) {
relay2State = param.asInt();
digitalWrite(RELAY_PIN_2, relay2State);
   }
//BLYNK_WRITE(V14) {
  //relay2State = param.asInt();
  //relay1State = param.asInt();
  //digitalWrite(RELAY_PIN_1, relay1State);
  //digitalWrite(RELAY_PIN_2, relay2State);
 // }
void setup()
{
  // Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(RELAY_PIN_1, OUTPUT);
pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
digitalWrite(RELAY_PIN_1, relay1State);
digitalWrite(RELAY_PIN_2, relay2State);
pinMode(RELAY_PIN_2, OUTPUT);
timer.setInterval(100L, checkPhysicalButton);
}
void checkPhysicalButton()
{
 if (digitalRead(PUSH_BUTTON_1) == LOW) {
// pushButton1State is used to avoid sequential toggles
 if (pushButton1State != LOW) {
relay1State = !relay1State;
relay2State = !relay2State;

digitalWrite(RELAY_PIN_1, relay1State);
digitalWrite(RELAY_PIN_2, relay2State);
Blynk.virtualWrite(V12, relay1State);
Blynk.virtualWrite(V13, relay2State);
}
pushButton1State = LOW;
 } else {
 pushButton1State = HIGH;
 }
 }

void loop()
{
  Blynk.run();
  timer.run();
  checkPin();

}

Okay, so you’ve finally done a bit of research and figured-out what a Lambda timeout timer looks like.

Unfortunately, You’ve lost sight of what you were trying to achieve in the first place. Originally, you wanted to change a relay state, wait 2 seconds then change the relay state back again.
That was coded in a very messy way, plus it had a blocking delay of 2 seconds which was causing your button lag.

That 2 second delay needed to be replaced with a lambda timeout timer of 2 seconds duration to achieve the same result.
Now that you know what a lambda timeout timer is, re-write your original code to replace the delay with the lambda function.

Pete.

Function achieved successfully in my Last code with a small problem, the problem is that I want to reset or stop the timer, each time I press the button less than two second it seems that the timer will continue, for example if I push the button at the beginning relay No.1 and relay No.2 turn on after 2 second delay relay No.2 will turn off but if I push the button again during this period of time within one second and before the two seconds ends relay No.2 will turn off within one second not two
So what function i could use to reset the timer?

Your latest code is all wrong, you shouldn’t be calling any functions from your void loop.

Pete.

I thought it is wrong but, when I checked below blynk example it seems that I can call any any functions in the void loop such as checkPin();
please check from the link below is this correct?

https://examples.blynk.cc/?board=ESP32&shield=ESP32%20WiFi&example=More%2FSync%2FButtonPoll

what are the other options I have? please assist me.

You don’t need the checkPin function at all.
You just need to structure your code in a logical way, and use timers to poll the state of your pins as necessary.

Pete.

Dear Sir

Greetings

Ok I have a new challenge, if I decided to use two relays one of them is (external low trigger relay) which is connected to esp8266 board.
In order to trigger such relay we should apply low voltage or zero to the input instead of high input 5v, please assist me in modifying my below code considering that RELAY_PIN_2 is the low trigger relay.

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
char auth[] = "LZn00S06P6uCHmk1BXA2uTzFlkz1KQf3";
char ssid[] = "afif.khoury";
char pass[] = "jaad2013";
void checkPhysicalButton();
int relay1State = LOW;
int relay2State = LOW;
int pushButton1State = HIGH;
boolean state = false;
int RELAY_PIN_1 = 12;
int RELAY_PIN_2 = 13;
int PUSH_BUTTON_1 = 19;
void checkPin()
{
 if (digitalRead(relay1State) == LOW) {
       digitalWrite(RELAY_PIN_2, LOW);
       digitalWrite(relay2State, LOW);
    }
if (digitalRead(RELAY_PIN_2) == HIGH) {
timer.setTimeout(5000L, [=]()  {
      digitalWrite(RELAY_PIN_2, LOW);
      digitalWrite(relay2State, LOW);

    } );
      }
     }
BLYNK_CONNECTED() {
Blynk.virtualWrite(V12, relay1State);
Blynk.virtualWrite(V13, relay2State);

}

// When App button is pushed - switch the state

BLYNK_WRITE(V12) {
  relay1State = param.asInt();
  relay2State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
  digitalWrite(RELAY_PIN_2, relay2State);

}
BLYNK_WRITE(V13) {
relay2State = param.asInt();
digitalWrite(RELAY_PIN_2, relay2State);
   }
//BLYNK_WRITE(V14) {
  //relay2State = param.asInt();
  //relay1State = param.asInt();
  //digitalWrite(RELAY_PIN_1, relay1State);
  //digitalWrite(RELAY_PIN_2, relay2State);
 // }
void setup()
{
  // Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(RELAY_PIN_1, OUTPUT);
pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
digitalWrite(RELAY_PIN_1, relay1State);
digitalWrite(RELAY_PIN_2, relay2State);
pinMode(RELAY_PIN_2, OUTPUT);
timer.setInterval(100L, checkPhysicalButton);
}
void checkPhysicalButton()
{
 if (digitalRead(PUSH_BUTTON_1) == LOW) {
// pushButton1State is used to avoid sequential toggles
 if (pushButton1State != LOW) {
relay1State = !relay1State;
relay2State = !relay2State;
digitalWrite(RELAY_PIN_1, relay1State);
digitalWrite(RELAY_PIN_2, relay2State);
Blynk.virtualWrite(V12, relay1State);
Blynk.virtualWrite(V13, relay2State);
}
pushButton1State = LOW;
 } else {
 pushButton1State = HIGH;
 }
 }

void loop()
{
  Blynk.run();
  timer.run();
  checkPin();

}

For that relay, change LOW for HIGH in your code.

But seriously your code is appalling and that’s the real challenge that you need to resolve.

Pete.

is there a light at the end of tunnel? is there an improvment so far in my Code?

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
char auth[] = "tqMqcpssdH1Z1UNLqEi89DOL57O0iAcx";
char ssid[] = "afif.khoury";
char pass[] = "jaad2013";
void checkPhysicalButton();
int relay1State = LOW;
int relay2State = LOW;
int pushButton1State = HIGH;
boolean state = false;
int RELAY_PIN_1 = 12;
int RELAY_PIN_2 = 14;
int PUSH_BUTTON_1 = 0;

BLYNK_CONNECTED() {
Blynk.virtualWrite(V12, relay1State);
Blynk.virtualWrite(V13, relay2State);
}

// When App button is pushed - switch the state

BLYNK_WRITE(V12) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
  //if (relay2State== LOW) {
  //digitalWrite(RELAY_PIN_2, HIGH);
  //}
  //else {digitalWrite(RELAY_PIN_2, LOW);}
}
BLYNK_WRITE(V13) {
relay1State = param.asInt();
relay2State = param.asInt();
digitalWrite(RELAY_PIN_1, relay1State);
digitalWrite(RELAY_PIN_2, !relay2State);

if (digitalRead(RELAY_PIN_2) == LOW) {
timer.setTimeout(5000L, [=]()  {
digitalWrite(RELAY_PIN_2, HIGH);
    } );
}
else {
digitalWrite(RELAY_PIN_2, HIGH);
}
}
//BLYNK_WRITE(V14) {
  //relay2State = param.asInt();
  //relay1State = param.asInt();
  //digitalWrite(RELAY_PIN_1, relay1State);
  //digitalWrite(RELAY_PIN_2, relay2State);
 // }
void setup()
{
  // Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(RELAY_PIN_1, OUTPUT);
pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
digitalWrite(RELAY_PIN_1, relay1State);
digitalWrite(RELAY_PIN_2, !relay2State);
pinMode(RELAY_PIN_2, OUTPUT);
timer.setInterval(100L, checkPhysicalButton);
}
void checkPhysicalButton()
{
 if (digitalRead(PUSH_BUTTON_1) == LOW) {
// pushButton1State is used to avoid sequential toggles
 if (pushButton1State != LOW) {
relay1State = !relay1State;
relay2State = !relay2State;

digitalWrite(RELAY_PIN_1, relay1State);
digitalWrite(RELAY_PIN_2, !relay2State);

if (digitalRead(RELAY_PIN_2) == LOW) {
timer.setTimeout(5000L, [=]()  {
digitalWrite(RELAY_PIN_2, HIGH);
    } );
}
else {

digitalWrite(RELAY_PIN_2, HIGH);}
   
Blynk.virtualWrite(V12, relay1State);
Blynk.virtualWrite(V13, relay2State);
}
pushButton1State = LOW;
 } else {
 pushButton1State = HIGH;
 }
 }

void loop()
{
  Blynk.run();
  timer.run();

}