Testing multiple conditions for If..Else Statement

Hello all.

So I am trying to understand how to write a code that tests multiple digital inputs, and when the states match the if statement, an output goes HIGH. Currently I am trying to just test and learn on a smaller scale on how to do this, so I made a simple test, using the LED on the board. In this test, I am looking for nothing to happen unless inputs 2,3,AND 4 have all gone LOW.

What keeps happening to me is that in the IF statement conditions, only the last condition in the string applies, and it ignores the other 2. With just one input, it works, but whenever I try to add in more inputs is where I get this issue.

The end goal of this current project is to test against a row of dipswitches. So a user can set the dipswitches to their desired state, and when a set of inputs match the state of the dipswitches(whether set to high or low), an output will go HIGH. I am trying to fully understand each step of this project, and build up the complexity as I go, to fully understand each part and why it is happening, because that is how I learn best.

Here is my code:

  int button1Pin = 2;
  int button2Pin = 3;
  int button3Pin = 4;
  int ledPin = 13;

void setup() {
  // put your setup code here, to run once:
  pinMode(button1Pin, INPUT_PULLUP);
  pinMode(button2Pin, INPUT_PULLUP);
  pinMode(button3Pin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
   
  if (digitalRead(2)==LOW&&digitalRead(3)==LOW&&digitalRead(4)==LOW){
    digitalWrite(ledPin,HIGH);
  } else{
    digitalWrite(ledPin,LOW);
  }
}

Not sure why you defined

  int button1Pin = 2;
  int button2Pin = 3;
  int button3Pin = 4;

and then used 2, 3, and 4, but otherwise it looks good to me. I usually use extra parentheses to ensure the correct interpretation but they should be unnecessary here.

Thank you for using code tags and presenting nicely formatted code!

Other unrelated tips:

When you are tempted to use numbers in the naming of variables, it may be time to learn about arrays.

Pins can use variables defined as byte instead of int. It saves memory space. Not important in this simple program but a good habit to get into in preparation for larger programs. const would be even better.

I defined because I was trying things different ways, and then ended up confusing myself, but that was left in the code.

With rereading my initial post, to clarify the issue that is happening, is that if Pin4 goes low, regardless of what is happening with 2 and 3, the led goes HIGH. I need all 3 pins to go LOW before the led pin goes HIGH, so I am wondering if I did something wrong with the IF statement

Which Arduino? How are the dipswitches wired?

 int button1Pin = 2;
 int button2Pin = 3;
 int button3Pin = 4;
 int ledPin = 13;

void setup() {
  // put your setup code here, to run once:
  pinMode(button1Pin, INPUT_PULLUP);
  pinMode(button2Pin, INPUT_PULLUP);
  pinMode(button3Pin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
   
  if (digitalRead(button1Pin) == LOW
    && digitalRead(button2Pin) == LOW
      && digitalRead(button3Pin) == LOW)
  {
    digitalWrite(ledPin,HIGH);
  }
  else
  {
    digitalWrite(ledPin,LOW);
  }
}

What keeps happening to me is that in the IF statement conditions, only the last condition in the string applies, and it ignores the other 2.

of course, the first condition is true if all 3 buttons are held down at the same time (assuming a buttons connects the pin to ground).

consider the following

    bool a = digitalRead(2);
    bool b = digitalRead(3);
    bool c = digitalRead(4);

    if ( !a && !b && !c)  {
        digitalWrite(ledPin,HIGH);
    }
    else if ( !a &&  b &&  c)  {
        ; // 1st button pressed
    }
    else if ( !a && !b &&  c)  {
        ; // 1st & 2nd buttons pressed
    }
    else {
        digitalWrite(ledPin,LOW);
    }

How do you have the buttons wired? I don’t see a problem with the if statement.

Would this make any difference?

  if ((digitalRead(button1Pin) == LOW)
    && (digitalRead(button2Pin) == LOW)
      && (digitalRead(button3Pin) == LOW))