First Program of Simple auto/manual 2 zone watering system HELP?

updated but now getting lost

const int button1 = 2; //d2 zone 1 selecter
const int button2 = 3; //d3 zone 2 selector

//2 pins used in power swap to limit the breakdown of the probe
const int sensorPower1 = 6; //sensor power pin d6 also 
const int sensorPower2 = 7; //sensor power pin d7

const int Valve1 = 8; //zone 1 12vdc water valve
const int Valve2 = 9; //zone 2 12vdc water valve

int moistureSensor1 = 0; //A0-3 probe readings
int moistureSensor2 = 1;
int moistureSensor3 = 2;
int moistureSensor4 = 3;
int flowmeter = 4; // not used atm but to come for fail safe shutdown of system to prevent flooding
                   // and to see the amount used
const int Green = 10;  //these four outputs go to relay board turning on lights 
const int Yellow = 11;
const int Red1 = 12;
const int Red2 =13;

const int sensorpowerswap = 10000; //10 to switch power 
const int sensorpoweron = 20000; //20 seconds of the sensor being powered
const int sensorpoweroff = 3600000; //1 hour of sensors turned off
const int displayinterval = 5000; //used for time between displays
const int waterzone1 = 30000;  //
const int waterzone2 = 30000;

int button1State;  //
int button2State;

int switchZone1;  //
int switchZone2;

unsigned long currentMillis = 0;
unsigned long previous_display = 0;
unsigned long previous_sensoron = 0;
unsigned long previous_sensoroff = 0;
unsigned long previous_sensorswap = 0;
void setup()
{
  Serial.begin(9600);
 
  
  pinMode(button1, INPUT_PULLUP); //uses the on board resisitor leaving the button state high
  pinMode(button2, INPUT_PULLUP);
  pinMode(Green, OUTPUT);  //manual zone 1
  pinMode(Yellow, OUTPUT); //manual zone 2
  pinMode(Red1, OUTPUT);   //auto mode 1
  pinMode(Red2, OUTPUT);   //auto mode 2
  pinMode(sensorPower1, OUTPUT);
  pinMode(sensorPower2, OUTPUT);
  pinMode(Valve1, OUTPUT);
  pinMode(Valve2, OUTPUT);
  digitalWrite(Valve1, LOW);
  digitalWrite(Valve2, LOW);
  digitalWrite(sensorPower1, LOW); //starts with d6 as negative
  digitalWrite(sensorPower2, HIGH);//starts with d7 as positive
  
  serial_display();
}

