String parsing from serial input in Arduino - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When reading and parsing strings from serial input on Arduino, it is important to understand how the time taken grows as the input size increases.
We want to know how the program's work changes when the input string gets longer.
Analyze the time complexity of the following code snippet.
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
String input = Serial.readStringUntil('\n');
int length = input.length();
for (int i = 0; i < length; i++) {
char c = input.charAt(i);
// process character c
}
}
}
This code reads a line of text from the serial input, then loops through each character to process it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character of the input string.
- How many times: Once for every character in the input string (length of the string).
As the input string gets longer, the number of characters to process grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character checks and processes |
| 100 | About 100 character checks and processes |
| 1000 | About 1000 character checks and processes |
Pattern observation: The work grows directly with the input size; double the input, double the work.
Time Complexity: O(n)
This means the time to parse grows in a straight line with the length of the input string.
[X] Wrong: "Reading from serial and parsing is always constant time because it just waits for input."
[OK] Correct: The waiting time is separate, but once input is received, parsing depends on how long the input is, so it grows with input size.
Understanding how input size affects parsing time helps you write efficient code for devices with limited speed and memory, a skill valuable in many real projects.
"What if we changed the code to process only every other character? How would the time complexity change?"
Practice
Serial.readStringUntil('\n') do in Arduino programming?Solution
Step 1: Understand the function purpose
Serial.readStringUntil('\n')reads characters from the serial buffer until it finds the newline character '\n'.Step 2: Compare with other options
It does not send data, clear buffer, or read only one character; it reads a full line until newline.Final Answer:
Reads characters from serial input until a newline character is found -> Option AQuick Check:
Read until '\n' means read full line [OK]
- Thinking it reads only one character
- Confusing reading with sending data
- Assuming it clears the buffer
Solution
Step 1: Identify function to read full line
Serial.readStringUntil('\n')reads characters until newline, capturing a full line.Step 2: Understand other functions
Serial.read()reads one byte,Serial.write()sends data, andSerial.available()checks bytes available.Final Answer:
Serial.readStringUntil('\n') -> Option CQuick Check:
Read full line = readStringUntil('\n') [OK]
- Using Serial.read() to get full line
- Confusing read and write functions
- Using Serial.available() to read data
"TEMP:25\n"?
String input = Serial.readStringUntil('\n');
int value = input.substring(5).toInt();
Serial.println(value);Solution
Step 1: Read the input string
The input string is "TEMP:25" (newline removed by readStringUntil).Step 2: Extract substring and convert to integer
input.substring(5)takes characters from index 5 onward, which is "25". ThentoInt()converts "25" to integer 25.Final Answer:
25 -> Option AQuick Check:
Substring from 5 = "25", toInt() = 25 [OK]
- Forgetting substring index starts at 0
- Expecting full string printed
- Not converting substring to int
String data = Serial.readStringUntil('\n');
int num = data.toInt();
if(num = 10) {
Serial.println("Number is 10");
}Solution
Step 1: Check if condition syntax
The conditionif(num = 10)uses assignment '=' instead of comparison '=='. This causes a bug.Step 2: Verify other parts
Semicolon is present,toInt()works correctly, andreadStringUntil('\n')reads input properly.Final Answer:
Using assignment '=' instead of comparison '==' in if condition -> Option DQuick Check:
Use '==' to compare values in if [OK]
- Using '=' instead of '==' in conditions
- Assuming toInt() fails on valid numbers
- Thinking readStringUntil doesn't read input
"CMD:VALUE\n", for example "LED:1\n". How can you parse the command and value separately in Arduino?Solution
Step 1: Read full line input
Serial.readStringUntil('\n')reads the entire line including command and value.Step 2: Extract command and value
input.substring(0,3)extracts the first 3 characters as command (e.g., "LED"), andinput.substring(4).toInt()converts the value part after ':' to integer.Final Answer:
Use String input = Serial.readStringUntil('\n'); String cmd = input.substring(0,3); int val = input.substring(4).toInt(); -> Option BQuick Check:
Substring command and value parsing works [OK]
- Using read() instead of readStringUntil
- Trying to split string with split() which is not available
- Mixing up substring indexes for command and value
