if statement with multiple conditions

Hi,
Hope somebody can help me out.

i have a motor forward and reverse. 2 buttons (A1 and A2) for forward one or the other and 2 buttons(B1 and B2) for reverse one or the other.
2 endswitches. C1 and C2

So the motor has to go forward when A1 or A2 is pressed and B1 and B2 are not pressed (LOW) and C1 is LOW

is this statement oke or is there an easier way:

if (A1 == HIGH || A2 == HIGH) && B1 == LOW && B2 == LOW && C1 == LOW)) {

?

I doubt a little that that compiles :wink:

Personally I would use two if statements.

Statement is no good because parentheses don't match.

1 Like

Hopefully, the compiler already told you that you if is not OK.

if (A1 == HIGH || A2 == HIGH)

One opening parenthesis, and one closing one.
That's it.

In any case, A1, A2, B1, B2, C1, C2 had better be the results of doing digitalRead(...) and had better not be the pin numbers. :o

is this statement oke or is there an easier way:

Compound statements can be difficult to interpret. It is often better to nest the tests to make the structure more obvious

Perhaps

if (A1 == HIGH || A2 == HIGH)
{
  if (B1 == LOW && B2 == LOW && C1 == LOW)
  {
    //do something
  }
}

Doing it that way allows you to put print statements between the groups of tests so that you can track operation of the code when debugging. It also pays to put the least likely tests first as then when it fails the program does not have to execute any more tests

Better variable names would help too