Receiving commands from computer in Arduino - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When an Arduino receives commands from a computer, it often reads data repeatedly. Understanding how the time it takes grows as more data comes in helps us write better programs.
We want to know how the time to process commands changes as the number of commands increases.
Analyze the time complexity of the following code snippet.
void loop() {
while (Serial.available() > 0) {
char command = Serial.read();
// process command
}
}
This code reads all available characters sent from the computer and processes each one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
whileloop that reads each character from the serial buffer. - How many times: Once for each character available to read.
Each new character sent means one more loop iteration to read and process it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 reads and processes |
| 100 | About 100 reads and processes |
| 1000 | About 1000 reads and processes |
Pattern observation: The time grows directly with the number of commands received.
Time Complexity: O(n)
This means the time to read and process commands grows in a straight line with the number of commands.
[X] Wrong: "Reading commands takes the same time no matter how many commands arrive."
[OK] Correct: Each command must be read and processed, so more commands mean more time spent.
Understanding how input size affects processing time helps you write efficient code that handles data smoothly, a skill valuable in many programming tasks.
What if we changed the code to process commands only when a full line is received? How would the time complexity change?
Practice
Serial.begin(9600); in an Arduino sketch?Solution
Step 1: Understand Serial.begin()
Serial.begin(9600);initializes the serial communication at a speed of 9600 bits per second.Step 2: Identify its role in communication
This function sets up the Arduino to send and receive data through the serial port at the specified speed.Final Answer:
It starts serial communication at 9600 bits per second. -> Option BQuick Check:
Serial.begin() = start communication [OK]
- Thinking Serial.begin() reads or sends data
- Confusing Serial.begin() with Serial.read()
- Assuming Serial.begin() stops communication
Solution
Step 1: Identify function to check data availability
Serial.available()returns the number of bytes available to read from the serial buffer.Step 2: Understand usage in condition
Usingif (Serial.available())checks if there is any data to read (non-zero means data is available).Final Answer:
if (Serial.available()) { } -> Option AQuick Check:
Serial.available() checks data presence [OK]
- Using Serial.read() to check availability
- Calling Serial.begin() inside loop
- Using Serial.write() to check data
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
Serial.println(command);
}
}Solution
Step 1: Understand Serial.readStringUntil()
This function reads characters from the serial buffer until it finds the newline character '\n'. It returns the string without the '\n'.Step 2: Analyze the code output
The input "HELLO" followed by Enter sends "HELLO\n". The code reads "HELLO" and prints it exactly.Final Answer:
HELLO -> Option CQuick Check:
readStringUntil('\n') returns string without newline [OK]
- Expecting newline character printed
- Thinking only one character is read
- Assuming no output without delay
void loop() {
if (Serial.available > 0) {
char c = Serial.read();
Serial.print(c);
}
}Solution
Step 1: Check Serial.available usage
Serial.available is a function and must be called with parentheses: Serial.available().Step 2: Verify other function calls
Serial.read() correctly reads one byte without parameters; Serial.print() can print chars.Final Answer:
Serial.available is used without parentheses -> Option AQuick Check:
Functions need parentheses to call [OK]
- Forgetting parentheses on function calls
- Thinking Serial.read() needs parameters
- Assuming Serial.print() can't print chars
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
if (Serial.available()) {
String cmd = Serial.readStringUntil('\n');
// What goes here?
}
}Solution
Step 1: Understand String comparison in Arduino
Arduino String objects overload the == operator to compare contents with string literals like "ON".Step 2: Check each option's correctness
if (cmd == "ON")correctly compares string contents.if (cmd = "ON")performs assignment, not comparison.cmd.equal("ON")fails--no such method (it's equals()).===is invalid C++ syntax.Final Answer:
if (cmd == "ON") digitalWrite(13, HIGH); else if (cmd == "OFF") digitalWrite(13, LOW); -> Option DQuick Check:
Arduino String == compares content [OK]
- Using = instead of == for comparison
- Calling non-existent cmd.equal()
- Using JavaScript === operator in Arduino
