Custom characters on LCD in Arduino - Time & Space Complexity
When creating custom characters on an LCD with Arduino, it's important to know how the time to create and display these characters grows as you add more. We want to understand how the program's work changes when we handle multiple custom characters.
How does the time needed change when we add more custom characters?
Analyze the time complexity of the following code snippet.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte smiley[8] = {0x00,0x0A,0x00,0x00,0x11,0x0E,0x00,0x00};
void setup() {
lcd.begin(16, 2);
lcd.createChar(0, smiley);
lcd.setCursor(0, 0);
lcd.write(byte(0));
}
This code creates one custom smiley character and displays it on the LCD.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Writing 8 bytes to define one custom character.
- How many times: Once per custom character created.
Each custom character requires writing 8 bytes to the LCD memory. If you create more characters, the total work grows by 8 bytes per character.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 | 8 byte writes |
| 5 | 40 byte writes |
| 10 | 80 byte writes |
Pattern observation: The work grows directly with the number of custom characters; doubling characters doubles the work.
Time Complexity: O(n)
This means the time to create custom characters grows linearly with how many characters you make.
[X] Wrong: "Creating multiple custom characters takes the same time as creating one."
[OK] Correct: Each character requires writing 8 bytes, so more characters mean more work and more time.
Understanding how time grows with input size helps you write efficient code for devices like LCDs. This skill shows you can think about how your program behaves as it handles more data or tasks.
"What if we changed the character size from 8 bytes to 16 bytes? How would the time complexity change?"
