2017-08-06 77 views
-5

我是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*) 
    } 
+1

'char x1 =「hello」'你知道這是錯誤的,對嗎?我想你的意思是'std :: string x1 =「hello」'然後就是:'x1 + x2 + x3'。 – DimChtz

回答

0

首先你不能把一個字符變量的字符串,但是,您可以創建一個空終止的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]); 
} 
+0

這將工作,謝謝。但是,是否有任何方法通過組合兩個字符串來顯示變量。 (對我的術語感到抱歉,我對此非常陌生) E.g. lcd.print(「x」+「1」)將打印x1 –

+0

否,但是Serial.print(「x」);然後是Serial.print(「1」)將一起打印。 –

+0

@Andre Medina如果你可以自由使用標準庫,你可以用char *替換std :: string,+運算符就像DimChtz提到的std :: string變量的串聯一樣工作 – Aelgawad