Skip to content

Commit f7296fb

Browse files
Add readString example and notes/warnings re: line endingssections (#808)
A very common mistake is not expecting readString value to have invisible \r \n characters.
1 parent ac98490 commit f7296fb

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Language/Functions/Communication/Serial/readString.adoc

+39
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,49 @@ title: Serial.readString()
3434
=== Returns
3535
A `String` read from the serial buffer
3636

37+
3738
--
3839
// OVERVIEW SECTION ENDS
3940

4041

42+
// HOW TO USE SECTION STARTS
43+
[#howtouse]
44+
--
45+
46+
[float]
47+
=== Example Code
48+
Demonstrate Serial.readString()
49+
50+
[source,arduino]
51+
----
52+
void setup() {
53+
Serial.begin(9600);
54+
}
55+
56+
void loop() {
57+
Serial.println("Enter data:");
58+
while (Serial.available() == 0) {} //wait for data available
59+
String teststr = Serial.readString(); //read until timeout
60+
teststr.trim(); // remove any \r \n whitespace at the end of the String
61+
if (teststr == "red") {
62+
Serial.println("A primary color");
63+
} else {
64+
Serial.println("Something else");
65+
}
66+
}
67+
----
68+
[%hardbreaks]
69+
70+
71+
[float]
72+
=== Notes and Warnings
73+
The function does not terminate early if the data contains end of line characters. The returned `String` may contain carriage return and/or line feed characters if they were received.
74+
[%hardbreaks]
75+
76+
--
77+
// HOW TO USE SECTION ENDS
78+
79+
4180
// SEE ALSO SECTION
4281
[#see_also]
4382
--

0 commit comments

Comments
 (0)