Arduino 液晶屏显示随机字符

Arduino 液晶屏显示随机字符,arduino,lcd,Arduino,Lcd,所以我最终尝试用一个4x4数字键盘、arduino和一个螺线管来构建一个有趣的安全系统。当我试着让数字键盘和液晶显示器一起工作时,我总是遇到一些我不知道的问题。以下代码是我目前掌握的代码: #include <LiquidCrystal.h> // includes the LiquidCrystal Library #include <Keypad.h> LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC obj

所以我最终尝试用一个4x4数字键盘、arduino和一个螺线管来构建一个有趣的安全系统。当我试着让数字键盘和液晶显示器一起工作时,我总是遇到一些我不知道的问题。以下代码是我目前掌握的代码:

#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();
  }
}

#include//包括液晶库
#包括
液晶液晶显示器(1,2,4,5,6,7);//创建一个LC对象。参数:(rs、enable、d4、d5、d6、d7)
//_________________________________________
常量字节行=4//键盘的行数和列数
常量字节cols=4;
char keyMap[rows][cols]={//定义键盘按钮上的cymbol
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*'、'0'、'#'、'D'}
};
字节rowPins[行]={1,2,3,4}//键盘上的针脚
字节colPins[cols]={5,6,7,8};
小键盘myKeypad=小键盘(makeyMap(keyMap)、行PIN、列PIN、行、列);
//_________________________________________
无效设置(){
Serial.begin(9600);
lcd.begin(16,2);//初始化lcd屏幕的接口,并指定显示器的尺寸(宽度和高度)}
}
void loop(){
char key=myKeypad.getKey();
如果(关键){
串行打印(密钥);
打印(“按键已按下!”);
延迟(2000年);
lcd.clear();
}
}
但我一直在得到随机的和破碎的字符,我不明白为什么。有人能帮我吗


您的LCD显示屏没有显示预期的字符串,因为您正在重叠用于其他任务的pin。
大多数Arduino板上的引脚1用作串行变送器(Tx)引脚。同样的引脚也发生在你的LCD显示器的一个引脚上(rs引脚)。这会导致LCD上出现意外行为和乱码文本

//LCD的引脚1
液晶液晶显示器(1,2,4,5,6,7);//创建一个LC对象。参数:(rs、enable、d4、d5、d6、d7)
...
//引脚1用于串行通信Tx,通过端口发送数据。
串行打印(密钥);
...

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

不要使用引脚1,因为它是为Tx/Rx保留的!除串行通信外,不得将其用于任何用途。这肯定是因为您使用的是插脚1
serial.print
功能,而插脚1在大多数Arduino上用作TX。非常感谢!我知道这是一件很无聊的事,但我非常感激。再次感谢!