How to turn on pump for long time then turn off ??

How can I stop LOOP in this if function

I know the lambda timer works. But as I said in my posting the way you written your code (no disrespect) a mess. There could be a number of things wrong. Firstly why do you a function to declare your the pin settings? Why is this not done in the setup? If you are using Blink your void loop must be totally clean. There is quite a lot on this issue in the Blynk forum. Now back to the lambda timer the way I would implement it

//Global Declaration
int Btn;


void setup()
{
 timer.setInterval(500L, PumpFunction);
}

void PumpFunction()
{
 if (Btn == 1)  //button on Blynk is on
 {
  digitalWrite(Pump, HIGH);
  timer.setTimeout(5000L, []() //5 secs on
  {
    digitalWrite(Pump, LOW);
    Blynk.virtualWrite(Btn, 0);  //turn button on Blynk off
   });
 }
}

BLYNK_WRITE(V1)
{
 Btn = param.asInt();
 }

I apologize again for not being able to explain the problem
I have sent an image to show the idea of the program to be implemented
I sent full code >>>the problem in Page call "Program"

want to digitalWrite ---> PUMP_PIN ----> HIGH for example 10 mins and then turn off the pump when moisture_percentage < DRY_SOIL
Then the pump will work for an example 10 mins and moisture will become : moisture_percentage > DRY_SOIL In this case I want to keep the pump closed
and I want when moistre become againe : moisture_percentage < DRY_SOIL I want the pump to run again for the previously set time then turn off
but when use millis or simple timer the pump diddn;t turn off because this function in LOOP
How can I solve this problem?

#include <SPIFFS.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <DNSServer.h>
#include <WebServer.h>
#include <WiFiManager.h>
#include <ArduinoJson.h>
#include <BlynkSimpleEsp32.h>          //  Blynk_Release_v0.6.1 
#include "stationDefines.h"       // Project definitions DHT
/* OLED */
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <dht.h>  /* DHT11*/

#include <EasyButton.h>

Adafruit_SSD1306 display(1, 3, 23, 22, 60);
WiFiManager wifiManager;
dht DHT;
EasyButton button(oled_AP);

void setup()
{
  startTimers();     //collect data from sensors
  oledStart();      
  timer.setInterval(1000, displayData);   
  pin_Mode();
  timer.setTimeout(1000, Config_WiFi);
  display.ssd1306_command(SSD1306_DISPLAYOFF);
  button.onPressedFor(5000, WiFi_initialization);
}
void loop()
{
  if (Blynk.connected())
  {
    Blynk.run();
    WiFi_control();          //when connected to Blynk we should chose if we want to run device auto by sensors or manual by buttons in Blynk APP
    digitalWrite(Green_LED_PIN, HIGH);    //when connected to blynk green led on
  }
  else
  {
    autoControlPlantation();    //when disconnected run device auto by sensors
    digitalWrite(Green_LED_PIN, LOW);    //when connected to blynk green led off
  }
  timer.run();//Give time of Blynk work
  feedback();
  button.read();
  sleepOLED();
}
void pin_Mode()
{
  pinMode(PUMP_PIN, OUTPUT);
  pinMode(FAN_PIN, OUTPUT);
  pinMode(LAMP_PIN, OUTPUT);
  pinMode(Humidity_Reduction_Device_PIN, OUTPUT);
  pinMode(Green_LED_PIN, OUTPUT);
  pinMode(PUMP_Feedback, INPUT_PULLUP);
  pinMode(FAN_Feedback, INPUT_PULLUP);
  pinMode(LAMP_Feedback, INPUT_PULLUP);
  pinMode(LAMP_Feedback, INPUT_PULLUP);
  pinMode(Humidity_Reduction_Device_Feedback, INPUT_PULLUP);
  pinMode(oled_AP, INPUT_PULLUP);
}
///////////////////////////////////////////////////////////////////////////////////////////////
/***************************************************
  Choose between automatic and manual control through Blynk button
 **************************************************/