void loop()
{
  currentMillis = millis(); //capture the latest value of mills
  button1State = digitalRead(button1);
  button2State = digitalRead(button2);
 
  
  int sensorValue1 = analogRead(moistureSensor1); //stores the anonlog value
  int sensorValue2 = analogRead(moistureSensor2);
  int sensorValue3 = analogRead(moistureSensor3);
  int sensorValue4 = analogRead(moistureSensor4);
  
  int per1 = map(sensorValue1, 340, 1024, 100, 0); //changed the range so that the value out put is in %
  int per2 = map(sensorValue2, 340, 1024, 100, 0);
  int per3 = map(sensorValue3, 340, 1024, 100, 0);
  int per4 = map(sensorValue4, 340, 1024, 100, 0);

  //timing for displaying the readings
  if (currentMillis - previous_display >= displayinterval)
  {
  serial_display();
  previous_display += displayinterval;
  }
  
  //setting up the perameters of the switch states for zones 1 and 2
  if(per1 <= 35 && per2 <= 35) 
  {
    switchZone1 = 0;
  }
  if(per1 > 35 && per2 > 35) 
  {
    switchZone1 = 1;
  }
  if(per1 <= 6) 
  {
    switchZone1 = 2;
  }
  if(per2 <= 6) 
  {
    switchZone1 = 3;
  }
  
  if(per3 <= 35 && per4 <= 35) 
  {
    switchZone2 = 0;
  }
  if(per3 > 35 && per4 > 35) 
  {
    switchZone2 = 1;
  }
  if(per3 <= 6) 
  {
    switchZone2 = 2;
  }
  if(per4 <= 6) 
  {
    switchZone2 = 3;
  }
  
  
  switch(button1State)
  {
  case 0://button not pushed to read as HIGH because of the pull up resistor
    
      digitalWrite(Valve1, HIGH);
      digitalWrite(Red1, LOW);
      digitalWrite(Green, HIGH);
      digitalRead(button1State);
      delay(30);        
      break; 

  default://button pushed and read as LOW
    digitalWrite(Green, LOW);
    digitalWrite(Red1, HIGH);
    digitalRead(button1State);
    delay(30);
    switch(switchZone1) //checks moisture and then runs which situation fits
    {
      case 0:
      digitalWrite(Valve1, HIGH);//dry
      break;
      case 1:
      digitalWrite(Valve1, LOW);//wet
      break;
      case 2:
      digitalWrite(Valve1, LOW);//probe one fail
      digitalRead(per1);        //re read to make sure failed and react fast when probe changed
      delay(50);
      break;
      case 3:
      digitalWrite(Valve1, LOW);//probe two fail
      digitalRead(per2);        //re read to make sure failed and react fast when probe changed
      delay(50);
      break;
    }
      
    break;    
  }
  
  switch (button2State)
  {
  case 0:
    digitalWrite(Valve2, HIGH);
    digitalWrite(Red2, LOW);    
    digitalWrite(Yellow, HIGH);
    delay(30);
    digitalRead(button2State);
    break;


  default:
    
    digitalWrite(Yellow, LOW);
    digitalWrite(Red2, HIGH);
    digitalRead(button2State);
    delay(30);
    switch(switchZone2)
    {
      case 0:
      digitalWrite(Valve2, HIGH);
      break;
      case 1:
      digitalWrite(Valve2, LOW);
      break;
      case 2:
      digitalWrite(Valve2, LOW);
      digitalRead(per3);
      delay(50);
      break;
      case 3:
      digitalWrite(Valve2, LOW);
      digitalRead(per4);
      delay(50);
      break;
    }
    break;


  }
}

void serial_display()
{
  int sensorValue1 = analogRead(moistureSensor1); //stores the anonlog value
  int sensorValue2 = analogRead(moistureSensor2);
  int sensorValue3 = analogRead(moistureSensor3);
  int sensorValue4 = analogRead(moistureSensor4);
  
  int per1 = map(sensorValue1, 340, 1024, 100, 0); //changed the range so that the value out put is in %
  int per2 = map(sensorValue2, 340, 1024, 100, 0);
  int per3 = map(sensorValue3, 340, 1024, 100, 0);
  int per4 = map(sensorValue4, 340, 1024, 100, 0);
  
  Serial.print("Soil Sensor 1 = ");//all printed on one line
  Serial.print(sensorValue1);      //
  Serial.print("\t\t");            //
  Serial.print(per1);              //
  Serial.println("% moisture");    //ln seems to finish this line moves the next print to next line
  
  Serial.print("Soil Sensor 2 = ");
  Serial.print(sensorValue2);
  Serial.print("\t\t");
  Serial.print(per2);
  Serial.println("% moisture");
  
  Serial.print("Soil Sensor 3 = ");
  Serial.print(sensorValue3);
  Serial.print("\t\t");
  Serial.print(per3);
  Serial.println("% moisture");
  
  Serial.print("Soil Sensor 4 = ");
  Serial.print(sensorValue4);
  Serial.print("\t\t");
  Serial.print(per4);
  Serial.println("% moisture");
  
  /*if(button1State == LOW)
  {
  Serial.println("Manual Zone 1");
  }
  else
  {
  Serial.println("Auto Zone 1");
  }
  if(button2State == LOW)
  {
  Serial.println("Manual Zone 2");
  }
  else
  {
  Serial.println("Auto Zone 2");
  }
  */
}  
  
  //turning on sensors and swapping power to them for longer lasting
