Plant WAtering System Code

Hi everyone, I have a question for my plant watering system. I am attempting to set up a watering system. I wrote the code so I have 2 potentiometers, one to set the high moisture level and one to set the low. There are 4 zones/valves for four plants. Once the moisture level reaches the lower moisture setting it SHOULD water in three minute intervals until the high moisture level has been reached. There is also a control valve in the system which is the main valve attached to the water supply. I am having 2 problems so far. When I put the moisture sensor in water, the resistance goes down, which I am in the process of fixing. THe main problem I could use some help with is that none of the valves open when the setting goes below the lower moisture range. The valves are 12v valves wired up to a separate 12v power source which are controlled by an arduino and 4 relays. I am kind of confused why this is happening. Can anyone please give me some insight? And before the online "tough guys" attack my code yet again, I realize it could be written more efficiently with for statements but I am using if statements because that's what I knew how to write when I started and I intent to use what I have. THanks in advance.

//Completed 4 zone auto watering system
//potPins values are just for setup purposes, must be calibrated once placed in soil
//potPins are to adjust the high and low moisture ranges for each zone


#include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,9,10,11,12);          //initialize library with the interface pins (these might need to be adjusted in the future)

const int zone1 = A0;                       //soil sensor for zone 1
const int zone2 = A1;                       //soil sensor for zone 2
const int zone3 = A2;                       //soil sensor for zone 3
const int zone4 = A3;                       //soil sensor for zone 4

const int potPinLow = A4;                      //low moisture pot
const int potPinHigh = A5;                     //high moisture pot

int potLow;
int potHigh;

const int valve1 = 2;                       //water valve 1
const int valve2 = 3;                       //water valve 2
const int valve3 = 4;                       //water valve 3
const int valve4 = 5;                       //water valve 4

const int controlValve = 6;                 //main valve control

int percent1;                               //mapped value of zone1
int percent2;                               //mapped value of zone2
int percent3;                               //mapped value of zone3
int percent4;                               //mapped value of zone4

int display1;
int display2;
int display3;
int display4;
int display7;
int display8;


void setup()
  {
   lcd.begin(16,2);
      
   pinMode(valve1,OUTPUT);
   pinMode(valve2,OUTPUT);
   pinMode(valve3,OUTPUT);
   pinMode(valve4,OUTPUT);

   pinMode(potLow,INPUT);
   pinMode(potHigh,INPUT);
  }
  

void loop()
  {
   potLow = analogRead(potPinLow);
   potHigh = analogRead(potPinHigh);
   
   percent1 = analogRead(zone1);
   percent2 = analogRead(zone2);
   percent3 = analogRead(zone3);
   percent4 = analogRead(zone4);
    
    if(percent1 <= potLow){                     //read the value from the soil moisture sensor
      digitalWrite(valve1,HIGH);                //open the valve
      digitalWrite(controlValve,HIGH);}         //open the main control valve
      delay(180000);                            //delay 3 minutes
      if(percent1 >= potHigh){                  //when value from soil moisture sensor reaches the upper limit
        digitalWrite(valve1,LOW);}              //close the valve

    if(percent2 <= potLow){                     //read the value from the soil moisture sensor
      digitalWrite(valve2,HIGH);                //open the valve
      digitalWrite(controlValve,HIGH);}         //open the main control valve
      delay(180000);                            //delay 3 minutes
      if(percent2 >= potHigh){                  //when value from soil moisture sensor reaches the upper limit
        digitalWrite(valve2,LOW);}              //close the valve

    if(percent3 <= potLow){                     //read the value from the soil moisture sensor
      digitalWrite(valve3,HIGH);               //open the valve
      digitalWrite(controlValve,HIGH);}         //open the main control valve
      delay(180000);                            //delay 3 minutes
      if(percent3 >= potHigh){                  //when value from soil moisture sensor reaches the upper limit
        digitalWrite(valve3,LOW);}              //close the valve

    if(percent4 <= potLow){                     //read the value from the soil moisture sensor
      digitalWrite(valve4,HIGH);               //open the valve
      digitalWrite(controlValve,HIGH);}         //open the main control valve
      delay(180000);                            //delay 3 minutes
      if(percent4 >= potHigh){                  //when value from soil moisture sensor reaches the upper limit
        digitalWrite(valve4,LOW);}              //close the valve
                 
      {
        {
          display1 = map(percent1,0,1023,0,99);       //map zone1 range to 0-99
          display2 = map(percent2,0,1023,0,99);       //map zone2 range to 0-99
          display3 = map(percent3,0,1023,0,99);       //map zone3 range to 0-99
          display4 = map(percent4,0,1023,0,99);       //map zone4 range to 0-99
          display7 = map(potLow,0,1023,0,100);        //map potLow range to 0-100
          display8 = map(potHigh,0,1023,0,100);       //map potHigh range to 0-100
            
            lcd.setCursor(0,0);                             //set the cursor to the first row and the first column
            lcd.print(display1);                            //print mapped value of zone1              
            lcd.setCursor(6,0);                             //set the cursor to the first row and the seventh column
            lcd.print(display2);                            //print mapped value of zone2
            lcd.setCursor(0,1);                             //set the cursor to the second row and the first column
            lcd.print(display3);                            //print mapped value of zone3
            lcd.setCursor(6,1);                             //set the cursor to the second row and the seventh column
            lcd.print(display4);                            //print mapped value of zone4
            lcd.setCursor(12,0);                            //set cursor to first row and the 13th column
            lcd.print(display8);                            //print mapped value for potHigh
            lcd.setCursor(12,1);                            //set cursor to the second row and the 13th column
            lcd.print(display7);                            //print the mapped value for potLow                        
      }
    }
  }

Have you tried printing the values that you are testing with the if statements ?

As you suggest, the code is in serious need of refactoring to use arrays, a for loop and millis() for timing

Get ahead of the game and read Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

I have the values displaying on a 16x2 lcd display and everything seems ok. Even with the values from the moisture sensor reversed, I would think that the valve would open once it receives a value that is below the permissible range.

Need to know what relays you're using as there may be some issue in trying to drive them directly (without transistors), especially as you're taking control off of one port.

These are the ones I'm using

Yep. Those relays are cute, and somewhat problematic as the solenoid coil typically requires 85mA to hold position. They need to have a dedicated power supply as the IO pins leading straight from the MCU aren't hot enough. Borderline cases (add and subtract all the tolerances) will make it seem to work but as the coil heats up it will fail without enough current. We should all be using solid state relays these days, especially for low power solutions, or a simple transistor. Post a copy of your circuit diagram and let's have a look.

Thanks Watson, I bet you're right. I'm going to add a secondary power source. I don't have a circuit diagram for this. Thanks for the help guys

As we don't know what Arduino you're using and assume it to be Uno, if you look at the Rev. 3 circuit diagram you'll see that the power pins (those on the same side of the board as PORTC and the analog pins) are wired directly (sort of) to the power supply. More importantly, they don't come out from the chip so they aren't constrained to the 40mA limit. Four of these however, at a typical draw of 85mA (320 total) puts a serious load on the maximum of 500mA which is the USB spec. In all likelihood what's left will be more than sufficient, but best practice suggests any time you use an electro-mechanical relay you seriously need to consider supplying it separately and as the relays are handling 12v for the valves, it makes perfect sense to draw off from that supply. You can put 12v across the solenoid and still control it with a 5v signal.

When I put the moisture sensor in water, the resistance goes down, which I am in the process of fixing.

Yes, that's the way the present Universe works, non treated water is a conductor, (albeit a lousy conductor), the more water, the higher the conductance and the lower the resistance (resistance = 1 / conductance). :wink: