Arduino(C/C++)代码在LCD上显示阵列的内容

Arduino (C/C++) Code To Display Contents of Array on LCD

本文关键字:显示 阵列 LCD C++ 代码 Arduino      更新时间:2023-10-16

在发布这篇文章之前,我已经尽可能多地进行了研究,但我是编程新手,所以我的普遍无知使我无法真正知道如何提出正确的问题。

当前目标:

  1. 构建一个存储50多个英语单词/短语的数组
  2. 访问我的Arduino上的数组,并在我的LCD上显示单个单词/短语;以及
  3. 点击Arduino上的按钮切换单词/短语

硬件规格:SainSmart UnoR3,基于HD44780 的液晶显示器

问题:编写一个代码,当我按下按钮时会显示一个新词。

"你好,世界!"LCD的代码

void setup() {
 // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
}
void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/1000);
}

数组中随机字符串的代码

#include <stdio.h>
#include <stdlib.h>
int main() {
    const char *messages[] = {
        "Hello!",
        "How are you?",
        "Good stuff!"
    };
    const size_t messages_count = sizeof(messages) / sizeof(messages[0]);
    char input[64];
    while (1) {
        scanf("%63s", input);
        printf("%sn", messages[rand() % messages_count]);
    }
    return 0;
}

我还有一个Arduino Uno和一个LCD显示屏。您的任务将是调试硬件和软件。所以,让我问一些问题。

在您的代码列表中,当您运行草图时,LCD上会显示"你好,世界!"吗?

您提供的main()与此问题有何关联。具体来说,main()在哪里运行?我希望这不是你素描的一部分!!

在您的loop()中,您确实没有有延迟。在一个刚起步的程序员。。。通常在显示内容时,您需要暂停几秒钟,否则您将以每秒数千次的更改来驱动LCD。

因此,添加一个delay(3000);语句,在LCD更新之间延迟3秒(3000毫秒)。

接下来,在"loop()"中,您需要测试是否按下了按钮,但现在只需要让LCD显示即可。

请做这些事情,并相应地更新你的问题,我会跟进更多的建议/问题。