c - 带 4x4 矩阵键盘的 16X2 LCD 屏蔽

标签 c arduino serial-port arduino-uno

我有一个小问题。我试图在我的 16x2 LCD 屏蔽上显示字符,带有连接到 arduino uno 的 4x4 键盘。当我按下键盘按钮时,相应的字符在串行监视器上成功打印,但不是 dsplay。 我还应该提到,在查找字符的 ASCII 表后,LCD 上打印的不正确字符对应于重复的 ASCII 字符,即按 1 键打印 1111 1111,而另一个键可能打印对应于 ASCII 1011 1011 的字符.本质上是ascii表中的字符从右下角到左上角。此外,当我按下一个键时,它会在串行监视器上打印一次,但在 LCD 上打印多次。 我将在下面发布我的代码以及我正在使用的键盘和 LCD 屏蔽的链接。最后一点,当我在显示器上打印字符/单词时,它们与键盘无关,它们打印得很好。

代码:

#include <Keypad.h>
#include <LiquidCrystal.h> // initialize the library with the numbers of the

LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 


const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'}, //define the cymbols on the buttons of the keypads
{'7','8','9','C'},
{'*','0','#','D'}
};   
byte rowPins[ROWS] = {13, 12, 11, 10}; //connect to the row pinouts of the         keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the     keypad


Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS,    COLS); //initialize an instance of class NewKeypad

void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
}

void loop()
{
char customKey = customKeypad.getKey();

if (customKey)
{
//lcd.setCursor(1,1); 
lcd.print(customKey);
delay(500);
Serial.print(customKey);
}
}

液晶屏: http://www.maplin.co.uk/p/16x2-lcd-shield-for-arduino-n07dh

键盘: https://www.coolcomponents.co.uk/sealed-membrane-4-4-button-pad-with-sticker.html

谢谢,希望有人能帮忙。

最佳答案

移除 if 条件并尝试。

#include <Keypad.h>
#include <LiquidCrystal.h> // initialize the library with the numbers of the

LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 


const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'}, //define the cymbols on the buttons of the keypads
{'7','8','9','C'},
{'*','0','#','D'}
};   
byte rowPins[ROWS] = {13, 12, 11, 10}; //connect to the row pinouts of the         keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the     keypad


Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS,    COLS); //initialize an instance of class NewKeypad

void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
}

void loop()
{
char customKey = customKeypad.getKey();
//lcd.setCursor(1,1); 
lcd.print(customKey);
delay(500);
Serial.print(customKey);

}

关于c - 带 4x4 矩阵键盘的 16X2 LCD 屏蔽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35249727/

相关文章:

c++ - 如何将 Arduino 库添加到 Eclipse 项目?

macos - 使用 Xcode 5 在 Mac OS X 上进行 Arduino 编程?

C++/Arduino 如何从类内部引用全局

linux - 使用socat同时嗅探多个串口

创建一个用零填充的特定维度的文件

c - 程序崩溃时如何捕获错误?

c - c语言udp套接字编程问题

c++ - MFC 串行通信

java - 串行端口库给出错误 java.lang.UnsatisfiedLinkError :

java - 如何检索 PostgreSQL 列名并导入到 Matlab 中的结构数据