Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arduino-ide

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.

like image 907
Salvador Rueda Avatar asked Aug 16 '16 19:08

Salvador Rueda


1 Answers

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

Serial.println((String)"x:"+x+" y:"+y);
like image 185
Guest Avatar answered Sep 18 '22 05:09

Guest