Code
This code is used to obtain all key code of 4x4 Matrix Keypad, when one of keys is pressed, the key code will
be printed out in the terminal window.
C Code 22.1.1 MatrixKeypad
First observe the project result, and then analyze the code.
1. Use cd command to enter 22.1.1_MatrixKeypad directory of C code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/C_Code/22.1.1_MatrixKeypad
2. Code of this project contains a custom header file. Use the following command to compile the code
MatrixKeypad.cpp, Keypad.cpp and Key.cpp generate executable file MatrixKeypad. And the custom
header file will be compiled at the same time.
gcc MatrixKeypad.cpp Keypad.cpp Key.cpp -o MatrixKeypad -lwiringPi
3. Run the generated file "MatrixKeypad".
sudo ./MatrixKeypad
After the program is executed, press any key on the MatrixKeypad, the terminal will print out the
corresponding key code. As is shown below:
The following is the program code:
#include "Keypad.hpp"
#include <stdio.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = { //key code
{ '1', '2','3','A'},
{ '4', '5','6','B'},
{ '7', '8','9','C'},
{ '*', '0','#','D'}
};
byte rowPins[ ROWS] = {1, 4, 5, 6 }; //connect to the row pinouts of the keypad
byte colPins[ COLS] = {12,3, 2, 0 }; //connect to the column pinouts of the keypad
//create Keypad object