Arduino sleep modes - Time & Space Complexity
When using Arduino sleep modes, we want to know how the time the program spends sleeping or waking changes as we add more tasks or checks.
We ask: How does the program's running time grow when we use sleep modes with different input sizes?
Analyze the time complexity of the following code snippet.
#include <avr/sleep.h>
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
sleep_cpu();
sleep_disable();
}
This code blinks an LED and then puts the Arduino into idle sleep mode once every loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop() function runs repeatedly, blinking the LED and then sleeping.
- How many times: It runs forever, repeating the blink and sleep cycle each time.
Here, the "input size" can be thought of as how many times the loop runs or how many tasks are added inside the loop.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 blink and sleep cycles |
| 100 | About 100 blink and sleep cycles |
| 1000 | About 1000 blink and sleep cycles |
Pattern observation: The total time grows linearly with the number of cycles because each cycle takes roughly the same time.
Time Complexity: O(n)
This means the total running time grows directly in proportion to how many times the loop runs.
[X] Wrong: "Using sleep mode makes the program run instantly or faster regardless of loop size."
[OK] Correct: Sleep mode saves power but the loop still runs each cycle, so total time grows with the number of cycles.
Understanding how sleep modes affect program timing helps you write efficient embedded programs and shows you can think about how code scales in real devices.
"What if we added more tasks inside the loop before sleeping? How would the time complexity change?"
Practice
Solution
Step 1: Understand sleep mode setup
The functionset_sleep_mode()is used to choose which sleep mode the Arduino will enter.Step 2: Differentiate from sleep execution
The functionsleep_cpu()actually puts the CPU to sleep, but does not set the mode.Final Answer:
set_sleep_mode() -> Option AQuick Check:
Set mode = set_sleep_mode() [OK]
- Confusing set_sleep_mode() with sleep_cpu()
- Thinking attachInterrupt() sets sleep mode
- Using delay() to enter sleep
Solution
Step 1: Identify the sleep function
The functionsleep_cpu()is the correct call to put the CPU into sleep mode after setting it.Step 2: Check other options
sleep_mode()is not a valid function, nor aresleep()orenter_sleep().Final Answer:
sleep_cpu(); -> Option BQuick Check:
Sleep call = sleep_cpu() [OK]
- Using sleep_mode() instead of sleep_cpu()
- Trying to call sleep() which doesn't exist
- Confusing function names
set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_cpu(); // CPU sleeps here sleep_disable();
Solution
Step 1: Analyze sleep mode setting
The code sets the sleep mode to power-down withset_sleep_mode(SLEEP_MODE_PWR_DOWN);.Step 2: Understand sleep flow
Callingsleep_enable()thensleep_cpu()puts the CPU to sleep in power-down mode. It stays asleep until an interrupt or reset wakes it. After waking,sleep_disable()disables sleep.Final Answer:
The CPU enters power-down sleep mode and stays asleep until an interrupt wakes it -> Option AQuick Check:
Power-down sleep = stays asleep until interrupt [OK]
- Assuming CPU wakes immediately without interrupt
- Forgetting sleep_enable() disables sleep
- Confusing idle mode with power-down
set_sleep_mode(SLEEP_MODE_IDLE); sleep_cpu(); sleep_enable();
Solution
Step 1: Check function order
The functionsleep_enable()must be called beforesleep_cpu()to enable sleep mode properly.Step 2: Verify other parts
set_sleep_mode()is correctly called with a valid mode.sleep_cpu()is the correct function to enter sleep.Final Answer:
sleep_enable() must be called before sleep_cpu() -> Option DQuick Check:
Enable sleep before sleeping [OK]
- Calling sleep_cpu() before sleep_enable()
- Confusing sleep_cpu() with sleep_mode()
- Missing parameters in set_sleep_mode()
Solution
Step 1: Choose lowest power sleep mode
SLEEP_MODE_PWR_DOWN uses the least power by shutting down most parts of the chip.Step 2: Select wake method
To wake from power-down mode, an external interrupt on pin 2 (using attachInterrupt) is needed because polling or delay() won't wake the CPU.Final Answer:
Use SLEEP_MODE_PWR_DOWN and attach an external interrupt on pin 2 -> Option CQuick Check:
Lowest power + external interrupt wake = Use SLEEP_MODE_PWR_DOWN and attach an external interrupt on pin 2 [OK]
- Using idle mode which uses more power
- Trying to wake with delay() which doesn't work
- Polling button instead of using interrupt
Ready to prove it?
Pass the test and this pattern moves unknown
