Keypad problems.

I'm using a 4x4 keypad from Jameco (this one) for user input with an Arduino Mega. The problem is, reading it doesn't work much at all.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>



#define I2C_ADDR      0x3F
#define BACKLIGHT_PIN 3
#define En_pin        2
#define Rw_pin        1
#define Rs_pin        0
#define D4_pin        4
#define D5_pin        5
#define D6_pin        6
#define D7_pin        7

#define KPC1          22
#define KPC2          24
#define KPC3          26
#define KPC4          28
#define KPR1          30
#define KPR2          32
#define KPR3          34
#define KPR4          36

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin, BACKLIGHT_PIN, POSITIVE);

int pl=0;
void setup()
{
  
  lcd.begin(20,4);
  Serial.begin(9600);
  lcd.print("start");
  delay(1000);
  lcd.clear();
  lcd.home();
  pinMode(KPR1,OUTPUT);
  pinMode(KPR2,OUTPUT);
  pinMode(KPR3,OUTPUT);
  pinMode(KPR4,OUTPUT);
  pinMode(KPC1,INPUT_PULLUP);
  pinMode(KPC2,INPUT_PULLUP);
  pinMode(KPC3,INPUT_PULLUP);
  pinMode(KPC4,INPUT_PULLUP);
  for(int i=22;i<=36;i+=2){digitalWrite(i,LOW);}
}

void loop()
{
  digitalWrite(KPR1,HIGH);
  delay(2);
  if(!digitalRead(KPC2)){if(pl!=1){lcd.print("!");pl=1;}Serial.println("!");}else{pl=0;lcd.clear();}
  digitalWrite(KPR1,LOW);
  delay(2);
}

Currently, my code (should) only read the first row, second column, so:

[ ][ ][ ][ ]
[ ][ ][ ][ ]
[ ][ ][ ][ ]
[ ][x][ ][ ]

Pressing the button that I'm trying to read will affect the output, but so do many of the other buttons, and some of them seem to latch it. Any ideas?

Hi,

Your setup() is performing digitalWrite(LOW) on the pins that just got set to INPUT_PULLUP. This will switch off the internal pullups.

I would suggest trying this: set all your row pins to input without pullup in setup(). Then, in loop(), switch one row at a time to output and low, read the columns and then set the row back to input.

Paul

I tried your idea, but it seems that no matter how I read the inputs and set the outputs, I have the same problem.
It seems that whatever button I try to read acts like this: It, and all the buttons lower in its column, trigger a button press. All the buttons higher in its column, as well as the two to the left and right of the one above it, seem to latch it so that a button press is always reported.

Where L is latching, T is target, and P is reporting press:
[ ][ ][L][ ]
[ ][L][L][L]
[ ][ ][T][ ]
[ ][ ][P][ ]

On the edges and corners the same rules mostly hold true, but with more weirdness.

To me it seems like this would just be a broken keypad, but here's something else that didn't occur to me earlier:
For cable management purposes, all 8 wires from the keypad to the Arduino are twisted together in a bundle. Is there any chance that there could be interference between the wires?

Why are you not using the keypad library? It does everything your doing and more.

I am now, it works great. Thanks!
Also, I might have had a problem due to plugging all my wires in 1 hole to left on the Arduino headers, leaving one of the columns connected to ground...