Alternative solutions for long conditional statement.

I recently purchased an arduino for a project I’m building. I have no prior knowledge of coding, but I’m learning. Im building a custom tv mount with a linear actuator and 2 DC motors that are controlled via IR. The build has 7 Micro switches acting as limits, and 6 push buttons in case the IR fails at some point. I have everything working for the manual controls via momentary push buttons and IR. Everything is wired to a port on my Mega. All of the circuits are opened and closed via the arduino.

My problem: I’m trying to write sequences for different positions of the tv.

For example, Position one would check the status of limit switch 1, 3, and 6. If they are all pressed, then the actuator would lower until limit switch 2 is pressed. Then it would turn the DC motor 1 clockwise until limit switch 4 is pressed, then dc motor 2 clockwise until limit switch 7 is pressed.

If Limit switch 2 is already closed and Limit switch 1 is open, then i would like it to proceed to the next step in that sequence. So it would look something like this:

void loop() {

if(digitalRead(LS1) == LOW
&& digitalRead(LS2) == HIGH
&& digitalRead(LS3) == LOW
&& digitalRead(LS4) == HIGH
&& digitalRead(LS5) == HIGH
&& digitalRead(LS6) == LOW
&& digitalRead(LS7) == HIGH)
({digitalWrite(relay1, LOW));
while(LS2)=HIGH));
digitalWrite(relay3, LOW);
while (LS4)=HIGH);
digitalWrite(relay4,LOW);
while (LS7)=HIGH);}

else if(digitalRead(LS1) == HIGH
&& digitalRead(LS2) == LOW
&& digitalRead(LS3) == LOW
&& digitalRead(LS4) == HIGH
&& digitalRead(LS5) == HIGH
&& digitalRead(LS6) == LOW
&& digitalRead(LS7) == HIGH)
digitalWrite(relay3, LOW);
while (LS4)=HIGH);
digitalWrite(relay4,LOW);
while (LS7)=HIGH);}

That code is showing errors at the moment. Im still having some trouble with bracket placements, but it should get the general idea across. It seems like there should be a much better way to write this. It's probably been answered before, but i don't really know what to search for.

Don't do it that way. It leads to a mess that cannot be debugged if it does not work.

For the sort of project you mention you need a State Machine concept (look it up). That is just a fancy name for a system in which one or more variables is used to keep track of the progress through a system.

The code in this link may give you some ideas.

Also, don't write code like this

if(digitalRead(LS1) == LOW

because it makes it impossible to print the values that are going into the test. Instead read all the switches and save their values and use the saved values in any tests

LS1state = digitalRead(LS1pin);
LS2state = digitalRead(LS2pin);
// etc

if (LS1state == LOW

...R

If you were to read each of the 7 inputs and use them to set the state of 7 bits in a byte it would result in a number between 0 and 127 giving you 128 unique states. You could then test the value and act accordingly

In order to read the inputs, putting the pin numbers in an array would allow you to use a for loop to read the state of the inputs in a for loop.

A small example using 4 inputs

const byte buttonPins[] = {A1, A2, A3, A4};

void setup()
{
  Serial.begin(115200);
  for (int input = 0; input < 4; input++)
  {
    pinMode(buttonPins[input], INPUT_PULLUP);
  }
}

void loop()
{
  byte output = 0;
  for (int input = 0; input < 4; input++)
  {
    bitWrite(output, input, digitalRead(buttonPins[input]));
  }
  if (output != 15)
  {
    Serial.println(output);
    delay(200);
  }
}