Pages: [1]   Go Down
Author Topic: Using print() with spaces between data on 1 line  (Read 1325 times)
0 Members and 1 Guest are viewing this topic.
ND, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 81
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
Serial.print("word1");      
  Serial.print("\t");              // prints a tab

  Serial.print("word2");  
  Serial.print("\t");      

  Serial.print("word3");
  Serial.print("\t");  

  Serial.print("word4");
  Serial.print("\t");

  Serial.print("word5");
  Serial.print("\t");
  println()


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:
print("word1", "word2"," word3", "word4", "word5");
println();

PO'T  :-?
Logged

Spokane, Washington
Offline Offline
God Member
*****
Karma: 0
Posts: 686
My name is Bob, and I'm an addict.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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("word1");
Serial.print(" ");
Serial.print("word2");

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:
http://arduiniana.org/2009/04/new-streaming-library/
Logged

Pages: [1]   Go Up
Jump to: