Try this one. Study it. Understand it. Then write something like it for your own use.
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the symbols
char stdKeys[ROWS][COLS] = {
{ '1' , '2' , '3' , 'A' },
{ '4' , '5' , '6' , 'B' },
{ '7', ' 8', ' 9', ' C' },
{ '#' , '0' , '*' , 'D'}
};
byte colPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte rowPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad
int myInt = 0;
byte digitCount = 0;
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(stdKeys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(115200);
Serial.println("Enter a 4 digit number");
}
void loop(){
char Key = customKeypad.getKey();
if (Key >= '0' && Key <= '9'){
Serial.println(Key);
myInt = (myInt * 10) + Key -'0';
digitCount++;
if (digitCount == 4) {
Serial.print("You entered: ");
Serial.println(myInt);
digitCount = 0;
myInt = 0;
}
}
}