Help with Code for Watering plants

Hello all and thanks for looking.

I like so many others are working on a auto plant water system.

I'm using the 4 moisture sensors
4 valves that control the water output
and 1 pump to move the water to the plants.

What I'm stuck with is this:

Under "Sensor1" I check the sensor for moisture level. level under 10%
Then i convert that to a percent and display that percentage.

Now its time to water the plant.

I open the valve to the plant;
start the pump and dump 5 sec of water on the plant

THIS IS WHERE I GET STUCK.

I want to go back and make sure the plant has been watered completely. So I want to check the sensor again after 30 sec and make sure the plant is above 80%.

If not i want to apply more water to the plant and check the level again.

This is for my Bonsai plants. So the trick is to water the fully and them let them go down to very little water. I just dont want to keep added water all the time its not good for them.

Here is the code that includes all 4 moister sensors. If someone could point me in the correct direction to check the level again that would be great.

Please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. Then I will study it. See How to use the Forum

Your extensive use of the delay() function may be the problem because the Arduino can't do anything during a delay(). The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking.

...R

Hello all and thanks for looking.

I like so many others are working on a auto plant water system.

I'm using the 4 moisture sensors
4 valves that control the water output
and 1 pump to move the water to the plants.

What I'm stuck with is this:

Under "Sensor1" I check the sensor for moisture level. level under 10%
Then i convert that to a percent and display that percentage.

Now its time to water the plant.

I open the valve to the plant;
start the pump and dump 5 sec of water on the plant

THIS IS WHERE I GET STUCK.

I want to go back and make sure the plant has been watered completely. So I want to check the sensor again after 30 sec and make sure the plant is above 80%.

If not i want to apply more water to the plant and check the level again.

This is for my Bonsai plants. So the trick is to water the fully and them let them go down to very little water. I just dont want to keep added water all the time its not good for them.

Here is the code that includes all 4 moister sensors. If someone could point me in the correct direction to check the level again that would be great.

/this sketch is part of the auto water system. This code show sesensor connection and display.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sensorValue0=0;
int sensorValue1=0;
int sensorValue2=0;
int sensorValue3=0;
int SoilA0=0;
int SoilA1=0;
int SoilA2=0;
int SoilA3=0;
int RELAY1=LOW;
int RELAY2=LOW;
int RELAY3=LOW;
int RELAY4=LOW;
int RELAY8=LOW;


void setup() {
  lcd.begin(16,2);
  pinMode(RELAY1,OUTPUT); //Moisture Sensor 1,A0
  pinMode(RELAY2,OUTPUT); //Moisture Sensor 2.A1
  pinMode(RELAY3,OUTPUT); //Moisture Sensor 3.A2
  pinMode(RELAY4,OUTPUT); //Moisture Sensor 4.A3
  pinMode(RELAY8,OUTPUT); //Water pump 
  
}

