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?