241
Chapter 22 Matrix Keypad
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
#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
Summary of Contents for Ultimate Starter Kit
Page 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Page 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Page 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Page 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Page 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Page 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...