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

Hi,
I want to turn on pump for long time (like 20 minutes) then turn off the pump and only works if the condition is met again without using delay() function

void autoControlPlantation(void)
{
  if (moisture_percentage < DRY_SOIL)
  {
   //want to turn on pump for long time (like 20 minutes) then turn off the pump and only works if the condition is met again
  }

  void WiFi_control()
{
  if (Auto == 0)  //Blynk button To decide whether the device will operate automatically with sensors, or it will work manually with the Blynk buttons when device online
  {
    autoControlPlantation();
  }
  else
  {
    manual_Blynk();
  }
}
void loop()
{
  if (Blynk.connected())
  {
    Blynk.run();
    WiFi_control();
  }
  else
  {
    autoControlPlantation(); //If the device does not connect to the Internet, it will work automatically
  }

use millis() to read the timeOn and then continue to monitor the time using millis() until the current - timeOn is 20 mins (20 * 60 * 1000 msec). use unsigned long variables

gcjr:
use millis() to read the timeOn and then continue to monitor the time using millis() until the current - timeOn is 20 mins (20 * 60 * 1000 msec). use unsigned long variables

I'm sorry but i didn't know how to use millis() correctly I tried to use it but I failed and didn't achieve the desired goal

abdallah10296:
I'm sorry but i didn't know how to use millis() correctly I tried to use it but I failed and didn't achieve the desired goal

But you didn't post that code, so how are we supposed to help, other than pointing you at Blink Without Delay?

Did you write 20 minutes like this "20 * 60 * 1000", because if so, write it "20UL * 60UL * 1000UL"

Please post your code which tried to use millis() and failed. Explain exactly what it did do that was wrong. Then we can help you to get it right.

Steve

using millis but didn't work??

void autoControlPlantation(void)
{
  if (moisture_percentage < DRY_SOIL)
  {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= interval) {
      digitalWrite(Green_LED_PIN, HIGH);
      previousMillis = currentMillis;
    }
  }

  else {
    
    digitalWrite(Green_LED_PIN, LOW);
  }

Your code is incomplete.

That's why it doesn't work - it doesn't compile

void setup()
{
  pinMode(PUMP_PIN, OUTPUT);
}

void loop()
{
  if (Blynk.connected())
  {
    Blynk.run();
    WiFi_control();
  }
  else
  {
    autoControlPlantation(); //If the device does not connect to the Internet, it will work automatically
  }
}


void autoControlPlantation(void)
{
  if (moisture_percentage < DRY_SOIL) //want to turn on pump for long time (like 20 minutes) then turn off the pump and only works if the condition is met again
  {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= interval) {
      digitalWrite(Green_LED_PIN, HIGH);
      previousMillis = currentMillis;
    }
    else {
      digitalWrite(Green_LED_PIN, LOW);
    }
  }

}

void WiFi_control()
{
  if (Auto == 0)  //Blynk button To decide whether the device will operate automatically with sensors, or it will work manually with the Blynk buttons
  {
    autoControlPlantation();
  }
  else
  {
    manual_Blynk();
  }
}

Your code is incomplete.

I think I may have mentioned this.

I'm sorry but code long and contain from more page
i will send it zip file

Plant_q.zip (5.39 KB)

this isn't correct, the else condition should be removed

   if (currentMillis - previousMillis >= interval) {
      digitalWrite(PUMP_PIN, HIGH);
    }

    else {
      previousMillis = currentMillis;
      digitalWrite(PUMP_PIN, LOW);

    }

if you update previousMillis whenever the interval isn't reached, you'll never reach the interval.

previousMillis should be updated when the pump is turned on -- digitalWrite(PUMP_PIN, LOW) and the test above for turning it off only needs to be done when the pump is on

I am really not good at programming, but that's what I applied

 if (moisture_percentage < DRY_SOIL)
  {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      digitalWrite(PUMP_PIN, HIGH);
      
    }
  }

The pump is running continuously and didn't not closed

sometimes even the simplest code doesn't work because of some simple mistake or assumption. When this happens to me, and it happens often enough, i start verifying those assumptions. I often make the dumbest mistakes because i'm focused on the more complicated code. (i've learned to find and correct those mistakes quickly)

i typically rely on using the Serial interface to get debug prints from my code. lacking that I would use LED(s). If you can use Serial, I would add Serial.println() to make sure

  • autoControlPlantation() is being invoked,
  • moisture_percentage is < DRY_SOIL
  • verify that interval is what you expect it to be
  • verify that current - previous > interval

if you can't use Serial, i would use an LED to indicate that some piece of code is being invoked

i also don't see where previousMillis() is being set. It should be set where the pump is being turned on.

void loop() {
  if(the pump is currently off) {
    if(the pump should be on now) {
      turn the pump on();
    }
  }
  else {
    if(the pump shoul be off now) {
      turn the pump off();
    }
  }
}

I have had a look at your sketch and I use Blynk for all my projects, but I the way you have implemented it is very complicated. There are a number of things you are doing which do not make any sense to me. But since you are using Blynk I am assuming you are using the Blynk.timer so in order to an action then do another you could use a lambda timer

Turn pump on
     timer.setTimeout(5000L, []()
    {
      Turn pump off
    });

I tried the last code, and it actually work, and it stopped the pump when the stopping condition was fulfilled, but the problem is that when the working condition is met, it works for the required period, and instead that the pump stops after that, it continues to work without stopping I think it is because of the code in loop How can I stop LOOP in this if function

if (moisture_percentage < DRY_SOIL)
  {
    digitalWrite(PUMP_PIN, HIGH);
    timer.setTimeout(5000L, []()
    {
      digitalWrite(PUMP_PIN, LOW);
    });
  }

That code would never compile.

aarg:
That code would never compile.

Damn right.

The file containing the code is compressed because it consists of more than one page, and the problem is found on the page called Program

Plant_Station_Q.zip (6.21 KB)

i took the older version of you code, Plant_q, commented out and #if 0'd most the code that I can't compile because i don't have your libraries, added some Serial prints and modified some of the code to demonstrate how to use millis() to execute digitalWrite(PUMP_PIN, HIGH) in autoControlPlantation(). see attached zip

int pumpOn = 1;

void autoControlPlantation(void)
{
    char s[200];

  unsigned long currentMillis = millis();

    sprintf (s, " %s: cur %6ld, prev %6ld,  pumpOn %d",
                __func__, currentMillis, previousMillis, pumpOn);
    Serial.println (s);

  if (moisture_percentage < DRY_SOIL)
  {
    if (pumpOn && currentMillis - previousMillis >= interval) {
      digitalWrite(PUMP_PIN, HIGH);
      pumpOn = 0;
    }

    else {
#if 0
      previousMillis = currentMillis;
#endif
      digitalWrite(PUMP_PIN, LOW);
    }
  }

here is the associated output

Plant_q
 autoControlPlantation: cur      0, prev      0,  pumpOn 1
 autoControlPlantation: cur    503, prev      0,  pumpOn 1
 autoControlPlantation: cur   1004, prev      0,  pumpOn 1
 autoControlPlantation: cur   1505, prev      0,  pumpOn 1
 autoControlPlantation: cur   2006, prev      0,  pumpOn 1
 autoControlPlantation: cur   2506, prev      0,  pumpOn 1
 autoControlPlantation: cur   3008, prev      0,  pumpOn 1
 autoControlPlantation: cur   3509, prev      0,  pumpOn 1
 autoControlPlantation: cur   4009, prev      0,  pumpOn 1
 autoControlPlantation: cur   4510, prev      0,  pumpOn 1
 autoControlPlantation: cur   5011, prev      0,  pumpOn 1
 autoControlPlantation: cur   5512, prev      0,  pumpOn 1
 autoControlPlantation: cur   6012, prev      0,  pumpOn 1
 autoControlPlantation: cur   6513, prev      0,  pumpOn 1
 autoControlPlantation: cur   7015, prev      0,  pumpOn 1
 autoControlPlantation: cur   7516, prev      0,  pumpOn 1
 autoControlPlantation: cur   8016, prev      0,  pumpOn 1
 autoControlPlantation: cur   8517, prev      0,  pumpOn 1
 autoControlPlantation: cur   9018, prev      0,  pumpOn 1
 autoControlPlantation: cur   9519, prev      0,  pumpOn 1
 autoControlPlantation: cur  10019, prev      0,  pumpOn 1
 autoControlPlantation: cur  10521, prev      0,  pumpOn 0
 autoControlPlantation: cur  11022, prev      0,  pumpOn 0
 autoControlPlantation: cur  11523, prev      0,  pumpOn 0
 autoControlPlantation: cur  12023, prev      0,  pumpOn 0
 autoControlPlantation: cur  12524, prev      0,  pumpOn 0
 autoControlPlantation: cur  13025, prev      0,  pumpOn 0
 autoControlPlantation: cur  13526, prev      0,  pumpOn 0
 autoControlPlantation: cur  14027, prev      0,  pumpOn 0

Plant_q2.zip (5.74 KB)