Board: Arduino Mega 2560
Keypad: Generic Membrane 4x4 keypad
I am using a membrane keypad to input numbers and function through the program. It was working fine, but now it will work very sporadically. It may read a key press, and then not read one for 10-20 seconds, and then it may read again or not. There's no rhyme or reason to the timing, and it's not even registering keypresses in the mean time. I've added serial prints all through the code, so I know it's not registering key presses in the background either. I've changed keypads multiple times, swapped connecting wires and even boards. The result is the same every time. I know it's at least connected right, because every now and again, I'm able to get the key to respond and continue on with the program. I am simply at a loss. Any help would be greatly appreciated.
Program (large portions are missing for proprietary reasons):
//LIBRARY INCLUSION
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
//KEYPAD DEFINITION____
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//Left to Right: (R1, R2, R3, R4, C1, C2, C3, C4)
byte rowPins[ROWS] = {22, 24, 26, 28}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {30, 32, 34, 36}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad iKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void loop() {
char iKey = iKeypad.getKey();
//SAMPLING DELAY___
if (Xcount == 0) {
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
lcd.setCursor(0, 1);
lcd.print("Set delay?");
lcd.setCursor(0,2);
lcd.print("A = YES / B = NO");
}
if (iKey && Xcount == 0 && RES == 0) {
if (iKey == 'A') {
Xcount = 1;
lcd.clear();
delay(100);
lcd.setCursor(0,1);
lcd.print("Delay (ms):");
iKey = ' ';
} else if (iKey == 'B') {
Xcount = 2;
digitalWrite(RED, HIGH);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
lcd.clear();
lcd.setCursor(0,1);
lcd.print("No delay selected.");
delay(2000);
lcd.clear();
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
iKey = ' ';
}