Need Help! Delay code for the water automation project

I hope everyone is doing well, I’ve been working on a water automation project and kinda stuck with the code.

Here what it’s suppose to do.

When the underground water storage start filling up, then send a notification ( i figured that part out) and then wait for a minute or two, activate the relay and when water reaches below the threshold then deactivate the relay. The Ultrasonic sensor puts out the value too fast and I really don’t want the relay flickering due to close values.

Here is the code…

#define TRIGGER 12
#define ECHO    13

// NodeMCU Pin D6 > TRIGGER | Pin D7 > ECHO

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxx";
char pass[] = "xxxxxxxxxxx";
void setup() {
  
  Serial.begin (9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BUILTIN_LED, OUTPUT);
  
}

SimpleTimer timer;
WidgetBridge bridge1(V6);

BLYNK_CONNECTED() {
  // Place the AuthToken of the second hardware here
  bridge1.setAuthToken("f7c7c7060df44ad1aa9a5a0c4cd6c5a5"); 
}


void loop() {
  
  long duration, distance;
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(5); 
  
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(20); 
  
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration/2) / 29.1;

   if (distance <= 150) {
    Blynk.virtualWrite(V0, 255);
}
  else {
    Blynk.virtualWrite(V0, 0);
  }

 if (distance <= 100) {
    Blynk.virtualWrite(V1, 255);
}
  else {
    Blynk.virtualWrite(V1, 0);
  }

   if (distance <= 80) {
    Blynk.virtualWrite(V2, 255);
}
  else {
    Blynk.virtualWrite(V2, 0);
  }

   if (distance <= 40) {
    Blynk.virtualWrite(V3, 255);
}
  else {
    Blynk.virtualWrite(V3, 0);
  }

   if (distance <= 20) {
    Blynk.virtualWrite(V4, 255);
}
  else {
    Blynk.virtualWrite(V4, 0);
  }

  
  
  Serial.print(distance);
  Serial.println("Centimeter:");
  Blynk.virtualWrite(V10, distance);
  bridge1.virtualWrite(V8, distance);

   if(distance > 28){
     bridge1.digitalWrite(5, 1000);
      Blynk.notify("hey , Water Is Comin , Yayy");
    }else{
     bridge1.digitalWrite(5, 0);
     
  }
  delay(500);
  Blynk.run();
}

Here is the scenario,

If Distance is 200cms send a notification, wait for 2 mins so that water fills up, when Distance is 160cms = activate the relay, when Distance reaches 200cms deactivate the relay.

All I want is a smooth switching of relay because it’s connected to a water pump and flickering could damage the pump.

I’m also using the bridge widget to communicate with the relay, ignore that part in the code.

Any help is appreciated :slight_smile:

Another prize-winning Blynk unfriendly void loop!

Do a bit of research about how to put the code from your void loop into a function and how to use a timer to call the function.

Pete.

1 Like

Well, I just started working with Arduino and IOT.

Okay, the main thing to realise with Blynk is that you need a very clean void loop, with just Blynk.run and timer.run.
This allows Blynk to function correctly and makes the Blynk magic happen.

Delays, even in functions that are called using timers generally don’t work with Blynk, unless they are VERY short delays.

In your situation, I’d think about how often you really need to check the water level in your tank. Would once every minute work? If so then set-up a timer to check the level every minute and take the appropriate action.

Pete.

3 Likes

Thanks for the suggestion, I guess I will have to do a bit more research on that :slight_smile:

1 Like

And PLEASE, format your code, before posting…

I ended up doing that for the OP… as is my apparent calling in life :stuck_out_tongue:

Blynk - FTFC

1 Like

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-display-any-sensor-data-in-blynk-app

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-control-anything-with-blynk-app