void loop() {


//moved to under the sensor configs
//int sensorValue0=analogRead(A0);   //Int all sensors to Read inputs
//int sensorValue1=analogRead(A1);
//int sensorValue2=analogRead(A2);
//int sensorValue3=analogRead(A3);

//sensorValue0=constrain(sensorValue0, 485, 1023);  //set the range of input value for all pins
//sensorValue1=constrain(sensorValue1, 485, 1023);
//sensorValue2=constrain(sensorValue2, 485, 1023);
//sensorValue3=constrain(sensorValue3, 485, 1023);


//Sensor 0 

int sensorValue0=analogRead(A0);                      //Int sensor and read value
delay(20);                                            //wait for sensor to settel
sensorValue0=constrain(sensorValue0, 485, 1023);      //map sensor value to a number
delay(20);         
SoilA0=map(sensorValue0, 485,1023,100, 0);            //change value to 0-100 for percentage

// set up the LCD's number of columns and rows:
// Print a message to the LCD.
  lcd.setCursor(0,0);
  lcd.print("Soil Moisture_0");
  lcd.setCursor(1,0 );
  lcd.print(SoilA0);
  lcd.print(" %");

if (SoilA0 < 10)

{ 

  digitalWrite(RELAY1,HIGH);
  delay(20);
  digitalWrite(RELAY8,HIGH);
  delay(5000);
  digitalWrite(RELAY8,LOW);
  delay(20);
  digitalWrite(RELAY1,LOW);

    
}
  delay(1000);
  
//Sensor 1

int sensorValue1=analogRead(A1);                      //Int sensor and read value
delay(20);                                            //wait for sensor to settel
sensorValue1=constrain(sensorValue1, 485, 1023);      //map sensor value to a number
delay(20);
SoilA1=map(sensorValue1, 485,1023,100, 0);            //change value to 0-100 for percentage

// set up the LCD's number of columns and rows:
// Print a message to the LCD.
  lcd.setCursor(0,0);
  lcd.print("Soil Moisture_1");
  lcd.setCursor(1,0 );
  lcd.print(SoilA1);
  lcd.print(" %");


if (SoilA1 < 10)

{ 
  digitalWrite(RELAY2,HIGH);
  delay(20);
  digitalWrite(RELAY8,HIGH);
  delay(5000);
  digitalWrite(RELAY8,LOW);
  delay(20);
  digitalWrite(RELAY2,LOW);
  
}
  delay(1000);

//Sensor 2

  int sensorValue2=analogRead(A2);                    //Int sensor and read value
  delay(20);                                          //wait for sensor to settel
  sensorValue2=constrain(sensorValue2, 485, 1023);    //map sensor value to a number
  delay(20);                                          
  SoilA2=map(sensorValue2, 485,1023,100, 0);          //change value to 0-100 for percentage

// set up the LCD's number of columns and rows:
// Print a message to the LCD.
  lcd.setCursor(0,0);
  lcd.print("Soil Moisture_2");
  lcd.setCursor(1,0 );
  lcd.print(SoilA2);
  lcd.print(" %");

if (SoilA2 < 10)

{ 
  digitalWrite(RELAY3,HIGH);
  delay(20);
  digitalWrite(RELAY8,HIGH);
  delay(5000);
  digitalWrite(RELAY8,LOW);
  delay(20);
  digitalWrite(RELAY3,LOW);
  
}
  delay(1000);
  
//Sensor 3

  int sensorValue3=analogRead(A3);                    //Int sensor and read value
  delay(20);                                          //wait for sensor to settel
  sensorValue3=constrain(sensorValue3, 485, 1023);    //map sensor value to a number
  delay(20);//map sensor value to a number
  SoilA3=map(sensorValue3, 485,1023,100, 0);          //change value to 0-100 for percentage

// set up the LCD's number of columns and rows:
// Print a message to the LCD.
  lcd.setCursor(0,0);
  lcd.print("Soil Moisture_3");
  lcd.setCursor(1,0 );
  lcd.print(SoilA3);
  lcd.print(" %");


if (SoilA3 < 10)

{ 
  digitalWrite(RELAY4,HIGH);
  delay(20);
  digitalWrite(RELAY8,HIGH);
  delay(5000);
  digitalWrite(RELAY8,LOW);
  delay(20);
  digitalWrite(RELAY4,LOW);
  
}
  delay(1000);
}

Only 30 seconds ? I would wait 5 minutes after watering and check the soil moisture again.
Those kind of timings can perhaps be done with delay(), but more sophisticated is using the millis() function. The example that Robin2 links to is with millis().

The price of a pump is in the same range as the price of a valve. Perhaps you could have used 4 pumps.
Those pumps should supply only very little water, perhaps a peristaltic pump could be a good choice.

They are between 5 and 20 dollars on Ebay.

Watering them completely at once may take an hour, that is for the plant the same as watering them in a few seconds.

A lot depends on your sensor and the soil. Suppose the sensor is at the top and the bottom is still too wet for the roots. I think it is better to measure the soil moisture at the bottom. If the top soil is very dry, who cares.

You did not need to start a new Thread. I will aske the Moderator to merge with the earlier one.

...R

Thanks, very new to this. trying to find out how to do things. Thanks for looking

The way your code is written it has to run through a series of very long delay()s before loop() gets to repeat and re-check things. Code should be organized so that loop() can repeat hundreds or thousands of times per second.

You may also find some useful ideas in Planning and Implementing a Program.

...R

Well thats very true. Now keep in mind i have not done code before But looking at the application I thought this was the correct way to do things.

Plants dont need to be water but 1 or 2 times a day in the case of the bonsai I want to water.
I really only need to sample and water those plants every 6-12 hours. In my head I was think from the point of view what the plant needed. So the loops really only has to run 1 time in that time frame. And to make sure that i have applied the correct amount of water I thought it would be a good idea to check to make the amount of water was the correct amount. The reason for that is the plants im worried about need to have a full load of water for lack of a better term.

Im sorry for having newbe question but I hope the reasoning behind the code may help.

Gorfme:
So the loops really only has to run 1 time in that time frame. And to make sure that i have applied the correct amount of water I thought it would be a good idea to check to make the amount of water was the correct amount.

That sounds like you want it to run at least twice. And what happens if the check shows that there is not enough water? Won't that mean that you need another check following a second watering?

Of course I can see from what you say that it might not matter if the interval between checks is 5 or 10 minutes - and maybe the use of delay() would not cause a problem in your application.

...R

You are correct. and of course this really depends on the amount of water that sent with the pump. that i have not tested yet.

So once i test how much water is delivered i can make sure the amount is correct and checking 1 time and added a more may be all that is needed. these plants are a pain.

So Im still stuck on the part on how to loop back and check again then proceed. it should be simple. I will continue to read but a nudge in the right direction would be great.

Gorfme:
So Im still stuck on the part on how to loop back and check again then proceed.

The simple way is for every iteration through loop() to check the moisture and run the pump if necessary. Then you would record the fact that a plant is fully watered (ie. moisture content is above X) and would not start watering again until the moisture content falls below Y. You could record this separately for each moisture sensor.

...R