I'm trying to use a 3x4 keypad to enter numbers to be shown on the LCD screen. There is no errors poping up, but the lcd only displays the line of press a key and the keypad doesn't seem to be working.
The lcd is a 1602 module and the keypad is a 3x4 Keypad MCU Board.
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal.h>
const int RS = 11, EN = 12, D4 = 2, D5 = 3, D6 = 4, D7 = 5;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
char keys[4][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[4] = {12, 5, 6, 10}; //connect to the row pinouts of the keypad
byte colPins[3] = {11, 7, 9}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(void) {
lcd.begin(16, 2); // Initialize the LCD
lcd.setCursor(0, 0);
lcd.print("Press a key:");
}
void loop(void) {
char key = keypad.getKey();
if (key){
lcd.setCursor(0, 1); // set cursor to second line
lcd.print("Pressed Key: ");
lcd.print(key);
delay(2000); // Display the pressed key for 2 second
lcd.setCursor(0, 1); // Clear the pressed key
lcd.print("");
lcd.print(millis()/1000);
}
}
const int RS = 7, EN = 8, D4 = 9, D5 = 10, D6 = 11, D7 = 12;
Not this... (keypad pin definitions are a mess)
Do this... (re-wire your keypad to match these pins)
byte rowPins[4] = {13, A0, A1, A2}; //connect to the row pinouts of the keypad
byte colPins[3] = {A3, A4, A5}; //connect to the column pinouts of the keypad
Not this... (delay stops the keypad read, setCursor does NOT clear the pressed key, all useless)
Do this... ("comment-out) the useless stuff)
if (key) {
lcd.setCursor(0, 1); // set cursor to second line
lcd.print("Pressed Key: ");
lcd.print(key);
// delay(2000); // Display the pressed key for 2 second
// lcd.setCursor(0, 10); // Clear the pressed key
// lcd.print("");
// lcd.print(millis() / 1000);
}
The problem has been solved. The issue was caused by the wringing.
I have one more question though, how can i get this code to display 7 consecutive key inputs at the same time?