Why displays enhance projects in Arduino - Performance Analysis
When we add displays to Arduino projects, the program spends time updating what shows on the screen.
We want to know how the time to update the display grows as we show more information.
Analyze the time complexity of the following code snippet.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
}
void loop() {
for (int i = 0; i < 16; i++) {
lcd.setCursor(i, 0);
lcd.print('*');
}
delay(1000);
}
This code prints a row of 16 stars on the display, updating each position one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that updates each of the 16 positions on the display.
- How many times: 16 times per loop cycle, once for each column on the display.
As the number of characters to display grows, the time to update grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 updates |
| 16 | 16 updates |
| 100 | 100 updates |
Pattern observation: The time grows directly with how many characters we update on the display.
Time Complexity: O(n)
This means the time to update the display grows in a straight line with the number of characters shown.
[X] Wrong: "Updating the display always takes the same time no matter how much we show."
[OK] Correct: Actually, each character update takes time, so more characters mean more time spent.
Understanding how display updates affect program speed helps you write smooth, responsive projects that users enjoy.
What if we only update changed characters instead of all? How would the time complexity change?
