Variable pump speed using slider widget

I am using ESP2866, IC L293D to control 5V DC Pump. To start the pump i got to condition which is working finely. I have problem to run the pump with varied speed
i want to used widget slider to adjust my pump speed. therefore, i tried to use BLYNK_WRITE(V7) function and digitalWrite(in1, V7); but it doesn’t working.
Is there any idea for coding part to adjust the the pump speed using widget slider?


#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include "DHT.h"
#define DHTTYPE DHT11
#define DHTPIN 2
DHT dht(DHTPIN, DHTTYPE); 

char auth[] = "__";              
char ssid[] = "__";                       
char pass[] = "__";                          

int moisture_pin = A0;
int rain_pin = 4; 
int enA = 14;
int in1 = 12;
int in2 = 13;
int motor_speed = 0;

BLYNK_WRITE(V7)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  analogWrite(in1,pinValue);
  Serial.print("V7 Slider value is: ");
  Serial.println(pinValue);
}

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(moisture_pin, INPUT);
  pinMode(rain_pin, INPUT);
  dht.begin();

  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  Serial.println("Initialising sensor...");
  delay(2000);
}

void loop() {

  float t = dht.readTemperature(); 
  float h = dht.readHumidity();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Blynk.virtualWrite(V1, t);
  Blynk.virtualWrite(V2, h);
  Serial.print("Temperature : ");
  Serial.print(t);
  Serial.println(" Celcius");
  Serial.print("Humidity : ");
  Serial.print(h);
  Serial.println(" %");
  delay(1000);

  //moisture read 
    int output_value = analogRead(moisture_pin);
    output_value = map(output_value,550,0,0,100);
    Serial.print("Moisture : ");
    Serial.print(output_value);
    Serial.println(" %");
    Blynk.virtualWrite(V3, output_value);
    delay(1000);
  // rain read
    int rain_state = digitalRead(rain_pin);
    Serial.print(rain_state);
  delay(100);
  if(rain_state == 1 && output_value <0 ){
    Serial.println("There is no rain now.");
    Blynk.virtualWrite(V4, rain_state);
      digitalWrite(in1, V7);
      digitalWrite(in2, LOW);
      delay(10000);

    Serial.println("Pump is running.");
    delay(10000);
  }else{
    Blynk.virtualWrite(V4, rain_state);
      digitalWrite(in1, LOW);
      digitalWrite(in2, LOW);
    Serial.println("Pump is not running.");
  }    
  delay(1000);
      digitalWrite(in1, LOW);
      delay(1000);
  Blynk.run();
  Serial.println(" ");
  delay(1000);
}

The first thing you need to do is to fix your void loop…

You then need to read-up about variable scope and assign you slider value to a global variable which is used elsewhere in your code, not just once when your widget value changes.

Pete.