Using Blynk slider as input to logic

Hi

I have a water tank setup with ultrasonic sensor to switch between Pump & backup mains water solenoid when water level gets too low.

I was wanting to use a blynk slider / number input so I could change when it switches.

How do I use the Slider (with a blynk.write(v10)) as an input into some logic to then drive an output which switches a relay (between pump relay and mains solenoid).

2nd query is whether anyone has managed the issues of relays chattering between pump & mains when Level sensor is on/around the cutover point with logic rather then the usual 2 sensor setup?

Tia

I assume you mean BLYNK_WRITE(V10) ?
It’s difficult to provide any specific advice without seeing your sketch, but in general the best way is usually to assign the value from the widget to a global variable, then use this value in an if statement within a function that is called via a timer.
You’ll probably want to have a BLYNK_CONNECTED() function that contains a Blynk.syncVirtual(V10) command to force the latest value for V10 to be retrieved from the server when the device starts/up or re-connects to Blynk.

As far as avoiding chatter is concerned, you need to implement a hysteresis function to de-sensitise your switching function.
This is frequently used in thermostats, to prevent boilers constantly turning on/off when the temperature is hovering around the set point.
A quick search of the forum will throw up some examples.

Pete.

Thanks Peter

I think i have attemtped what you suggested but with no luck. Code still doesnt seem to respond to the slider value.

There is a bit in here, but the relevant bits are

  1. ‘pumpcutout’ set as global variable
  2. blynk_write(20) set to read slider value (i incorrectly said i was using V10 in previous post)
  3. Blynk_connected() includes a blynk.syncAll()
  4. void Watersource() contains the logic to use the slider value to switch between Pump & Mains with a timer.setinterval calling it every 1000ms

Any help would be greatly appreciated & will do some research on thermostats to avoid the bouncing


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Filter.h>

BlynkTimer timer;
char auth[] = "xxxxx";
char ssid[] = "xxxxx";
char pass[] = "xxxxx";

#define BLYNK_VPIN_MAINS_PUMP V1

String displaywifi;
int wifisignal;
boolean Auto_mode;

const int relay = 13;
const int echopin = 12;
const int trigpin = 14;

const float diameter = 140;
const int depth = 194.883603785994;
const int area = PI * ((diameter / 2) * (diameter / 2));

long duration;
int RawDistance;
int constraineddistance;
int depthwater;
int litres;
int percentage;
int weightvalue = 20;
int pumpcutout;

#define BUFFER 5
int data[BUFFER];

ExponentialFilter<float> FilteredDistance(weightvalue, 0);

WidgetLED led1(V2);
WidgetLED led2(V3);

BLYNK_CONNECTED()
{
  Blynk.syncAll();
}

BLYNK_WRITE(V1) {
  switch (param.asInt()) {
    case 1: {
        Auto_mode = true;
        Serial.println(Auto_mode);
        Serial.println("Case 1");
        //watersource;
        break;
      }

    case 2: {
        Auto_mode = false;
        Serial.println(Auto_mode);
        Serial.println("Case 2");
        digitalWrite(relay, HIGH);
        led1.on();
        led2.off();
        break;
      }
    case 3: {
        Auto_mode = false;
        Serial.println(Auto_mode);
        Serial.println("Case 3");
        digitalWrite(relay, LOW);
        led1.off();
        led2.on();
        break;
      }
  }
}

BLYNK_WRITE(V10) {
  int weightvalue = param.asInt();
}

//NEW Section~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
BLYNK_WRITE(V20) {
  int pumpcutout = param.asInt();
  //Serial.print("Pump Cut Out = ");
  //Serial.println(pumpcutout);
}

//NEW Section End~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void setup()
{
  Blynk.begin(auth, ssid, pass);
  pinMode(trigpin, OUTPUT);
  pinMode(echopin, INPUT);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);

  digitalWrite(trigpin, LOW);
  delayMicroseconds(100);
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(100);
  digitalWrite(trigpin, LOW);

  int n = 0;
  while (n < BUFFER) {
    data[n] = ((pulseIn(echopin, HIGH)) * .034 / 2);
    n++;
  }

  timer.setInterval(1000L, sendlevel);
  timer.setInterval(5000L, sendwifi);
  timer.setInterval(1000L, watersource);

  Serial.begin(9600);
}

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

void sendwifi()
{
  wifisignal = map(WiFi.RSSI(), -105, -40, 0, 100);
  displaywifi = String("                           Signal:  ") + wifisignal + String(" %");
  Blynk.setProperty(V5, "label", displaywifi);
}

void sendlevel()
{
  digitalWrite(trigpin, LOW);
  delayMicroseconds(100);
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(100);
  digitalWrite(trigpin, LOW);

  float datasum = 0;
  float diffsum = 0;
  for (int n = 0; n < BUFFER; n++) {
    datasum += data[n];
  }
  float mean = float(datasum / BUFFER);
  for (int n = 0; n < BUFFER; n++) {
    diffsum += ((data[n] - mean) * (data[n] - mean));
  }
  float sd = sqrt(diffsum / (BUFFER - 1));

  if (sd != 0) {
    float newSum = 0;
    float newLen = 0;
    int n = 0;
    while (n < BUFFER) {
      if ((data[n] < (mean + sd)) and (data[n] > (mean - sd))) {
        newSum += data[n];
        newLen ++;
      }
      n++;
    }
    mean = newSum / newLen;
  }

  for (int n = (BUFFER - 1); n > 0; n--) {
    data[n] = data [n - 1];
  }
  data[0] = pulseIn(echopin, HIGH);

  RawDistance = mean * 0.034 / 2;
  
  float constraineddistance = constrain(RawDistance, 18, depth);
  FilteredDistance.Filter(constraineddistance);
  float SmoothDistance = FilteredDistance.Current();

  depthwater = depth - SmoothDistance;
  litres = 2 * (area * depthwater) / 1000;
  percentage = 100 - ((SmoothDistance) / depth);

  Serial.print("Distance = ");
  Serial.print(constraineddistance);
  Serial.println(" cm");

  Serial.print("Litres = ");
  Serial.print(litres);
  Serial.println(" L");
 
  Blynk.virtualWrite(V4, RawDistance);
  Blynk.virtualWrite(V5, constraineddistance);
  Blynk.virtualWrite(V8, SmoothDistance);

  Blynk.virtualWrite(V6, depthwater);
  Blynk.virtualWrite(V7, litres);
}
void watersource()
{
  if (Auto_mode) {
    if (litres < pumpcutout) {    //NEW modified from 1000 --> pumpcutout
      Serial.print("Pump Cut Out = ");
      Serial.println(pumpcutout);
      
      digitalWrite(relay, HIGH);
      led1.on();
      led2.off();
      Serial.println("MAINS");
    }

    if (litres >= pumpcutout) {       //NEW modified from 1000 --> pumpcutout
      
      Serial.print("Pump Cut Out = ");
      Serial.println(pumpcutout);
      
      
      digitalWrite(relay, LOW);
      led1.off();
      led2.on();
      Serial.println("PUMP");
    }
  }
}

@Bobbyh88 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:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

1 Like

You’ve declared it globally…

But then re-declared it locally…

If you changed this to…

BLYNK_WRITE(V20) {
  pumpcutout = param.asInt(); <<< Local Re-declaration 

then the value coming from the V20 widget would be stored to the global variable and be accessible in other functions.

Pete.

As always Thankyou Peter

Cheers

1 Like