// SerialReadString.ino // Reads multiple chars from Serial until nothing received for 5mS (good for 9600baud and above) // https://www.forward.com.au/pfod/ArduinoProgramming/SoftwareSolutions/index.html //Pros: Simple. Robust in that it will return all the available input even if there is no line ending specified. No delay if nothing to read. // Small delay if reading input. Use 115200 or higher baud rate to reduce the reading delay to <15mS for 80 chars input. //Cons: Blocking while reading, but increasing the baud rate minimizes that. This approach temporarily uses twice the RAM as Serial.readString( ) creates a String the size of the input and then copies it to input before releasing the String read in. // Useful for trivial sketches that only do one thing and have enough memory to hold the input String twice. void setup() { Serial.begin(9600); Serial.setTimeout(10); // at 9600 get about 1char/1mS so when reading user input from Arduino IDE monitor can set this short timeout for (int i = 10; i > 0; i--) { Serial.print(' '); Serial.print(i); delay(500); } Serial.println(); Serial.println(F("SerialReadString.ino")); } void processInput() { if (Serial.available()) { // skip readString timeout if nothing to read String input = Serial.readString(); input.trim(); // strip off any leading/trailing space and \r \n if (input.length() > 0) { // got some input handle it Serial.print(F(" got a some input '")); Serial.print(input); Serial.println("'"); // parse input here and set globals with results } } } void loop() { processInput(); // handle the Serial in this task // other tasks here }