Multiple If statements with multiple conditions

Hello all,
I am currently building a foot bath to wash cows feet on a farm. There are four baths that the cows walk through after being milked. I have built a panel with a four position selector switch so that farmer can select which bath he would like to fill. A momentary button to say he would like to fill the bath, a maintained button to turn chemical on or off (the first two baths will get soap added and the second two baths will get a chemical added if he chooses) he also has a button to add water for a hard coded amount of time. if his water usage was high at the point of filling he would not get a full bath like he would if no water was being used on the farm. I have also coded a prime pump button to prime 1 of 3 pumps (1 soap 2 chemical) depending on the position of the selector switch. My problem with the following code is that as soon as I move the selector switch to a position it starts filling that bath. The chemical on/off button seems to be working but I am unable to test the add or prime because it instantly starts filling.
any help or suggestions is appreciated!

Code:

// Declare pin names and corresponding arduino pin
const int Bath1Select = A2;
const int Bath2Select = A3;
const int Bath3Select = A4;
const int Bath4Select = A5;
const int ChemOnOff = 0;
const int AddWater = 1;
const int FillWater = 2;
const int PrimePump = 3;
const int Chem1 = 6;
const int Chem2 = 7;
const int Chem3 = 8;
const int MainValve = 9;
const int Bath1Valve = 10;
const int Bath2Valve = 11;
const int Bath3Valve = 12;
const int Bath4Valve = 5;

//Declare Variables with initial values (Ajustable as desired)
int SoapTime = 6000;
int ChemTime = 6000;
int FillTime = 10000;
int AddTime = 6000;

bool FillWaterBit = 0;
bool AddWaterBit = 0;

//This code declares the initial setup when powered up
void setup() {
 
 // Initialize pins as inputs or outputs
 pinMode(Bath1Select, INPUT);
 pinMode(Bath2Select, INPUT);
 pinMode(Bath3Select, INPUT);
 pinMode(Bath4Select, INPUT);
 pinMode(ChemOnOff, INPUT);
 pinMode(AddWater, INPUT);
 pinMode(FillWater, INPUT);  
 pinMode(PrimePump, INPUT);
 pinMode(Chem1, OUTPUT);
 pinMode(Chem2, OUTPUT);
 pinMode(Chem3, OUTPUT);
 pinMode(MainValve, OUTPUT);
 pinMode(Bath1Valve, OUTPUT);
 pinMode(Bath2Valve, OUTPUT);
 pinMode(Bath3Valve, OUTPUT);
 pinMode(Bath4Valve, OUTPUT);
}

