Why communication protocols matter in Arduino - Performance Analysis
When Arduino devices talk to each other, they use communication protocols. Understanding how these protocols affect the time it takes to send and receive data helps us build faster and more reliable projects.
We want to know how the time to communicate grows as the amount of data increases.
Analyze the time complexity of the following Arduino code snippet.
void sendData(int data[], int size) {
for (int i = 0; i < size; i++) {
Serial.write(data[i]);
delay(1); // simulate transmission time
}
}
This code sends an array of data bytes one by one over a serial connection, with a small delay to simulate transmission time.
- Primary operation: Sending each data byte using Serial.write()
- How many times: Once for each element in the data array (size times)
As the number of data bytes increases, the total time to send all bytes grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 sends |
| 100 | 100 sends |
| 1000 | 1000 sends |
Pattern observation: Doubling the data doubles the time needed to send it.
Time Complexity: O(n)
This means the time to send data grows directly with the amount of data you want to send.
[X] Wrong: "Sending more data doesn't take much longer because the delay is very small."
[OK] Correct: Even small delays add up when repeated many times, so sending more data always takes more time.
Knowing how communication time grows helps you design better Arduino projects and explain your choices clearly in conversations about device communication.
"What if we removed the delay inside the loop? How would the time complexity change?"
