Virtual button not responding

Details:
• Hardware model + communication type: ESP8266, HC SR04, I2C, 5V relay
• Smartphone OS (iOS or Android) + version: Android 9
• Blynk server or local server: Blynk server
• Blynk Library version :0.6.1

Hi , I have some problem with my first Blynk project and I am beginner to Arduino coding. It is about a water level indicator. When I click the switch button from OFF to ON in Blynk app, the software will automatically detect the water level using the ultrasonic sensor and turn on or off the relay(water pump) ,depending on my maximum or minimum level set.

Now the problem is when I click the switch button from OFF to ON or ON to OFF, the relay sometimes does not respond to my switch button statement and the I2C from the previous condition. I need to click multiple times on the switch button for the relay to turn on or off. The MOTOR(LED) in the Blynk app represent the condition of the relay(water pump).

For example:

  1. I click switch button in Blynk from OFF to ON to turn on the relay. The I2C show " Water Lvl: 70cm" and “Motor is ON”.

  2. I wanted to turn off the relay. I click the switch button in Blynk from ON to OFF. However, the relay does not turn OFF and the I2C remain “Water Lvl: 70cm” and “Motor is ON”. I need to click the switch button multiple times in Blynk to turn off the relay then the I2C will show “System is OFF” and “Check Blynk”.

I think it is wrong with my code or something else? I tried searching the net but to no avail.

I attached my code below. Apologize if my code is a mess as I am new to Arduino programming. Thanks!

Input:
Switch(V0)
Level V(V1)
LED (V2)

#include <Wire.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

WidgetLED led1(V2);
 
char auth[] = "  ";//Enter your Auth token
char ssid[] = "   ";//Enter your WIFI name
char pass[] = "   ";//Enter your WIFI password
 
BlynkTimer timer;
bool pinValue = 1;

long duration;
int distance; 
int lvlmax =5;
int lvlmin =10;
 
#define trig D7
#define echo D8
#define relay D5
 
void setup() {
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(relay, OUTPUT);
  Wire.begin(D2, D1);
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  digitalWrite(relay, LOW);
  
}

BLYNK_CONNECTED(){
  Blynk.syncAll();
  }
 
BLYNK_WRITE(V0) {
  pinValue = param.asInt();
}
 
void loop() {
  Blynk.run();
  timer.run();

  {
  digitalWrite(trig, LOW);
  delayMicroseconds(4);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;
 
  Blynk.virtualWrite(V1, distance);
  Serial.println(distance);
  }
  

 if (pinValue == 1)
    {
       if (distance >= lvlmin)
       {
          digitalWrite(relay, HIGH);
          led1.on();
          lcd.setCursor(0, 0);
          lcd.print("Water Lvl: ");
          lcd.print(distance);
          lcd.print(" CM");
          lcd.setCursor(0,1);
          lcd.print("Motor is ON ");
       } 
       
       if ( distance <= lvlmax) 
       {
          digitalWrite(relay, LOW);
          led1.off(); 
          lcd.setCursor(0, 0);
          lcd.print("Water Lvl: ");
          lcd.print(distance);
          lcd.print(" CM");
          lcd.setCursor(0,1);
          lcd.print("Motor is OFF");
       }

   if (pinValue == 0)
    {
      digitalWrite(relay, LOW);
      led1.off();
      lcd.setCursor(0,0);
      lcd.print("System is OFF   ");
      lcd.setCursor(0,1);
      lcd.print("Check Blynk");
      delay(500);
    }

   }
}

Your Loop is a total mess. Read this !! :point_down:t2:

http://help.blynk.cc/en/articles/2091699-keep-your-void-loop-clean

I am sorry but the link is not working.

May be they have a maintenance going on.
Basically your void loop must not contain any code
Except Blynk.run(); and timer.run():

Delays are totally prohibited !! It blocks the whole code and makes the connection unstable and eventually the device crashes.

So you need to call the desired function with Blynk timers. To avoid delay you can use timeout.

You can find many examples in the forum if you search a bit.