Serial.println("Hello " + worldvarible + "\t" + VARIABLE2); how do I output mult

Hi all,

I'm trying to output a table of data I'm happy to manually format it using "/t" but I cant make the arduino output a Serial.print with text, then a variable.

Thanks for any help. I know its a simple piece of code like && or + or ++ or something but I can't find a reference anywhere! Anyone have any ideas?

Thanks R

There is a Streaming library that does what you want.

there must be a way of doing it without a lib its a standard C operator I just can't remeber how

I want it to say

"Your input is : 25" "Your input is : 55" Code would be something liek Serial.println("Your input is : " + variable);

http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/ possibly?

I voted Yes (Thank you) as it seemed more polite.

THATS THE ONE !

Thanks :-) Forgot that you had to put place holders in!

there must be a way of doing it without a lib its a standard C operator I just can't remeber how

There is no standard C operator that concatenates a string and an int in any meaningful fashion, which explains why you can't remember what it is.

Serial.println(“hahahaha I WIN : %i”, banana);

and it dosen’t work :frowning:

Error:

sketch_jan25a:10: error: call of overloaded ‘println(const char [20], int&)’ is ambiguous
Print::println(long unsigned int, int)

mellink: THATS THE ONE !

Thanks :-) Forgot that you had to put place holders in!

If you want a tutorial on sprintf, I just wrote one for my blog:

Down-to-earth tutorial for mere mortals (thanks to RobertBerger for not registering his catch phrase as trademark!)

http://liudr.wordpress.com/2012/01/16/sprintf/

THATS THE ONE !

That's a function, not "a standard C operator".

mellink:
Serial.println(“hahahaha I WIN : %i”, banana);

and it dosen’t work :frowning:

Error:

sketch_jan25a:10: error: call of overloaded ‘println(const char [20], int&)’ is ambiguous
Print::println(long unsigned int, int)

That’s because you didn’t use sprintf().

PaulS:

THATS THE ONE !

That's a function, not "a standard C operator".

Java programming has an overloaded '+' operator that does concatenation on string and .toString() on numbers so it works in Java. Not sure if C++ even has that or is able to overload an operator that way. I always thought when you add strings with numbers, strings are converted into numbers (maybe the first character) and added to numbers, not the other way.

Not sure if C++ even has that or is able to overload an operator that way.

It has the ability to overload operators. However, it is rarely needed, since the << operator can output just about any kind of data. That is, of course, what the Streaming library brings to the Arduino.

I do not understand why I need to include a libary for just printing some text to a serial window.

Ok so if I have to how do I go about adding the libary and using it?

I do not understand why I need to include a libary for just printing some text to a serial window.

You don't. You are free to put one piece of data at a time to the serial window.

Serial.print("Hello ");
Serial.print(worldVariable);
Serial.print("\t");
Serial.print(VARIABLE2);

If you want to do it in one step, you have choices. You can use sprintf() to create an array of characters in one step. Or, you can use strcat() and itoa() and ftoa() to build the array piece by piece. Or, you can use the Streaming library.

Your choice.

mellink: I do not understand why I need to include a libary for just printing some text to a serial window.

Ok so if I have to how do I go about adding the libary and using it?

Arduino IDE includes a half dozen libraries (which each includes half dozen libraries) for you. The Streaming library is among them so no work on your part. I know the library that has sprintf (stdlib?) is also included by default.

If you've programed anything in C, you should know to include libraries in order to use their functions, which is not much effort. It's a one-line instruction.