Hardware Reference
In-Depth Information
Those lines of text were put on different lines; why didn't the second text start
on the next line? Well, the compiler wasn't told to do this. To manually insert a
new line, you must use the \n character, for a new line.
Serial.print("Imagination is more important than knowledge.\n");
Serial.print("Albert Einstein");
Now things look better. The text now appears like this:
Imagination is more important than knowledge.
Albert Einstein
That's more like it. Now this quotation is readable. Of course, inserting the
new line escape sequence is going to get boring, especially if some are forgot-
ten. Luckily, there is a function that can do this for you. The println function
automatically adds a new line and a return at the end of the text.
Serial.println("Imagination is more important than knowledge.");
Serial.println("Albert Einstein");
With citations, the author is frequently added on the bottom of the text, but
with an indentation. This too can be added by the tabulation sequence: \t.
Serial.println("Imagination is more important than knowledge.");
Serial.print("\tAlbert Einstein");
Tabulation can be important for data output, as shown in more detail in the
chapter example.
Sending Data
Not all data can be sent as easily as ASCII. If you are trying to output the result
of a sensor, it sometimes isn't practical to convert that data to an int and send
it as text. It takes up more time and is just as easy to send that data as a byte
onto the serial line. Because the default serial connection can send 8 bits of data
per packet, you can send a byte in a single data packet. This is exactly what is
done when l ashing an Arduino; the Arduino IDE doesn't convert your sketch
to ASCII before sending the data; it sends the data 1 complete byte at a time.
Luckily, sending data is just as easy as sending text and can be accomplished
with the write() function. This function accepts either a single byte or a string
to send. It can also accept a buffer as a parameter and a second parameter to
indicate the length of the buffer.
Serial.write(byte);
Serial.write(string);
Serial.write(buffer, len);
 
Search WWH ::




Custom Search