void WiFi_control()
{
  if (Auto == 0)
  {
    autoControlPlantation();
  }
  else
  {
    manual_Blynk();
  }
}
/***************************************************
   control from Blynk buttons
 **************************************************/
void manual_Blynk()
{
  if (pumpo == 1)
  {
    digitalWrite(PUMP_PIN, HIGH);
  }
  if (pumpo == 0)
  {
    digitalWrite(PUMP_PIN, LOW);
  }
  ////////////////////////////////
  if (fano == 1)
  {

    digitalWrite(FAN_PIN, HIGH);
  }
  else
  {
    digitalWrite(FAN_PIN, LOW);
  }
  /////////////////////////////////
  if (Lampo == 1)
  {
    digitalWrite(LAMP_PIN, HIGH);
  }
  else
  {
    digitalWrite(LAMP_PIN, LOW);
  }
  ////////////////////////////////
  if (Humidity_Reduction_Device_o == 1)
  {
    digitalWrite(Humidity_Reduction_Device_PIN, HIGH);
  }
  else
  {
    digitalWrite(Humidity_Reduction_Device_PIN, LOW);
  }
}



/***********************************************
  Blynk Buttons definition
***********************************************/
BLYNK_WRITE(V2) //select Auto/Manual
{
  Auto = param.asInt();
}
/////////////////////////////////////////////////
BLYNK_WRITE(V3) //Pump remote control
{
  pumpo = param.asInt();
}
/////////////////////////////////////////////////

BLYNK_WRITE(V4) // FAN remote control
{
  fano = param.asInt();
}
//////////////////////////////////////////////////

BLYNK_WRITE(V17) // LAMP remote control
{
  Lampo = param.asInt();
}
//////////////////////////////////////////////////

BLYNK_WRITE(V6) // LAMP remote control
{
  Humidity_Reduction_Device_o = param.asInt();
}



/***************************************************
  Automatically Control the Plantation based on sensors reading
****************************************************/
void autoControlPlantation(void)
{
  if (moisture_percentage < DRY_SOIL)
  {
  // want to  digitalWrite ---> PUMP_PIN ---->  HIGH  for example 10 mins and then turn off the pump  when moisture_percentage < DRY_SOIL 
  //Then the pump will work for an example 10 mins and moisture  will become  :  moisture_percentage > DRY_SOIL  In this case I want to keep the pump closed
  //and I want when moistre become againe :   moisture_percentage < DRY_SOIL I want the pump to run again for the previously set time then turn off
  //but when use millis or simple timer the pump diddn;t turn off because this function in LOOP
  //How can I solve this problem?
  }
  if (DHT.temperature > HOT_TEMP)
  {
    digitalWrite(FAN_PIN, HIGH);
  }

  if (DHT.temperature < COLD_TEMP)
  {
    digitalWrite(FAN_PIN, LOW);
  }

  if (LDRValue == LOW)
  {
    digitalWrite(LAMP_PIN, HIGH);
  }

  else
  {
    digitalWrite(LAMP_PIN, LOW);
  }
  if (DHT.humidity > HIGH_HUM)
  {
    digitalWrite(Humidity_Reduction_Device_PIN, HIGH);
  }

  if (DHT.humidity < LOW_HUM)
  {
    digitalWrite(Humidity_Reduction_Device_PIN, LOW);
  }
}
/***************************************************
   Send data to Blynk
 **************************************************/
void sendUptime()
{
  Blynk.virtualWrite(10, DHT.temperature); //virtual pin V10
  Blynk.virtualWrite(11, DHT.humidity); // virtual pin V11
  Blynk.virtualWrite(12, moisture_percentage); // virtual pin V12
}


void Initially_LOW() {
  digitalWrite(PUMP_PIN, LOW);
  digitalWrite(FAN_PIN, LOW);
  digitalWrite(LAMP_PIN, LOW);
  digitalWrite(Humidity_Reduction_Device_PIN, LOW);
}

Plant_Station_Q.zip (129 KB)

