New Blynk. Soil Moisture

Timeout timer then…

Pete.

I managed to make that, when the button was pressed, the timeout command worked, as in the example below:

if(value == 1){ //If the button that triggers the relay is pressed
    digitalWrite(rele, HIGH);
    timer.setTimeout(5000L, []() //After 5 seconds the relay is turned off
  {  
    digitalWrite(rele, LOW);
  }); 
  }

But when I use the same command with a slider it doesn’t work. The low example is the part of the code where, if the moisture target is greater than the moisture in the ground, the relay will be turned on and after 5 seconds it is turned off, but the interval part with the timeout command is not executed. Would there be a way to make the timeout work with the slider?

else if(value == 0){ //If the button that triggers the relay is not pressed
  if(humidity > target)//If the soil moisture is greater than the configured minimum value
  { 
    digitalWrite(relay, LOW);
  }  
  if(humidity < target)//If the soil moisture is less than the configured minimum value
  { 
    digitalWrite(relay, HIGH);
    timer.setTimeout(5000L, []() //After 5 seconds the relay is turned off
  {  
    digitalWrite(relay, LOW);
  }); 
  }

You can use variable in the timeout timer, for example

timer.setTimeout(slider_value , []()
digitalWrite(relay, HIGH);
    timer.setTimeout(interval, []() //interval fixed by slider in seconds / the relay is turned off
  {

Actually, my difficulty is that when I put an interval variable to control the relay it doesn’t work. The code below is an if structure for the soil moisture, when the humidity is below the desired value the relay should turn on, stay on for 5 seconds and turn off, but it doesn’t. What can I do to force relay shutdown after 5 seconds?

else if(value == 0){ //If the button that triggers the relay is not pressed
  if(humidity < target)//If the soil moisture is less than the configured minimum value
  { 
    digitalWrite(relay, HIGH);
    timer.setTimeout(5000L, []() //After 5 seconds the relay is turned off
  {  
    digitalWrite(relay, LOW);
  });

Do you have timer.run(); in your void loop?

If yes, then please post your complete sketch.

Pete.

Below is the full code:

#include "BlynkEdgent.h"

#define sensor A0
#define relay D2

int target = 0; //Minimum humidity limit (configured in the app)
int humidity; //Variable that will store moisture
int value = 0; //Variable that will store the value of the button that will trigger the relay




BlynkTimer timer;

BLYNK_WRITE(V0)//Button that activates the relay
{
   value = param.asInt();
}

BLYNK_WRITE(V2)//Slider for setting the target of humidity
{
   target = param.asInt();
  }

void sendData()// Function that sends circuit data to the application
{
  humidity = analogRead(sensor);// Sensor reading

  humidity = map(humidity, 0, 1023, 100, 0); //Remaps the sensor value from 0 - 1023 to 0 - 100
  if(value == 1){ //If the button that triggers the relay is pressed
    digitalWrite(rele, HIGH);
    timer.setTimeout(5000L, []() //After 5 seconds the relay is turned off
  {  
    digitalWrite(rele, LOW);
  }); 
  }
  else if(value == 0){ //If the button that triggers the relay is not pressed
  if(humidity < target)//If the soil moisture is less than the configured minimum value
  { 
    digitalWrite(relay, HIGH);
    timer.setTimeout(5000L, []() //After 5 seconds the relay is turned off
  {  
    digitalWrite(relay, LOW);
  }); 
  } 
  Blynk.virtualWrite(V1, humidity); //"Tells" the app that the value of the variable "moisture" is to be shown in a graph in the app
}}

void setup()
{
  pinMode(sensor, INPUT);//Set the variable "sensor" as input
  pinMode(relay, OUTPUT);//Set "relay" variable as output
  Serial.begin(115200);
  delay(100);

  BlynkEdgent.begin();
  timer.setInterval(100L, sendData);
}

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

rele should be relay

The map command that you are using Les the do what your description says. It inverts the result, so a reading of 0 would be mapped to 100 and a reading of 1023 would be mapped to 0.
I’m not sure if this is what you are expecting, or if it affects your results.

This timer is calling your sendData function ten times per second.
What you probably don’t realise is that the timeout timer is not a blocking function (that’s why we use it) so the sendData fu citing doesn’t simply wait until the timeout timer has completed before returning to the void loop and allowing the function tobe triggered again.
Instead, the state of your widget button is evaluated the first time that sendData is executed, starting one of your timeout timers, then 100ms later the state of the widget button is evaluated again, most likely reversing the state of the relay before the timeout timer is even a fraction of the way through its timeout countdown.

Pete.

1 Like