Trying to use Blynk slider to control water pump

Hello, I'm trying to create a project using ultrasonic sensor and 5v water pump. I'm trying to create manual and automatic option for when the water pump will be turned ON. For the automatic process there is no problem. unfortunately for the manual process, which is using Blynk slider, did not seem to be working for me. Do you guys have any advice for it?


#define BLYNK_TEMPLATE_ID "TMPL6Lv2tL2GV"
#define BLYNK_TEMPLATE_NAME "FYP"
#define BLYNK_AUTH_TOKEN "GF0KuUtvkXScV-Q0OYfRa3T--ZRt37Qy"



#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>


#define ESPADC 4096.0   //the esp Analog Digital Convertion value
#define ESPVOLTAGE 3300 //the esp voltage supply value
#define PH_PIN 34
#define BLYNK_PRINT Serial
#define water 21    //Pin for water pump at percentage> 100
#define dosing 33   // pin for dosing pH
#include "DFRobot_ESP_PH.h"
#include "EEPROM.h"
DFRobot_ESP_PH ph;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "MUZESOLAR";  // Replace with your WiFi SSID
char pass[] = "syedhensem";            // Replace with your WiFi password

void ultra();
float voltage, phValue, temperature = 25;
float temp = 5;           //sensor temp
const int trigPin = 22;
const int echoPin = 23;
long duration;
long distance;
int percentage;
float initial_distance = 32;
float surface_area = 576;
float height;
float vol;
float litres;
float tempC;
int tur1 = 19;
int tur2 = 15;
int slider;





unsigned long dosingStartTime = 0;
unsigned long dosingDuration = 15000;  // 8 hours in milliseconds

OneWire oneWire(temp);            //for temp
DallasTemperature DS18B20(&oneWire);    //for temp

void setup()
 {
  Serial.begin(115200);
  EEPROM.begin(32);//needed to permit storage of calibration value in eeprom
  Serial.println("Starting setup...");
	ph.begin();
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(water, OUTPUT);
  pinMode(tur1, INPUT);
  pinMode(tur2, INPUT);
  DS18B20.begin();    // initialize the DS18B20 sensor

   Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

   Serial.println("Setup complete.");

   delay(100);
  }

void loop() 
  {
  Blynk.run();
  //timer.run();
  ultra();
  tempoo();
  measure_ph();
  turb();

 
  }


// This is the code for my slider in blynk
BLYNK_WRITE(V8){
  slider = param[0].asInt();
}


// below is my ultrasonic sensor code.

void ultra() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  percentage = map(distance, 30.5, 15, 0, 100);
  height = initial_distance - distance;
  vol = height * surface_area;
  litres = vol / 1000;
//  percentage = slider;

 // Serial.println(distance);
 // Serial.println(slider);
 

  Blynk.virtualWrite(V2, percentage); // Display percentage in Blynk app (use Gauge widget on V2)
  Blynk.virtualWrite(V3, litres);     // Display litres in Blynk app (use Gauge widget on V3)


  if (slider < percentage)
  {
    digitalWrite(water, HIGH);
  }



   if (percentage > 100)
    {
   // Serial.println("pumping out water");
    Blynk.logEvent("Overflow", "Pumping water out ");
    digitalWrite(water, HIGH);

    } 
  else if (percentage > 60)
    {
    //Serial.println("ok");
 //   Blynk.logEvent("W1", " Water good Percentage: " + String(percentage));
    digitalWrite(water, LOW);
    } 
  else if (percentage > 40)
   {
    //Serial.println("water deplete");
    Blynk.logEvent("W2", " Water depleting Percentage: " + String(percentage));
    digitalWrite(water, LOW);
   }
   else if (percentage <= 39) 
   {
    //Serial.println("water low");
    Blynk.logEvent("W3", " Water is LOW Percentage: " + String(percentage));
    digitalWrite(water, LOW);
   }
  



}


void tempoo()
{
  DS18B20.requestTemperatures();       // send the command to get temperatures
  tempC = DS18B20.getTempCByIndex(0);  // read temperature in °C
  Blynk.virtualWrite(V1, tempC); // 
 // Serial.println(tempC);

  if (tempC > 35 || tempC < 19) 
  {
    // Temperature is either greater than 30 or less than 19
    Blynk.logEvent("Temperature_Issue", "Temperature is out of range: " + String(tempC));
  }

}

void measure_ph()
{
    voltage = analogRead(PH_PIN) / ESPADC * ESPVOLTAGE; // read the voltage
    phValue = ph.readPH(voltage, temperature);          // convert voltage to pH with temperature compensation
    
    Serial.print("pH:");
    Serial.println(phValue, 4);

    Blynk.virtualWrite(V4, phValue);

    // ph.calibration(voltage, temperature); // calibration process by Serial CMD

    // Check if 8 hours have passed since the last dosing
    if (millis() - dosingStartTime >= dosingDuration)
    {
        // pH checking is only allowed after 8 hours
        if (phValue < 6.9)
        {
            // pH is less than 6.9
            if (dosingStartTime == 0)
            {
                // Dosing pump is not already on, turn it on
                digitalWrite(dosing, HIGH);
                // Add your code to control the dosing pump here
                delay(7500);
                digitalWrite(dosing, LOW);

                // Record the start time of dosing
                dosingStartTime = millis();
            }
        }
    }

    // Check if the dosing pump has been on for an 8-hour interval
    if (dosingStartTime > 0 && millis() - dosingStartTime >= dosingDuration)
    {
        // Dosing duration has passed
        // Add your code to turn off the dosing pump or perform other actions here

        // Reset the dosing start time to allow for the next triggering
        dosingStartTime = 0;
    }
    // Optional: Add an else statement to handle other conditions if needed
}




void turb()
  {
  if(digitalRead(tur1)==LOW)
    {       //read sensor signal
 //   digitalWrite(ledPin, HIGH);   // if sensor is LOW, then turn on
  //    Serial.println("keruh");
      WidgetLED led1(5); //means you need to have LED on app configured on virtual pin 1.
      led1.on();
    }
  if(digitalRead(tur2)==LOW)
    {
//    digitalWrite(ledPin, LOW);    // if sensor is HIGH, then turn off the led
  //    Serial.println("keruh");
      WidgetLED led2(6); //means you need to have LED on app configured on virtual pin 1.
      led2.on();
      
    }
  
  if(digitalRead(tur1)==HIGH)
  {
   // Serial.println("do nothing");
    WidgetLED led1(5); //means you need to have LED on app configured on virtual pin 1.
      led1.off();
  }

  if(digitalRead(tur2)==HIGH)
  {
  //  Serial.println("do nothing");
    WidgetLED led2(6); //means you need to have LED on app configured on virtual pin 1.
      led2.off();
  }
    
  }

What are you expecting and what did you get?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.