sounds like what you may really want

    if (! pumpOn && moisture_percentage < DRY_SOIL)
    {
        digitalWrite(Green_LED_PIN, HIGH);  // turn pump on
        pumpOn = 1;
    }
    else if (pumpOn && moisture_percentage > DRY_SOIL)
        digitalWrite(Green_LED_PIN, LOW);
        pumpOn = 0;
    }

if you turn the pump off after a set time and moisture_percentage < DRY_SOIL, won't the pump get turned right back on?

if you turn the pump off after a set time and moisture_percentage < DRY_SOIL, won't the pump get turned right back on?

yes I want
I tried the code the pump (that i replaced with green led)
It kept working constantly and didn't respond to the change in the condition

does this make sense?

    unsigned long curMillis = millis();

    if (pumpOn && curMillis - prevMillis > interval)  {
        digitalWrite(Green_LED_PIN, LOW);   // turn pump off
        pumpOn = 0;
    }

    if (! pumpOn && moisture_percentage < DRY_SOIL)
    {
        digitalWrite(Green_LED_PIN, HIGH);  // turn pump on
        pumpOn = 1;
        prevMillis = millis();
    }

does this make sense?

First of all, thank you very much for your help
I tried the code and the result was:
the led was turned off in the beginning for interval time then the led turned on To infinity
,when moisture_percentage > DRY_SOIL the LED is constantly turned off
and when again moisture_percentage < DRY_SOIL The LED works continuously directly
without delaying

Post your code.

My code + The image containing the program map
The problem is on the page called Program
want to: digitalWrite ---> PUMP_PIN ----> HIGH for example 10 mins and then turn off the pump when moisture_percentage < DRY_SOIL
Then the pump will work for an example 10 mins and moisture will become : moisture_percentage > DRY_SOIL In this case I want to keep the pump closed
and I want when moistre become againe : moisture_percentage < DRY_SOIL I want the pump to run again for the previously set time then turn off
but when use millis or simple timer the pump diddn;t turn off because this function in LOOP
How can I solve this problem?

Plant_Station_Q.zip (129 KB)

The problem as I see it is first you say turn on pump for 10 minutes until moisture percent < dry soil. What if the moisture percent < dry soil is achieved in less than 10 minutes?

Do you not mean turn on pump for 10 minutes then after 10minutes compare the moisture percent?

Or leave out the timing all together and control the pump by the moisture percent only

Proietti:
The problem as I see it is first you say turn on pump for 10 minutes until moisture percent < dry soil. What if the moisture percent < dry soil is achieved in less than 10 minutes?

I do not want it to take care of the sensor except to open the pump only
-I cannot do without timer because when the conditions for opening the pump are met, it will work and if some little water comes, it will give a signal that the irrigation has been completed even though in fact it is not completed
I hope a solution to this problem

So let me get this straight so that I understand what you want

Step 1
Pump comes for 10 minutes

Step 2
Sensor checks moisture, if moisture condition not met repeat Step 1, if moisture condition is ok turn off pump

Step 3
??? How do we get back to Step 1?

if moisture condition not met repeat Step 1

I don't mean this ... After 10 minutes, I want the pump to turn off whatever(The condition must change - I will adjust this in practice)
And I want this to be repeated ... That is, after 10 minutes (the condition must become unmet) I want at When the condition is met again, the pump runs for 10 minutes and then turn off And so on

I've been reading this thread off and on over the last few days, and have now just read it top to bottom twice, and not sure I understand the requirement

I second this.

i thought the desired behavior is

    if pump is on for >= 10 minutes
         turn pump off

    if (soil is dry)
         turn pump on and start timer

the above implies that the pump is turned on for a minimum of 10 minutes and may turn back on immediately after being turned off

if pump is on for >= 10 minutes
if soil is wet
turn pump off

if (soil is dry)
turn pump on and start timer

Sorry but I really can't write this code and hope for help

do you see how people are trying to describe what we think you want to do using pseudo code that doesn't go into details? can you try writing a pseudo code description of what you want to do?

For us to be able to help you we need to know exactly what it is you want. You must give a clear concise explanation step by step what you want