4x5 Matrix Keypad with code

Hi, I tried to write code for 4x5 Matrix Keyboard.
There are many examples for 3x4 and 4x4 but not many for 4x5.
At last, I have a running code. And I wanted to share it.
Thanks for all sharings.

HW : Arduino Mega (clone)
4x5 keypad 4 Columns, 5 Rows {F1 F2 # * },{1 2 3 UP}, {4 5 6 DOWN}, {7 8 9 ESC}, {LEFT 0 RIGHT ENT}

References :

Cable Connections between MEGA and Keypad.

10 9 8 7 6 5 4 3 2 Arduino MEGA Pins
| | | | | | | | |
O O O O O O O O O Keypad -- 9 pins.
Face Upright position
Like
F1 F2 # *
1 2 3 UP , etc.

CODE :
// rdiot-s187/basic_test.ino at master · rdiot/rdiot-s187 · GitHub
// RDIoT Demo :: 4x5 Matrix Array 20 Keypad [S187]
#include <Wire.h>
#include <Keypad.h>

const byte numRows= 5; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad

//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{'A','B','#','*'},
{'1','2','3','U'},
{'4','5','6','D'},
{'7','8','9','C'},
{'L','0','R','E'}
};

byte rowPins[numRows] = {2, 3, 4, 5, 6};
byte colPins[numCols]= {10, 9, 8, 7};

//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

String numstr = "";

void setup(){
Serial.begin(9600);
Serial.println("Setup");
} // setup

void loop()
{
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY) {
// Serial.print(keypressed);
//lcd.setCursor(0,1);
Serial.println("keypressed=" + (String)keypressed + " ");
switch (keypressed) {
case '1':
numstr = "1 ";
break;
case '2':
numstr = "2 ";
break;
case '3':
numstr = "3 ";
break;
case '4':
numstr = "4 ";
break;
case '5':
numstr = "5 ";
break;
case '6':
numstr = "6 ";
break;
case '7':
numstr = "7 ";
break;
case '8':
numstr = "9 ";
break;
case '0':
numstr = "0 ";
break;
case 'A':
numstr = "F1 ";
break;
case 'B':
numstr = "F2 ";
break;
case 'C':
numstr = "ESC ";
break;
case 'E':
numstr = "ENTER";
break;
case 'D':
numstr = "DOWN ";
break;
case 'U':
numstr = "UP ";
break;
case 'R':
numstr = "RIGHT ";
break;
case 'L':
numstr = "LEFT ";
break;
case '':
numstr = "
";
break;
case '#':
numstr = "# ";
break;

default:
break;
}

if(numstr !="") {
//lcd.setCursor(0,2);
Serial.println("numstr=" + (String)numstr + " ");
}
}
} // loop

sample.pdf (48.5 KB)

Good for you for getting it working, but there is a much better way to convert the input from the keypad into a string

If you put actual numbers from 1 to 21 in the keymap array and define it as an array of bytes then getKey() will return a number between 1 and 21 which you can use as the index to an array of strings (or Strings if you really must use them). Don't use 0 as a key value as that is used by the library as the value of NO_KEY. Using an array like this eliminates the need for any if statements to determine which key has been pressed thus making the program shorter

Please follow the advice on posting code given in Read this before posting a programming question in order to make your sketch easy to read, copy and test

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

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