Go Down

Topic: Using print() with spaces between data on 1 line (Read 1 time) previous topic - next topic


How can print() be used to print several fields of data on one line, with spaces between each field of data?   for instance:

Code: [Select]
 Serial.print("\t");              // prints a tab





Will this print a row of words 1 - 5 on one line, with spaces between each word.  

What will print("\t");  produce?

Or could one print() be used in front of the words which are seperated by a colon or a comma?  such as:

Code: [Select]
print("word1", "word2"," word3", "word4", "word5");

PO'T  :-?


You can use it like so:
Serial.print("word1 \t");
Serial.print("word2 \t");

Make sure you add that space, that will work for the space, or you can add another line, just for that space:
Serial.print(" ");

If you use Serial.print alot, you should look into the Streaming library, it's mighty fancy! lol can turn the above, into pretty much:

Serial << "word1" << " " << "\t" << "word2" << " " <<"\t" << "word3" << endl;
or isntead of endl, original Serial.println();

or of course:
Serial << "word1 \t" << "word2 \t" << "word3 \t" << endl;

And the awesome thing about it.. it adds 0 bytes on top of your sketch, it just adds a variation of the "print" library, for Serial.print etc it's awesome.

Here's the link, and more examples:

Go Up