Intermittent Membrane Keypad

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 = ' ';
}

Your improperly posted code will not compile. If you can't post all the code, post a minimal verifiable program that shows the problem.

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

Calls to the delay() function leads to unresponsive progams. Here are some tutorials on non-blocking programming:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.