Button not working in tandem with another button

Sorry if I'm doing a repeat topic r posting in the wrong place, I'm still new to the forum.

I'm trying to learn basic arduino coding, but I'm having an issue with a piece of code I tried to write.

I've connected 2 pushbuttons to my arduino board, one to pin 2 and the other to pin 3, both using pinMode(INPUT_PULLUP). The first button works fine, but the second button is reading LOW regardless of whether it is pressed or not. However, what is strange is that the 2nd button is functioning normally when the 1st one is pressed (reading LOW when pressed and HIGH when not pressed).

I'm fairly sure it isn't a problem with my circuit, since I've tried changing the pins which the buttons are attached to, and I've tried wiring the buttons with pull-down resistors (and subsequently changing the code to work with pull down resistors. I've also tried re-wiring the circuit in case the issue is something to do with certain pins on my breadboard.

As such, I think something must be wrong with my code. Here it is:

const int ledBlue = 11;
const int ledGreen = 10;
const int ledRed = 9;
const int buttonOne = 3;
const int buttonTwo = 2;
int buttonOneState;
int buttonTwoState;

void setup() {
   pinMode(ledBlue, OUTPUT);
   pinMode(ledGreen, OUTPUT);
   pinMode(ledRed, OUTPUT);
   pinMode(buttonOne, INPUT_PULLUP);
   pinMode(buttonTwo, INPUT_PULLUP);
   Serial.begin(9600);
}

void loop() {
   buttonOneState = digitalRead(buttonOne);
   buttonTwoState = digitalRead(buttonTwo);
   
   if(buttonOneState == LOW && buttonTwoState == LOW){
     digitalWrite(ledRed, HIGH);
     digitalWrite(ledBlue, LOW);
     digitalWrite(ledGreen, LOW);
   }
   else{
     if(buttonOneState == LOW){
       digitalWrite(ledRed, LOW);
       digitalWrite(ledBlue, LOW);
       digitalWrite(ledGreen, HIGH);
     }
     else{
       if(buttonTwoState = LOW){
         digitalWrite(ledRed, LOW);
         digitalWrite(ledBlue, HIGH);
         digitalWrite(ledGreen, LOW);
       }
       else{
         digitalWrite(ledRed, LOW);
         digitalWrite(ledBlue, LOW);
         digitalWrite(ledGreen, LOW);
       }
     }
   }
   Serial.print("buttonOne: ");
   Serial.println(buttonOneState);
   Serial.print("buttonTwo: ");
   Serial.println(buttonTwoState);
   delay(1);
}

rothdu:
However, what is strange is that the 2nd button is functioning normally when the 1st one is pressed (reading LOW when pressed and HIGH when not pressed).

I'm fairly sure it isn't a problem with my circuit

I'm fairly sure it is. What is your circuit?

       if(buttonTwoState = LOW){

Oops..

PaulRB:

       if(buttonTwoState = LOW){

Oops..

Ha :slight_smile:

I'll withdraw my answer then