Automated Relay Trigger with Switch

Hi,

i want to do a Watering System with my Arduino Esp32 and the System should automatically trigger a relay to give the plant water, when the soil-humidity is below a special value, defined with a slider.
With a Switch the User should be able to turn of the System and when the Switch is ON the automated Watering-System should work.

For that i created the following code:

BLYNK_WRITE(V1)
{
  output_value = analogRead(soil_sensor);
  moisturelevel = 100 - map(output_value,1600,3400, 0, 100);

  if((param.asInt() == 1))
  {
    if(moisturelevel <= SliderValue)
      digitalWrite(26,HIGH);  // Set digital pin 26 HIGH
    else
      digitalWrite(26,LOW);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    digitalWrite(26,LOW);  // Set digital pin 26 LOW    
  }
}

BLYNK_WRITE(V0)
{
  //reads the slider value when it changes in the app
  SliderValue = param.asInt();
}

The problem with this code is, that the Blynk_Write only triggers, when the value of the Switch changes. But when the Switch is on and the SliderValue is greater than the actual SoilMoisture the relay keeps on. If this happens the relay should go off and when the SoilMoisture is again smaller than the SliderValue it should go on again.

Thanks in advance

Yes, that’s how BLYNK_WRITE works.

The usual approach is to save the incoming value from the slider (obtained from the param.asInt() line of code) to a global variable, then use BlynkTimer to call a function on a regular basis which compares the moisture reading and slider value and takes the appropriate action.

Pete.