how to print text and variables values in the same line with Serial.println in Arduino

how to print text and variables values in the same line with Serial.println in Arduino

There is a quicker way:
Just convert your output directly to a String:

Serial.println((String)x:+x+ y:+y);

String concatenation can be very useful when you need to display a combination of values and the descriptions of those values into one String to display via serial communication.

 int sValor = analogRead(A5); 
 String StrUno = Valor Sensor N°5: ;
 String StrDos = StrUno + sValor ;
 Serial.println(StrDos);  

We can concatenate multiple values, forming a string with all the data and then send it.
This can also be used with LCD dislpay.

how to print text and variables values in the same line with Serial.println in Arduino

Either way, there needs to be an explicit conversion from int to String like seen in Guests post – worked for me in the following way:

String(intVariable)

In the post of user3923880 this is missing and the code does not work in my Arduino IDE (Version 1.8.13). What worked for me, for example:

String outString = stringVar1 + t + String(time) + n; 
Serial.print(outString);

With t being a tab delimiter and n a line break.

Leave a Reply

Your email address will not be published. Required fields are marked *