Seven-segment display control in Arduino - Time & Space Complexity
When controlling a seven-segment display, we want to know how the time to update the display changes as we handle more digits.
We ask: How does the work grow when we show more numbers?
Analyze the time complexity of the following code snippet.
const int segmentPins[7] = {2, 3, 4, 5, 6, 7, 8};
const byte digits[10][7] = {
{1,1,1,1,1,1,0}, // 0
{0,1,1,0,0,0,0}, // 1
// ... other digits
};
void displayDigit(int digit) {
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], digits[digit][i]);
}
}
This code lights up the correct segments to show one digit on a seven-segment display.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs 7 times to set each segment.
- How many times: Always 7 times per digit displayed, no matter which digit.
Each digit requires turning on or off 7 segments, so the work per digit is fixed.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 digit | 7 operations |
| 10 digits | 70 operations |
| 100 digits | 700 operations |
Pattern observation: The work grows directly with the number of digits; more digits mean more segments to update.
Time Complexity: O(n)
This means the time to update the display grows in a straight line as you show more digits.
[X] Wrong: "Updating one digit takes longer if the digit is bigger or more complex."
[OK] Correct: The code always updates all 7 segments regardless of the digit, so time stays the same for any digit.
Understanding how loops affect performance helps you explain how your code scales when controlling hardware like displays.
"What if we changed the code to update only the segments that need to change from the previous digit? How would the time complexity change?"
Practice
Solution
Step 1: Understand seven-segment display function
Each segment lights up to form numbers by controlling its pin.Step 2: Purpose of controlling pins
Setting pins HIGH or LOW turns segments on or off to show digits.Final Answer:
To turn on or off specific segments to display digits -> Option AQuick Check:
Control pins = display digits [OK]
- Thinking pins read data instead of controlling segments
- Confusing display power with pin control
- Assuming pins connect to internet
Solution
Step 1: Identify correct function to set pin mode
pinMode(pin, mode) sets pin as INPUT or OUTPUT.Step 2: Choose correct mode for controlling segments
Segments need OUTPUT mode to send signals.Final Answer:
pinMode(7, OUTPUT); -> Option CQuick Check:
pinMode with OUTPUT sets pin to control segments [OK]
- Using INPUT instead of OUTPUT for control
- Confusing digitalWrite with pinMode
- Trying to read pin instead of setting mode
pinMode(8, OUTPUT); digitalWrite(8, HIGH);What will happen to the segment connected to pin 8?
Solution
Step 1: Analyze pinMode and digitalWrite commands
pinMode(8, OUTPUT) sets pin 8 to output mode; digitalWrite(8, HIGH) sends voltage.Step 2: Effect on seven-segment segment
Sending HIGH voltage turns the connected segment on.Final Answer:
The segment will turn on -> Option BQuick Check:
digitalWrite HIGH = segment ON [OK]
- Thinking HIGH turns segment off
- Assuming blinking without delay code
- Confusing pinMode with digitalWrite effects
pinMode(2, OUTPUT); pinMode(3, OUTPUT); digitalWrite(2, LOW); digitalWrite(3, LOW);Why does the display not show the digit '1' as expected?
Solution
Step 1: Understand common cathode display logic
Common cathode means segments turn on when pins are set HIGH.Step 2: Analyze digitalWrite commands
Setting pins LOW turns segments off, so digit '1' won't show.Final Answer:
Pins should be set to HIGH to turn on segments on a common cathode display -> Option AQuick Check:
Common cathode ON = HIGH signal [OK]
- Setting pins LOW instead of HIGH for common cathode
- Thinking pins 2 and 3 can't be outputs
- Ignoring pinMode setup
Solution
Step 1: Understand common anode display logic
Common anode means segments turn on when pins are LOW (connected to ground).Step 2: Control segments for digits 0-9
Set pins LOW for segments to light, HIGH to turn off, update pins per digit pattern.Final Answer:
Set segment pins LOW to turn on segments, HIGH to turn off, and update pins for each digit -> Option DQuick Check:
Common anode ON = LOW signal [OK]
- Using HIGH to turn on segments on common anode
- Using analogWrite instead of digitalWrite for digits
- Connecting all pins to 5V without control
Ready to prove it?
Pass the test and this pattern moves unknown
