You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Language/Functions/Communication/Serial/print.adoc
+73-3Lines changed: 73 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -12,21 +12,44 @@
12
12
13
13
[float]
14
14
=== Description
15
+
Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is. For example-
An optional second parameter specifies the base (format) to use; permitted values are `BIN(binary, or base 2)`, `OCT(octal, or base 8)`, `DEC(decimal, or base 10)`, `HEX(hexadecimal, or base 16)`. For floating point numbers, this parameter specifies the number of decimal places to use. For example-
23
+
24
+
* `Serial.print(78, BIN) gives "1001110"` +
25
+
* `Serial.print(78, OCT) gives "116"` +
26
+
* `Serial.print(78, DEC) gives "78"` +
27
+
* `Serial.print(78, HEX) gives "4E"` +
28
+
* `Serial.println(1.23456, 0) gives "1"` +
29
+
* `Serial.println(1.23456, 2) gives "1.23"` +
30
+
* `Serial.println(1.23456, 4) gives "1.2346"`
31
+
32
+
You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :
33
+
34
+
`Serial.print(F(“Hello World”))`
35
+
36
+
To send a single byte, use link:serialWrite{ext-relative}[Serial.write()].
15
37
[%hardbreaks]
16
38
17
39
18
40
[float]
19
41
=== Syntax
20
-
42
+
`Serial.print(val)` +
43
+
`Serial.print(val, format)`
21
44
22
45
23
46
[float]
24
47
=== Parameters
25
-
48
+
`val`: the value to print - any data type
26
49
27
50
[float]
28
51
=== Returns
29
-
Nothing
52
+
`size_t (long)`: `print()` returns the number of bytes written, though reading that number is optional.
30
53
31
54
--
32
55
// OVERVIEW SECTION ENDS
@@ -45,12 +68,59 @@ Nothing
45
68
46
69
[source,arduino]
47
70
----
71
+
/*
72
+
Uses a FOR loop for data and prints a number in various formats.
73
+
*/
74
+
int x = 0; // variable
75
+
76
+
void setup() {
77
+
Serial.begin(9600); // open the serial port at 9600 bps:
78
+
}
79
+
80
+
void loop() {
81
+
// print labels
82
+
Serial.print("NO FORMAT"); // prints a label
83
+
Serial.print("\t"); // prints a tab
84
+
85
+
Serial.print("DEC");
86
+
Serial.print("\t");
87
+
88
+
Serial.print("HEX");
89
+
Serial.print("\t");
90
+
91
+
Serial.print("OCT");
92
+
Serial.print("\t");
93
+
94
+
Serial.print("BIN");
95
+
Serial.print("\t");
96
+
97
+
for(x=0; x< 64; x++){ // only part of the ASCII chart, change to suit
98
+
99
+
// print it out in many formats:
100
+
Serial.print(x); // print as an ASCII-encoded decimal - same as "DEC"
101
+
Serial.print("\t"); // prints a tab
102
+
103
+
Serial.print(x, DEC); // print as an ASCII-encoded decimal
104
+
Serial.print("\t"); // prints a tab
105
+
106
+
Serial.print(x, HEX); // print as an ASCII-encoded hexadecimal
107
+
Serial.print("\t"); // prints a tab
108
+
109
+
Serial.print(x, OCT); // print as an ASCII-encoded octal
110
+
Serial.print("\t"); // prints a tab
48
111
112
+
Serial.println(x, BIN); // print as an ASCII-encoded binary
113
+
// then adds the carriage return with "println"
114
+
delay(200); // delay 200 milliseconds
115
+
}
116
+
Serial.println(""); // prints another carriage return
117
+
}
49
118
----
50
119
[%hardbreaks]
51
120
52
121
[float]
53
122
=== Notes and Warnings
123
+
As of version 1.0, serial transmission is asynchronous; `Serial.print()` will return before any characters are transmitted.
0 commit comments