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?"
Practice
createChar() in Arduino LCD programming?Solution
Step 1: Understand the function of
This function is used to define a new character pattern by specifying pixel rows for the LCD.createChar()Step 2: Compare with other LCD functions
Functions like clearing the screen, setting cursor, or printing strings are different and not related to defining custom characters.Final Answer:
To define a custom character pattern for the LCD -> Option BQuick Check:
createChar()defines custom characters [OK]
- Confusing createChar with print or clear functions
- Thinking createChar moves the cursor
- Assuming createChar prints text directly
createChar()?Solution
Step 1: Check data type and array size
Custom characters require 8 bytes (one for each row) and use the byte type for pixel data.Step 2: Validate array length and type
byte smiley[8] = {0, 10, 0, 0, 17, 14, 0, 0}; uses byte type and 8 elements, matching the LCD custom character format.Final Answer:
byte smiley[8] = {0, 10, 0, 0, 17, 14, 0, 0}; -> Option AQuick Check:
8 bytes array with byte type = correct declaration [OK]
- Using int instead of byte
- Using fewer than 8 elements
- Using char type which is signed and may cause issues
byte heart[8] = {0,10,31,31,31,14,4,0};
lcd.createChar(0, heart);
lcd.setCursor(0,0);
lcd.write(byte(0));Solution
Step 1: Understand createChar and write usage
The code defines a heart pattern, stores it at position 0, then writes character 0 at cursor (0,0).Step 2: Predict LCD output
Writing byte(0) displays the custom character stored at position 0, which is the heart shape.Final Answer:
A heart-shaped custom character appears at the top-left corner -> Option DQuick Check:
createChar + write(byte(0)) = custom char display [OK]
- Expecting numeric 0 to print
- Forgetting to use byte() cast in write()
- Assuming createChar prints automatically
byte star[8] = {4,14,21,4,4,0,4,0};
lcd.createChar(1, star);
lcd.setCursor(0,0);
lcd.write(1);Solution
Step 1: Check usage of lcd.write()
When writing a custom character, the index must be cast to byte, solcd.write(byte(1))is correct.Step 2: Verify other parts
Array size is 8, createChar index 1 is valid (0-7), setCursor(0,0) is valid.Final Answer:
Missing byte cast inlcd.write(1)-> Option AQuick Check:
Use byte() cast in write() for custom chars [OK]
- Not casting index to byte in lcd.write()
- Thinking createChar index must be zero
- Assuming setCursor(0,0) is invalid
Solution
Step 1: Understand createChar indexing
Custom characters are stored in slots 0 to 7. To display two different chars, use two slots, e.g., 0 and 1.Step 2: Display characters correctly
Usewrite(byte(0))andwrite(byte(1))to print the two custom characters side by side.Final Answer:
Define two byte arrays, usecreateChar(0, char1)andcreateChar(1, char2), thenwrite(byte(0))andwrite(byte(1))in order. -> Option CQuick Check:
Two createChar slots + write(byte(index)) = display two chars [OK]
- Trying to store both chars in one array
- Using invalid createChar indexes like 2 or higher without checking
- Not casting index to byte in write()
Ready to prove it?
Pass the test and this pattern moves unknown
