目的:本人打算利用4×4矩阵键盘输入参数,若没有输入,则一直等待,直到数据按键按下为止。然后在LCD上进行展示。
问题:主函数中不加waitForKey时,利用主函数loop的特性,可以实现目的;但加入waitForKey之后,按键按下去LCD没有显示;
没有waitForKey这一行时,结果如图:
代码如下:
#include<Keypad.h>
#include<Wire.h>
#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
lcd.init();
lcd.backlight();
Serial.begin(9600);
}
void loop(){
while(!customKeypad.waitForKey()) delay(10);
char customKey = customKeypad.getKey();
if (customKey){
Serial.println(customKey);
lcd.print(customKey);
delay(2000);
lcd.clear();
}
}
|