Bird
0
0
Arduinoprogramming~5 mins

Custom characters on LCD in Arduino - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Custom characters on LCD
O(n)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

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
18 byte writes
540 byte writes
1080 byte writes

Pattern observation: The work grows directly with the number of custom characters; doubling characters doubles the work.

Final Time Complexity

Time Complexity: O(n)

This means the time to create custom characters grows linearly with how many characters you make.

Common Mistake

[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.

Interview Connect

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.

Self-Check

"What if we changed the character size from 8 bytes to 16 bytes? How would the time complexity change?"