如何使用函数写入 LCD 文本? - 阿杜诺

How can I use a function to write LCD text? - Arduino

我正在尝试创建一种数字选择器。它会显示 0 到 9 并使用 X,我将把它写在数字上,您可以选择一个数字。例如 01234567x9 或 0123x56789。然而,我正在努力将其打印到 LCD 上。我使用的是 tinkercad,所以我所有的引脚连接都是预定义的并且非常正确。我使用标准的 tinkercad LCD 和 LiquidCrystal 作为库。

但是当我在 void loop() 中使用 lcd.print("Hello World) 时,它会连续打印 Hello World,显然是因为我将它放在一个循环中。我已经尝试将它放入一个函数中,但我无法让它正常工作...如果有一些帮助将是完美的。

这是我目前正在使用的代码,returns“函数 'void PrintText()' 的参数太多”作为错误。

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char lcdtxt[] = "0123456789 x";

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
 
}
void PrintText(){
   lcd.print(lcdtxt);
   lcd.clear();
  }
void loop() {
  PrintText(lcdtxt);


}

看到你的代码,你有一个全局字符数组来保存要写入的内容,PrintText() 将从那里获取它的输入:

char lcdtxt[] = "0123456789 x";

void PrintText(){
   lcd.print(lcdtxt);
   lcd.clear();
  }

void loop() {
  PrintText(lcdtxt);
}

所以,PrintText() 不会接受任何参数,而你用一个参数调用它。在这种情况下,错误消息会准确告诉您发生了什么。

要修复您的代码,您应该只删除 `PrintText()' 调用的参数,因为我知道您已经初始化了数组。

void loop() {
  PrintText();
}

您应该定义一个 PrintText() 函数接受 const char * 并将其发送到 LCD,无论如何。

关于 hello world 不断重复,我会将对 PrintText() 的调用移动到 setup(),因为您不需要这种行为。或者可能将其留在 loop() 中并通过 exit(0).

停止