IF loop for checking buttons

Hi All,

I have a problem where I am trying to filter out a certain condition that exists for a particular hardware switch I need to use.

I have a 4 direction joystick part that also includes a push action. The joystick can be pressed in 4 directions (up, down, left, right) and also pushed in (push). The issue is that the device has output pins for each direction and the Push, however the push circuit is also closed whenever any of the directions are pressed (see output table image attached).

I need to remove the PUSH signal whenever a direction is pressed, I only want the PUSH signal when the joystick is actually pushed.

I tried the following code, and it works fine for the above purpose, however for some reason when I trigger an unrelated input (in this case analogue input A2) part of this loop is triggered and the wrong output is sent. If I comment out all but the last if statement, the error goes away.

Should I be taking a different approach to this?

 // handle buttons
  for (int i=0; i<n_buttons; i++) {
   
   if ( (digitalRead(hardware_buttons[26].pin) == LOW) && (digitalRead(hardware_buttons[21].pin == LOW)) ) {
        Serial.print(hardware_buttons[21].cmd);
        Serial.print("\n");
        delay(debounce_delay_long); 
    }

   else if ( (digitalRead(hardware_buttons[26].pin) == LOW) && (digitalRead(hardware_buttons[28].pin == LOW)) ) {
        Serial.print(hardware_buttons[28].cmd);
        Serial.print("\n");
        delay(debounce_delay_long); 
    }

   else if ( (digitalRead(hardware_buttons[26].pin) == LOW) && (digitalRead(hardware_buttons[19].pin == LOW)) ) {
        Serial.print(hardware_buttons[19].cmd);
        Serial.print("\n");
        delay(debounce_delay_long); 
    }

   else if ( (digitalRead(hardware_buttons[26].pin) == LOW) && (digitalRead(hardware_buttons[20].pin == LOW)) ) {
        Serial.print(hardware_buttons[20].cmd);
        Serial.print("\n");
        delay(debounce_delay_long); 
    }

   if (digitalRead(hardware_buttons[i].pin) == LOW) {   
      Serial.print(hardware_buttons[i].cmd);
      Serial.print("\n");
      delay(debounce_delay);      
    }
  }

What is hardware_buttons ?
Is it an array of structs or what ?

To really help we need to see the whole program or a smaller sample program that demonstrates the problem.

Do you really have 29 or more buttons connected?

Yes, I have buttons and encoders taking every available input on the MEGA

And the code, as asked by UKHeliBob in the first reply.

sterretje:
And the code, as asked by UKHeliBob in the first reply.

Some of it is. You got some of the answer.

If you want ALL of the answer, post ALL of the code.

By the way, there is no such thing as an if loop. There is an if statement, but the if statement does NOT loop.