【发布时间】:2020-08-10 13:36:06
【问题描述】:
所以我最终尝试使用 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();
}
}
我不断收到随机和损坏的字符,但我不明白为什么。有人可以帮我吗?
【问题讨论】:
-
不要使用引脚 1,因为它是为 Tx/Rx 保留的!除串行通信外,不得用于任何用途。
-
这绝对是因为您使用
Serial.print功能时的引脚1,并且引脚1 在大多数Arduino 上用作TX。