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);
}