#include <Keypad.h>
// 定义Keypad的行和列数量
const byte ROWS = 4; // 四行
const byte COLS = 4; // 四列
// 定义Keypad的按键布局
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 12, 14, 27}; // 将这些引脚连接到Keypad的行引脚
byte colPins[COLS] = {26, 25, 33, 32}; // 将这些引脚连接到Keypad的列引脚
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
Serial.println(getChar());
}
String getKeypadString() {
String inputString = "";
char key;
while (true) {
key = keypad.getKey();
if (key != NO_KEY) {
if (key == '#') { // '#' 作为输入结束的标志
break;
} else {
inputString += key;
}
}
}
return inputString;
}
char getChar() {
char key;
while (true) {
key = keypad.getKey();
if (key != NO_KEY) {
return key;
}
}
}