Why power matters for battery projects in Arduino - Performance Analysis
When working on battery-powered Arduino projects, it's important to understand how power use grows as your code runs.
We want to see how the program's work affects battery life over time.
Analyze the time complexity of the following code snippet.
void loop() {
for (int i = 0; i < 1000; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(1);
digitalWrite(LED_BUILTIN, LOW);
delay(1);
}
}
This code blinks an LED 1000 times with short delays, using power each time it runs.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop blinking the LED 1000 times.
- How many times: Exactly 1000 times each time loop() runs.
As the number of blinks increases, the time and power used grow directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 LED blinks |
| 100 | 100 LED blinks |
| 1000 | 1000 LED blinks |
Pattern observation: Doubling the number of blinks doubles the work and power used.
Time Complexity: O(n)
This means the work and power use grow in a straight line with the number of blinks.
[X] Wrong: "Adding more blinks won't affect battery life much because each blink is very short."
[OK] Correct: Even short blinks add up; more blinks mean more power used and shorter battery life.
Understanding how your code's work affects power helps you design better battery projects and shows you think about real-world impacts.
"What if we replaced the delay with a non-blocking timer? How would the time complexity and power use change?"