EEPROM for storing settings in Arduino - Time & Space Complexity
When using EEPROM to save settings on an Arduino, it's important to know how the time to save or read data changes as the amount of data grows.
We want to understand how the program's running time changes when storing more settings.
Analyze the time complexity of the following code snippet.
#include <EEPROM.h>
void saveSettings(int* settings, int size) {
for (int i = 0; i < size; i++) {
EEPROM.write(i, settings[i]);
}
}
This code saves an array of settings into EEPROM memory, one byte at a time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Writing each setting byte to EEPROM inside a for-loop.
- How many times: The loop runs once for each setting, so it repeats size times.
As the number of settings increases, the time to save them grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 writes to EEPROM |
| 100 | 100 writes to EEPROM |
| 1000 | 1000 writes to EEPROM |
Pattern observation: Doubling the number of settings doubles the time needed to save them.
Time Complexity: O(n)
This means the time to save settings grows directly with the number of settings you want to store.
[X] Wrong: "Writing one byte to EEPROM takes the same time no matter how many bytes I write overall."
[OK] Correct: Each byte write takes time, so writing more bytes adds up and takes longer overall.
Understanding how saving data scales helps you design efficient programs that handle settings well, a useful skill in many embedded projects.
"What if we only write to EEPROM when a setting changes? How would the time complexity change?"
Practice
EEPROM in an Arduino program?Solution
Step 1: Understand EEPROM functionality
EEPROM memory keeps data saved even if the Arduino loses power.Step 2: Compare options with EEPROM purpose
Only To store data permanently even when the power is off describes permanent data storage, which matches EEPROM use.Final Answer:
To store data permanently even when the power is off -> Option BQuick Check:
EEPROM stores permanent data = A [OK]
- Confusing EEPROM with RAM (temporary memory)
- Thinking EEPROM speeds up the program
- Mixing EEPROM with internet or LED control
Solution
Step 1: Recall EEPROM write syntax
The correct function to save data isEEPROM.write(address, value);.Step 2: Match syntax with options
Only EEPROM.write(10, 42); usesEEPROM.writewith correct parameter order.Final Answer:
EEPROM.write(10, 42); -> Option CQuick Check:
Write uses EEPROM.write(address, value) = D [OK]
- Using EEPROM.read() to write data
- Using non-existent functions like EEPROM.store()
- Swapping address and value parameters
EEPROM.write(5, 100); int val = EEPROM.read(5); Serial.println(val);
Solution
Step 1: Understand EEPROM write and read
The code writes 100 to address 5, then reads from address 5.Step 2: Predict Serial output
Reading address 5 returns 100, so Serial.println prints 100.Final Answer:
100 -> Option DQuick Check:
Write 100, read 100 = A [OK]
- Expecting read to return address instead of value
- Confusing EEPROM.read() with EEPROM.write()
- Assuming default value 0 without write
int setting = 25; EEPROM.read(0, setting);
Solution
Step 1: Check function purpose
EEPROM.read() reads data; EEPROM.write() saves data.Step 2: Identify misuse in code
The code uses EEPROM.read() with two parameters, which is incorrect for saving.Final Answer:
Using EEPROM.read() instead of EEPROM.write() to save data -> Option AQuick Check:
Write needs EEPROM.write(), not EEPROM.read() = C [OK]
- Confusing read and write functions
- Passing wrong number of parameters
- Assuming EEPROM.read() can save data
Solution
Step 1: Save volume to EEPROM address 20
UseEEPROM.write(20, volume);to store the value.Step 2: Read volume back from address 20
UseEEPROM.read(20);to get the saved value.Final Answer:
EEPROM.write(20, volume); int vol = EEPROM.read(20); -> Option AQuick Check:
Write then read from same address = B [OK]
- Swapping address and value parameters
- Using non-existent EEPROM.save() or EEPROM.load()
- Reading before writing data
Ready to prove it?
Pass the test and this pattern moves unknown
