我是Arduino初学者,我想知道是否有一种简单的方法可以将一系列消息打印到LCD显示屏上。下面是我想要做的一个例子。如何使用for循环打印一系列字符串
char x1 = "hello"
char x2 = "world"
char x3 = "hi"
for(int z = 1; z <= 3; z++){
lcd.setCursor(0,0);
lcd.print(*x1 then x2 then x3*)
}
我是Arduino初学者,我想知道是否有一种简单的方法可以将一系列消息打印到LCD显示屏上。下面是我想要做的一个例子。如何使用for循环打印一系列字符串
char x1 = "hello"
char x2 = "world"
char x3 = "hi"
for(int z = 1; z <= 3; z++){
lcd.setCursor(0,0);
lcd.print(*x1 then x2 then x3*)
}
首先你不能把一个字符变量的字符串,但是,您可以创建一个空终止的C字符串如下:
char* x1 = "hello";
char* x2 = "world";
char* x3 = "hi";
,那么你可以单独打印出来,或者如果你想使用for循环,将它们放在一个数组中,如下所示:
char* sentence[3];
sentence[0] = "hello";
sentence[1] = "world";
sentence[2] = "hi";
for(int i=0; i<sizeof(sentence); i++)
{
// words will be displayed one at a time
lcd.clear();
lcd.setCursor(0,0);
lcd.print(sentence[i]);
}
这将工作,谢谢。但是,是否有任何方法通过组合两个字符串来显示变量。 (对我的术语感到抱歉,我对此非常陌生) E.g. lcd.print(“x”+“1”)将打印x1 –
否,但是Serial.print(“x”);然后是Serial.print(“1”)将一起打印。 –
@Andre Medina如果你可以自由使用标准库,你可以用char *替换std :: string,+运算符就像DimChtz提到的std :: string变量的串联一样工作 – Aelgawad
'char x1 =“hello”'你知道这是错误的,对吗?我想你的意思是'std :: string x1 =“hello”'然后就是:'x1 + x2 + x3'。 – DimChtz