void sensoron()
 {
   if(currentMillis - previous_sensoron >= sensorpoweron && currentMillis - previous_sensorswap >= sensorpowerswap)
     {
      digitalWrite(sensorPower1, HIGH); 
      digitalWrite(sensorPower2, LOW);
      previous_sensorswap += sensorpowerswap;
     }
   else
     {
      digitalWrite(sensorPower1, LOW); 
      digitalWrite(sensorPower2, HIGH); 
      previous_sensoron += sensorpoweron;
     }
 }

Attach your code using the </> icon on the left side of the posting menu.
Put your sketch between the code tags [code][/code]

Do things work the way you want?

.

we call them code tags, and the code you post takes a lot of space. go to the forum, any one. the top sticky post titled how to use this forum, item #7, will show you how. come back, edit/modify your post (link at bottom right of your post) put your code in code tags.

Thank you. Feel bad for my poor forum skill set.

As for the program well it does do what is asked. I was hoping for more ideas or thoughts to make it more user friendly such as lcd display or maybe just connect to a computer. Make it simply something more yet still simple.

Mainly does it make sense and what would you like to see added if you were to use it.

I've jumped in way over my head as is I feel thus I turn to you to help really streamline this in a better way.

Thank you

If the sketch does what you need who can argue with success? ;) Most here would urge you to get rid of your delay()s. Have you studied BWD 'blink without delay' technique?

A few more comments might be helpful to remind you of things 6 months from now.

Good! .

I'll have to look at that thanks.

Would like to get rid of less and send info maybe via serial to lcd?

thanks for the fix on your post. and welcome to the forum !

in general, we frown on delay(). delay is like pause.a blocking function. it stops the program cold at that spot for the duration.
you have over 2.5 seconds of delay on a chip that would normally scan a couple thousand times a second.

I would offer that your digitalwrite could be in setup()
they are only ever called once.

I think one of the questions you are asking indirectly is how someone with lots of experience would alter your code for both simplicity and elegance ?

I think that the most common response is that if it is working and there are no real flaws or problems, then use it the way it is.

I am developing an observation that programmers go through stages.
you have a begging, an if() programmer, who uses if() statements for almost everything they write, and copy other work and add it into their sketch.

as you program, and find easier things, you incorporate them and become more versed.

here is the catch, if someone of great ability were to take your program, cut it down to say, 30 lines of code, you would not be able to understand it ! almost more harm than good.

having a program you can understand if lightyears better than having one that does the exact same result, but you have no clue about.

=================
as for delay()

read about blink without delay, check a few tutorials, some explain it easier for you to understand than others.

but think of this. millis() is the system clock.
if you set a value to mills(), and it is behind an if() statement so it will not reset to 0 each scan.

if (duration - millis() ) >= 3000  {
  duration = millis();
}

this will set duration to be equal to millis, the millisecond internal clock
but, once 3000 milliseconds passes, it resets.

so this would have duration forever between 0 and 3000

if (duration >= 1500) {  // this could be a way of making a 1.5 second period to flash an  LED
  do this thing
}
else
{
 do this other thing
}

or, use a window to do a thing.

you can preceed it with an if() statemtn that if it was done, don’t do it again till the next time around.

if ( duration >= 500)  && (duration <== 550) {
 do this thing
}

Yes an LCD would be a good thing to have, but you know this already :wink:
Maybe a keypad to modify sketch operation.

Both of these are relatively easy to use.
Suggest you get a 4X20 LCD, lots on the Adafruit web site.
Maybe an IR keypad or a simple 4X4 for data entry.
After experimenting with these, then add into your sketch.
You could make an LCD and keypad integrated into a hand held unit that plugs into your controller.
Use serial for data transfers.
Then you can use it for other projects too.

BTW
Recommend you have stuff like
if(per1 <= 35 && per2 <= 35) switchZone1 = 0;
changed to
if(per1 <= 35 && per2 <= 35)
{
switchZone1 = 0;
}

Also, look into using arrays.
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

.

  • If you need more help, post the current sketch version you are working on.

  • Describe what is not working.

  • Describe what you want changed and tell us what areas you are having trouble with.

  • If you are needing to add more capabilities to the project, what exactly, what components and what have you done to proceed with getting these elements?

No PMs please.

.