how to print text and variable's values in the same line with Serial.println in Arduino

Salvador Rueda picture Salvador Rueda · Aug 16, 2016 · Viewed 69k times · Source

I've this code:

 Serial.print("x:");
 Serial.print(x);
 Serial.print(" y: ");
 Serial.println(y);

and works fine. There's an example of the output:

x:41 y: 31

but I wonder if there's a way to write the four sentences in one with something like:

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

that returns an error:

invalid operands of types 'const char*' and 'const char [4]' to binary 'operator+'

Any idea?. Thanks in advance.

Answer

Guest picture Guest · Oct 10, 2018

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

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