LCD屏幕显示随机字符



因此,我最终尝试使用4x4数字键盘、arduino和螺线管构建一个有趣的安全系统。当我试图让数字键盘和LCD一起工作时,由于未知的原因,我不断遇到问题。下面的代码是我迄今为止所拥有的:

#include <LiquidCrystal.h> // includes the LiquidCrystal Library 
#include <Keypad.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) 
//_________________________________________
const byte rows = 4; //number of the keypad's rows and columns
const byte cols = 4;
char keyMap [rows] [cols] = { //define the cymbols on the buttons of the keypad
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins [rows] = {1, 2, 3, 4}; //pins of the keypad
byte colPins [cols] = {5, 6, 7, 8};
Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, rows, cols);
//_________________________________________
void setup() { 
Serial.begin(9600);
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display } 
}
void loop() { 
char key = myKeypad.getKey();
if (key){
Serial.print(key);
lcd.print("key has been pressed!");
delay(2000);
lcd.clear();
}
}

然而,我不断地得到随机和破碎的角色,我不明白为什么。有人能帮我吗?

在此处输入图像描述

您的LCD显示器没有显示预期的字符串,因为您正在重叠用于另一个任务的引脚
Arduino板的大部分引脚1用作串行发射器(Tx(引脚。同样的引脚也发生在你的一个液晶显示器引脚上(rs引脚(。这会导致LCD上出现意外行为和胡言乱语的文本。

//Pin 1 for LCD
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) 
...
//Pin 1 is used for Serial Communication Tx to send the data via the port.
Serial.print(key);
...

要使用Arduino板正确配置LCD显示器,请阅读Arduino官方网站上的文档:LCD 上的HelloWorld

最新更新