Embedded Programming‎ > ‎Examples‎ > ‎



/* Serial output

This example shows how to print out values you may create in your running
program. This is useful to debug a program or to see what some intermediate
program variables are.

In the program below we first define some arbitrary variables. Some are
floating point, others are integers. In the setup section we establish the
so-called 'baudrate', which is the rate (bits per second) at which
communications will occur over the USB cable between the Arduino and
your PC. We also print out a heading. You put literal text between
parentheses, like "abcde".

In the loop section we first send the current value of the millisecond timer,
and then we send the 5 values. Notice that for the floating point variables
you can specify how many digits to show to the right of the decimal point. The
truncated values are automatically rounded up/down as appropriate. For the
integers the whole value gets sent.

Also notice that we send a comma after each value, so that the data can later
be easily read into a spreadsheet as a comma-delimited text file.

We then delay for 2 seconds, and do it all over again.

Note: Refer to the superSerial library for how to do serial input to the Arduino.

float value1 = 1.2345, value2=2.34567, value3=3.45678; // create floating pt variables
int value4 = 1234, value5 = 2345; // and some integers

void setup() {
Serial.begin(115200); // sets the baudrate (communications rate, bits/s)
Serial.println("Example serial output...") // print out a heading

void loop() {
Serial.print (millis()); Serial.print(","); // print millisecond timer value
Serial.print (value1,3); Serial.print(","); // print value1 to 3 decimal places
Serial.print (value2,2); Serial.print(","); // print value2 to 2 decimal places
Serial.print (value3,3); Serial.print(","); // print value3 to 3 decimal places
Serial.print (value4); Serial.print(","); // print value4, an integer
Serial.println(value5); // print value5, using the println function to
// put a 'carriage return' (newline) character at the end
delay(2000); // burn up 2 seconds