void loop() {  
                                           
if (digitalRead(FillWater) == HIGH) {      //Bit is lactched when add or fill button is pressed
  FillWaterBit = HIGH;
}
if (digitalRead(AddWater) == HIGH) {
  AddWaterBit = HIGH;
}

if (digitalRead(PrimePump) == HIGH && digitalRead(Bath1Select) == HIGH) {  //Pump Priming - While prime button is pressed, the pump 1 , 2 or 3 will pump based on the position of the bath selector switch
      digitalWrite(Chem1, HIGH);
}
if (digitalRead(PrimePump) == HIGH && digitalRead(Bath2Select) == HIGH) {
      digitalWrite(Chem2, HIGH);
}
if (digitalRead(PrimePump) == HIGH && digitalRead(Bath3Select) == HIGH) {
      digitalWrite(Chem3, HIGH);
}

//Bath Filling
if (digitalRead(FillWaterBit) == HIGH && digitalRead(ChemOnOff) == HIGH && digitalRead(Bath1Select) == HIGH) {      //FILL BATH 1 CHEM ON
      digitalWrite(MainValve, HIGH);
      digitalWrite(Bath1Valve, HIGH);
      digitalWrite(Chem1, HIGH);
      delay(SoapTime);
      digitalWrite(Chem1, LOW);
      delay(FillTime-SoapTime);
      digitalWrite(MainValve, LOW);
      digitalWrite(Bath1Valve, LOW);
      FillWaterBit = LOW;
}
if (digitalRead(FillWaterBit) == HIGH && digitalRead(ChemOnOff) == HIGH && digitalRead(Bath2Select) == HIGH) {     //FILL BATH 2 CHEM ON
      digitalWrite(MainValve, HIGH);
      digitalWrite(Bath2Valve, HIGH);
      digitalWrite(Chem1, HIGH);
      delay(SoapTime);
      digitalWrite(Chem1, LOW);
      delay(FillTime-SoapTime);
      digitalWrite(MainValve, LOW);
      digitalWrite(Bath2Valve, LOW);
      FillWaterBit = LOW;
}
if (digitalRead(FillWaterBit) == HIGH && digitalRead(ChemOnOff) == HIGH && digitalRead(Bath3Select) == HIGH) {     //FILL BATH 3 CHEM ON
      digitalWrite(MainValve, HIGH);
      digitalWrite(Bath3Valve, HIGH);
      digitalWrite(Chem2, HIGH);
      delay(ChemTime);
      digitalWrite(Chem2, LOW);
      delay(FillTime-ChemTime);
      digitalWrite(MainValve, LOW);
      digitalWrite(Bath3Valve, LOW);
      FillWaterBit = LOW;
}
if (digitalRead(FillWaterBit) == HIGH && digitalRead(ChemOnOff) == HIGH && digitalRead(Bath4Select) == HIGH) {     //FILL BATH 4 CHEM ON
      digitalWrite(MainValve, HIGH);
      digitalWrite(Bath4Valve, HIGH);
      digitalWrite(Chem3, HIGH);
      delay(ChemTime);
      digitalWrite(Chem3, LOW);
      delay(FillTime-ChemTime);
      digitalWrite(MainValve, LOW);
      digitalWrite(Bath4Valve, LOW);
      FillWaterBit = LOW;
}
if (digitalRead(FillWaterBit) == HIGH && digitalRead(ChemOnOff) == LOW && digitalRead(Bath1Select) == HIGH) {     //FILL BATH 1 CHEM OFF
      digitalWrite(MainValve, HIGH);
      digitalWrite(Bath1Valve, HIGH);
      delay(FillTime);
      digitalWrite(MainValve, LOW);
      digitalWrite(Bath4Valve, LOW);
      FillWaterBit = LOW;
}
if (digitalRead(FillWaterBit) == HIGH && digitalRead(ChemOnOff) == LOW && digitalRead(Bath2Select) == HIGH) {     //FILL BATH 2 CHEM OFF     
      digitalWrite(MainValve, HIGH);
      digitalWrite(Bath2Valve, HIGH);
      delay(FillTime);
      digitalWrite(MainValve, LOW);
      digitalWrite(Bath4Valve, LOW);
      FillWaterBit = LOW;
}
if (digitalRead(FillWaterBit) == HIGH && digitalRead(ChemOnOff) == LOW && digitalRead(Bath3Select) == HIGH) {     //FILL BATH 3 CHEM OFF     
      digitalWrite(MainValve, HIGH);
      digitalWrite(Bath3Valve, HIGH);
      delay(FillTime);
      digitalWrite(MainValve, LOW);
      digitalWrite(Bath4Valve, LOW);
      FillWaterBit = LOW;
}
if (digitalRead(FillWaterBit) == HIGH && digitalRead(ChemOnOff) == LOW && digitalRead(Bath4Select) == HIGH) {     //FILL BATH 4 CHEM OFF     
      digitalWrite(MainValve, HIGH);
      digitalWrite(Bath4Valve, HIGH);
      delay(FillTime);
      digitalWrite(MainValve, LOW);
      digitalWrite(Bath4Valve, LOW);
      FillWaterBit = LOW;
}
if (digitalRead(AddWaterBit) == HIGH && digitalRead(Bath1Select) == HIGH) { // ADD WATER BATH 1
      digitalWrite(MainValve, HIGH);
      digitalWrite(Bath1Valve, HIGH);
      delay(AddTime);
      digitalWrite(MainValve, LOW);
      digitalWrite(Bath1Valve, LOW);
      AddWaterBit = LOW;
}
if (digitalRead(AddWaterBit) == HIGH && digitalRead(Bath2Select) == HIGH) { // ADD WATER BATH 2
      digitalWrite(MainValve, HIGH);
      digitalWrite(Bath2Valve, HIGH);
      delay(AddTime);
      digitalWrite(MainValve, LOW);
      digitalWrite(Bath2Valve, LOW);
      AddWaterBit = LOW;
}
if (digitalRead(AddWaterBit) == HIGH && digitalRead(Bath3Select) == HIGH) { // ADD WATER BATH 3
      digitalWrite(MainValve, HIGH);
      digitalWrite(Bath3Valve, HIGH);
      delay(AddTime);
      digitalWrite(MainValve, LOW);
      digitalWrite(Bath3Valve, LOW);
      AddWaterBit = LOW;
}
if (digitalRead(AddWaterBit) == HIGH && digitalRead(Bath4Select) == HIGH) { // ADD WATER BATH 4
      digitalWrite(MainValve, HIGH);
      digitalWrite(Bath4Valve, HIGH);
      delay(AddTime);
      digitalWrite(MainValve, LOW);
      digitalWrite(Bath4Valve, LOW);
      AddWaterBit = LOW;
}
 if (digitalRead(AddWaterBit) == HIGH && digitalRead(Bath1Select) == LOW && digitalRead(Bath2Select) == LOW && digitalRead(Bath3Select) == LOW && digitalRead(Bath4Select) == LOW) { // If Add button is pressed while adding water it will cancel
      AddWaterBit = LOW;
}
 if (digitalRead(FillWaterBit) == HIGH && digitalRead(Bath1Select) == LOW && digitalRead(Bath2Select) == LOW && digitalRead(Bath3Select) == LOW && digitalRead(Bath4Select) == LOW) { // If Fill button is pressed while filling it will cancel 
      FillWaterBit = LOW;
}
}

HooveBath.V5.ino (7.4 KB)

How are the inputs wired ?

as soon as I move the selector switch to a position it starts filling that bath

i don't see any code to debounce the switch/button inputs to make sure they are stable.

wait for the same value to have been read a number (5) of times with sufficient delay (20msec) between each reading.

may be best to read your input in one piece of code. you don't and probably shouldn't read the same input in multiple code locations.

UKHeliBob:
How are the inputs wired ?

My inputs are wired as follows : 5v to the button/switch, button to the input with a 10kohm resistor from pin to ground

Please edit your post to add code tags, as described in How to use this forum.

This is a mistake:

if (digitalRead(FillWaterBit) == HIGH 
   && digitalRead(ChemOnOff) == HIGH 
   && digitalRead(Bath2Select) == HIGH) {     //FILL BATH 2 CHEM ON

FillWaterBit is NOT a pin number.

gcjr:
i don't see any code to debounce the switch/button inputs to make sure they are stable.

wait for the same value to have been read a number (5) of times with sufficient delay (20msec) between each reading.

may be best to read your input in one piece of code. you don't and probably shouldn't read the same input in multiple code locations.

Just read up on debounce and that seems like a problem I am going to face next! My fill button has not been pressed yet and it starts filling as soon as the selector switch gets moved into a position(there is one empty position that I leave it in on boot up) I am new to C++ but I really thought I could make this one work easily....Now im stressing lol

johnwasser:
This is a mistake:

if (digitalRead(FillWaterBit) == HIGH 

&& digitalRead(ChemOnOff) == HIGH
  && digitalRead(Bath2Select) == HIGH) {    //FILL BATH 2 CHEM ON




FillWaterBit is NOT a pin number.

ahhh bool must be read and written as true or false! would that solve my issue of jumping right into a fill when the button has never been pressed?

ardweiner69:
ahhh bool must be read and written as true or false! would that solve my issue of jumping right into a fill when the button has never been pressed?

Using it as a pin number in a digitalRead() will almost certainly give you the wrong answer. That is one reason why it is good to use "Pin" as part of the names of pin numbers.

You have a number of long (6min. and 10 min.) delays. FYI, during delay() the processor does nothing else. It focuses on delay only. This means no updating of outputs - indicators/displays, solenoids and such - and no reading of inputs, like switches.

Before going too much farther you may want to think about writing the code in a non-blocking way.

An oft-referred-to post: several things at the same time

dougp:
You have a number of long (6min. and 10 min.) delays. FYI, during delay() the processor does nothing else. It focuses on delay only. This means no updating of outputs - indicators/displays, solenoids and such - and no reading of inputs, like switches.

Before going too much farther you may want to think about writing the code in a non-blocking way.

An oft-referred-to post: several things at the same time

I am okay with being held up with the delays because the farmer only wants to fill one bath at a time. He will turn the chemical on or off before filling. he will only use the addwater button once fill is done and he realizes the bath is not full enough. If he wishes to stop the process he can just hit the estop then reboot and try again.

When I was starting with Arduino, I did several projects where, based on the initial plan, I thought I could get by with delay-based blocking code. As the project progressed, I realized I wanted to add additional features that would not work with the blocking code. It was way more work to rewrite the code to be non-blocking then it would have been if I had just written it that way from the start.

Those were trivial little projects I did just for fun. If your project is going to be used in the operation of a dairy, you definitely want to do it the right way. It's really not that hard once you study the tutorial and play around with some simple sketches to learn the concept.

make sure you can read all you inputs correctly and reliably before worrying about processing.