Too many buttons on an arduino mega?

I have a project with 30 buttons connected, all using input_pullup. My last button (connected to pin 51) always shows low...even with a physical pullup resistor connected.

I wasn't sure if maybe pins 51/52 didn't have internal pullups or what's going on. The button is fine and works on other inputs as expected. I wrote a demo sketch to try to figure out what's going on. Essentially, I set pins 50, 51, 52, and 12 all to input_pullup and read them with both buttons connected and no buttons connected and this is what I get.
Pin50: 1
Pin51: 0
Pin52: 0
Pin12: 1

Always. If I upload the sketch to a mega with no buttons connected, I get 1s on all 4 inputs. What am I missing?

Here's my demo sketch. int btn0 = 50;int btn1 = 51;int btn2 = 52;int btn3 = 12;void setup(){ - Pastebin.com

Please post your program here.

...R

int btn0 = 50;
int btn1 = 51;
int btn2 = 52;
int btn3 = 12;

void setup(){
  Serial.begin(9600);
  Serial.println("Arduino Has Booted!");
  pinMode(btn0,INPUT_PULLUP);
  pinMode(btn1,INPUT_PULLUP);
  pinMode(btn2,INPUT_PULLUP);
  pinMode(btn3,INPUT_PULLUP);
 }

void loop(){
int read = digitalRead(btn0);
Serial.print("Pin50: ");
Serial.println(read);
  
read = digitalRead(btn1);
Serial.print("Pin51: ");
Serial.println(read);

read = digitalRead(btn2);
Serial.print("Pin52: ");
Serial.println(read);

read = digitalRead(btn3);
Serial.print("Pin12: ");
Serial.println(read);
Serial.println("");
Serial.println("");
Serial.println("");
delay(2000);
}

The code works as expected on my Mega. Do you have a shield on the Mega or anything connected to the 2x3 ICSP header on the Mega?

Yes. In fact, I just figured out that the ethernet library seems to write to the pins in the 50s which seems insane considering they shouldn't be there on most arduinos. I'll post a follow up.

Pins 50-52 on the Mega are the SPI bus. The SPI bus is used for communication with the Ethernet shield. Because the SPI bus is on different pins from one board to another, the connections to the Ethernet shield are made via the ICSP header, which is at the same place on all the boards. The ICSP header has connections to those same pins 50-52 on your Mega. When you are using the SPI bus, you must only use pins 50-52 for SPI. You also are limited in how you can use pin 53.

More information:

Can use the analogue pins if you run out of digital pins.
A pin array and a for loop could make the sketch a lot shorter.
Leo..

const byte button[] = {22, 23, 24, 25, 26, A8, A9, A10}; // pins used
const byte n_buttons = 8; // number of buttons
boolean reading;

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < n_buttons; i++) {
    pinMode(button[i], INPUT_PULLUP);
  }
}

void loop() {
  for (int i = 0; i < n_buttons; i++) {
    reading = digitalRead(button[i]);
    Serial.print("Pin");
    Serial.print(button[i]);
    Serial.print(": ");
    Serial.println(reading);
  }
  Serial.println("\n\n");
  delay(2000);
}

Thanks so much for this!