soil moisture sensors and watering system using shift registers and analog mux's

i just thought i should write here as a follow up... i found a few things in my code that had to be change, so here is the new code for the project (with better notes).

basically, i got everything wired up, wrote up some code and got everything going. i chose not to have water going through the pump or the lines yet, because i'm not completely done designing or building this... the electronics aren't in an enclosure yet either, so i'm keeping everything dry until i can get everything sealed up that needs to be.

anyway, after i hooked everything up i found an issue with the 8 channel relay bank that controls the pump and solenoids.
the first time i tried my code, it worked great... however, upon unplugging the arduino from my computer, the solenoids and pump would all turn on, until controlled again by my arduino. if left like that, in the case of the arduino losing power, all plants would be flooded with water until power is turned on again. (not good)

i tried changing up the wires to the relay to fix this, but it only caused another problem... when i changed up the wires, the pump and solenoids would stay off when the arduino lost power (as they should) but i noticed that the switching was reversed with how the pumps and relay were controlled. i had made a change to the code to turn on the solenoid first before the pump, so that pressure doesn't build up in the lines, and then the pump comes off for 2.5 seconds, then turns off, then the solenoid turns off a half second later. rather than that working correctly, i noticed it was reversed... so the solenoid and pumps were always on and open, except for when they were supposed to be watering, in which case it would turn off the solenoid, and then turn off the pump...

so basically it was either working correctly but would turn on the pump when powered down, or it would work incorrectly but at least the pump didn't come on when powered down.

i quickly deduced that there was no rewiring that would fix this, it would need to be done in code... and that the solution was to invert the signals coming from the shift registers to the solenoids and pumps....

so rather than sending 2 bytes like this '10000000 00000001' i rewrote the code to send something like '01111111 11111110' instead.
now the code works perfectly AND the pumps and solenoids stay closed when powered down... so everything now works exactly as it should.

besides that i also added a new function for water control, changed some of the timing, and also mae it so that whenever its done watering it sends the off signal to the solenoids and the pump 3 times just to make sure that it turns off for sure. ( i had noticed that sometimes it wouldn't turn on or off when it should and i figured that resending the off signal a few times would help ensure that they definitely get turned off when they should.

anyway, here is the new commented code! thanks for all your help!

/*

-Garden Arduino using Shift Registers, relay banks, solenoids,
and a water pump.

written by plato_03 5/9/2014
email me: plato_03@hyahoo.com

----------------------------------------------------------------*/

/*setting variables for controlling the data, latch, and clock
 pins of the shift shift registers*/
int dataPin = 8;
int latchPin = 9;
int clockPin = 10;

/*variables that will hold the values of soil moisture for
 each plant */
int plantOne;
int plantTwo;
int plantThree;

/* shiftControl - a function for controlling the shift registers, 
 both in sending 5v signals to individual plants but also to 
 control relays. SR4-SR1 variables values range between 0-255, 
 which pushes those values as a binary sequence through each shift 
 register. 
 
 if you are to add or take away shift registers, you would need to 
 revise this portion of code to have more 'SRx' variables, and add 
 another line of 'shiftOut' for each additional shift register. 
 the 'duration' variable is how long the code delays before moving 
 on to the next event afterwards.
 
 currently it sends out 4 bytes of information, the first two bytes
 are to control the 5v going to each soil probe, the last two bytes
 are to control the relay bank that are hooked up to the solenoids
 and water pump.
 
 when sending out 5v to soil moisture probes, it will send out data
 to each shift register as follows:   
 SR4      SR3      SR2      SR1
 11111111 11111111 00000000 00000001
 11111111 11111111 00000000 00000010
 11111111 11111111 00000000 00000100
 ^^^^^^^^-^^^^^^^^---soil moisture probes
 
 when controlling the pump and the solenoids it will send 0's 
 instead of 1's to SR4 and SR3 to turn on the relays as follows:
 
 SR4      SR3      SR2      SR1
 pump-->01111111 11111110 00000000 00000000
 01111111 11111101 00000000 00000000
 01111111 11111011 00000000 00000000
 solenoids-^^^^^^^-^^^^^^^^
 */



void shiftControl(int SR4, int SR3, int SR2, int SR1, int duration)
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, SR4);
  shiftOut(dataPin, clockPin, MSBFIRST, SR3);
  shiftOut(dataPin, clockPin, MSBFIRST, SR2);
  shiftOut(dataPin, clockPin, MSBFIRST, SR1);
  digitalWrite(latchPin, HIGH);
  delay(duration);
}

