Problems with Accurately Reading Many Button Switches

Hello, I'm working on a project that right now includes 14 latching button switches from sparkfun (Metal Pushbutton - Latching (16mm, Red) - COM-11971 - SparkFun Electronics). I've posted code below that should read from the digital pins and print out a 0 or a 1 depending on the state of the button. The problem is that it does not accurately print the state of the button. Based on the serial monitor, the Arduino seems to have trouble recognizing 1s consistently. If all of the buttons are set to output 0, then I get all 0s. But if they are all set to 1, then I get an almost random list of 0s and 1s. Not sure what the issue is.

I've posted my code below. I am using an Arduino Mega 2560 that is powered by USB right now. Each LED only draws about 17mA of current, so the 500mA USB port shouldn't have any problem powering everything. I am confident that all of the soldering is correct, so I don't think there is an issue there either. Thoughts?

int blueA, blueB, blueC, blueD;
int redA, redB, redC, redD;
int mode, lock, loopSwitch, channel, record, power;

void setup(){
  Serial.begin(9600);
  for (int thisPin = 22; thisPin < 36; thisPin++){
    pinMode(thisPin, INPUT);
  }
}

void loop(){
  //// BLUE AND RED SWITCHES ////
  blueA = digitalRead(22);
  delay(1);
  blueB = digitalRead(23);
  delay(1);
  blueC = digitalRead(24);
  delay(1);
  blueD = digitalRead(25);
  delay(1);
  redA = digitalRead(26);
  delay(1);
  redB = digitalRead(27);
  delay(1);
  redC = digitalRead(28);
  delay(1);
  redD = digitalRead(29);
  delay(1);
  
  //// GLOBAL SWITCHES ////
  mode = digitalRead(30);
  delay(1);
  lock = digitalRead(31);
  delay(1);
  loopSwitch = digitalRead(32);
  delay(1);
  channel = digitalRead(33);
  delay(1);
  record = digitalRead(34);
  delay(1);
  power = digitalRead(35);
  delay(1);

  // SERIAL WRITE ALL SWITCHES
  Serial.println(blueA);
  Serial.println(blueB);
  Serial.println(blueC);
  Serial.println(blueD);
  Serial.println(redA);
  Serial.println(redB);
  Serial.println(redC);
  Serial.println(redD);
  Serial.println(mode);
  Serial.println(lock);
  Serial.println(loopSwitch);
  Serial.println(channel);
  Serial.println(record);
  Serial.println(power);
  
  delay(1000);
}

How are the buttons wired?
It should be between the input and ground with the internal pull up resistors enabled in the pin mode statement.

You have mentioned LEDs - but no LEDs are mentioned in the sketch.

My suspicion is that you have the LEDs - of various colours - wired to the switches and given that you specify 17 mA, I presume you have current limiting resistors in series with the LEDs.

Since you have described essentially nothing about your circuit, we can only surmise what is going on. We do not know for example, whether you have the switches to ground and the LEDs to 5V (which is the preferred way to do it), or switches to 5V and LEDs to ground. And presumably, you have the inputs connected to the point between switch and current limiting resistor rather than between resistor and LED.

So the point is, the LEDs when no current is fed to them, do not pull the current across them to zero, but only to the point just below their threshold voltage, which may be as high as 3V or more for coloured LEDs. This is more than the switching threshold of the Arduino inputs which is about half of Vcc, or perhaps 2.5V or so. This means that when the button is open, the voltage does not drop back to the "logic 0" level.

One - but not the only - reason for wiring the buttons to ground and the LEDs to Vcc, is that you can enable the internal pull-ups in the Arduino (by writing the pins to HIGH even though they are inputs) to pull the pin voltage fully (or near enough) up to Vcc to conclusively register a "1". Of course your logic is reversed, but that matters not in the slightest; it is dead easy to arrange the code to do whatever you need it to do.

Well, the good news is that enabling the pullup resistor on all of the pins did the trick. I thought I had tried this yesterday, but apparently not.

And sorry about the confusion over the switches. The switches have LEDs on them. There is a - and + terminal for the LED and a - and NC pin I am connected to for the switch.

I didn't see any switch debouncing for the buttons.

raschemmel:
I didn't see any switch debouncing for the buttons.

In 95% of applications you do not need debouncing on push buttons.