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/readString.adoc
+39
Original file line number
Diff line number
Diff line change
@@ -34,10 +34,49 @@ title: Serial.readString()
34
34
=== Returns
35
35
A `String` read from the serial buffer
36
36
37
+
37
38
--
38
39
// OVERVIEW SECTION ENDS
39
40
40
41
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.
0 commit comments