/* waterControl - a function for controlling the water pump and 
 solenoids. they should be wired up to a relay bank, which are
 controlled by the shift registers. it works by using shiftControl
 function, except with more parameters. 
 
 solLVal and solRVal is the same as SR4 and SR3 from shiftControl 
 and are used to turn on solenoids connected to the relays. 
 
 pumpVal is for turning on the pump and the solenoid and replaces 
 solLVal in the 2nd iteration of shiftControl. 
 offset is how long the solenoids open or close up before and after
 the pump runs. 
 
 runTime is how long  the pump stays on before turning off.
 
 notice that in the place of SR2, an SR1, they are always 0. this is
 because soil moisture sensors are always off during watering.
 
 */
void waterControl(int solLVal, int solRVal, int pumpVal, int offset, int runTime)
{
  shiftControl(solLVal, solRVal, 0, 0, offset);
  shiftControl(pumpVal, solRVal, 0, 0, runTime);
  shiftControl(solLVal, solRVal, 0, 0, offset);
}

/*setup shift register latch, clock, data pins, begins the Serial
 output, and also sends a shiftControl signal to turn off all the 
 relays by sending all 1's through SR3 and SR4, and turns off all 
 moisture probes by sending all 0's to SR1 and SR2*/
void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  Serial.begin(9600);
  shiftControl(255,255,0,0,10);


}
/*loop turns on each soil moisture probe in sequence, stores the 
 energy that gets through the other end of moisture probe as a
 variable (plantOne, plantTwo, plantThree, etc), it then displays
 through serial moniter which plant is being detected and what the
 value is that was read by the moisutre sensor, then moves on to 
 detecting the next one.*/
void loop()
{
  shiftControl(255,255,0,1,10);
  plantOne = analogRead(0);
  Serial.print("Plant 1 : ");
  Serial.print(plantOne);
  Serial.print(" | ");
  shiftControl(255,255,0,2,10);
  plantTwo = analogRead(1);
  Serial.print("Plant 2 : ");
  Serial.print(plantTwo);
  Serial.print(" | ");
  shiftControl(255,255,0,4,10);
  plantThree = analogRead(2);
  Serial.print("Plant 3 : ");
  Serial.println(plantThree);
  Serial.println(".............................................");
  shiftControl(255,255,0,0, 1000);

  /*these 'if' conditions are for if the water level of any plant
   goes below the level stated, then it will read in the serial 
   moniter that is it Watering Plant X and kick on the pump and 
   solenoid needed to water the plant using waterControl function.
   after the waterControl function is used, shiftControl is called
   twice to make sure that the water pump and solenoids are all 
   closed for sure.*/
  if(plantOne < 375)
  {
    Serial.println("Watering Plant 1");
    Serial.println(".............................................");
    waterControl(255, 254, 126, 500, 2500);
    shiftControl(255,255,0,0,10);  
    shiftControl(255,255,0,0,10);
  }
  if(plantTwo < 375)
  {        
    Serial.println("Watering Plant 2");
    Serial.println(".............................................");
    waterControl(255,253,125,500,2500);
    shiftControl(255,255,0,0,10);
    shiftControl(255,255,0,0,10);
  }
  if(plantThree < 375)
  {
    Serial.println("Watering Plant 3");
    Serial.println(".............................................");
    waterControl(255,251,123,500,2500);
    shiftControl(255,255,0,0,10);
    shiftControl(255,255,0,0,10); 
  }

}