Go Down

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

ptool64ar

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


PO'T  :-?

CaptainObvious

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/

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy