Bird
0
0
Arduinoprogramming~20 mins

Seven-segment display control in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Seven-Segment Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output on the seven-segment display?

Consider this Arduino code snippet controlling a common cathode seven-segment display connected to pins 2 to 8. What digit will be shown on the display?

Arduino
const int segmentPins[] = {2, 3, 4, 5, 6, 7, 8};
// Segments a,b,c,d,e,f,g
const byte digit2Segments = 0b01011011; // segments to light for digit '2'

void setup() {
  for (int i = 0; i < 7; i++) {
    pinMode(segmentPins[i], OUTPUT);
  }
  for (int i = 0; i < 7; i++) {
    bool segmentOn = digit2Segments & (1 << i);
    digitalWrite(segmentPins[i], segmentOn ? HIGH : LOW);
  }
}

void loop() {
  // nothing here
}
ADigit '3' is displayed
BNo segments light up
CDigit '5' is displayed
DDigit '2' is displayed
Attempts:
2 left
💡 Hint

Look at the binary pattern and which segments correspond to each bit.

🧠 Conceptual
intermediate
1:00remaining
Which pin controls the 'g' segment in this setup?

Given the array const int segmentPins[] = {2, 3, 4, 5, 6, 7, 8}; representing segments a to g respectively, which pin controls the 'g' segment?

APin 5
BPin 8
CPin 7
DPin 2
Attempts:
2 left
💡 Hint

Remember the order is a, b, c, d, e, f, g in the array.

🔧 Debug
advanced
2:30remaining
Why does the display show nothing?

This Arduino code is supposed to display digit '1' on a common cathode seven-segment display, but the display stays off. What is the cause?

Arduino
const int segmentPins[] = {2, 3, 4, 5, 6, 7, 8};
const byte digit1Segments = 0b00000110; // segments b and c

void setup() {
  for (int i = 0; i < 7; i++) {
    pinMode(segmentPins[i], OUTPUT);
  }
  for (int i = 0; i < 7; i++) {
    bool segmentOn = digit1Segments & (1 << i);
    digitalWrite(segmentPins[i], segmentOn ? HIGH : LOW);
  }
}

void loop() {
  // nothing
}
APins are set LOW to turn on segments, but it's a common cathode display needing HIGH
BPins are set HIGH to turn on segments, but it's a common anode display needing LOW
CPin modes are not set to OUTPUT
DThe bitmask for digit '1' is incorrect
Attempts:
2 left
💡 Hint

Check how HIGH and LOW relate to turning on segments for common cathode vs anode.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this seven-segment control code?

Identify the option that fixes the syntax error in this Arduino code snippet:

for (int i = 0; i < 7; i++)
  digitalWrite(segmentPins[i], digitSegments & (1 << i) ? HIGH : LOW
ARemove the ternary operator and write digitalWrite(segmentPins[i], HIGH);
BReplace digitalWrite with digitalRead
CAdd a closing parenthesis at the end: digitalWrite(segmentPins[i], digitSegments & (1 << i) ? HIGH : LOW);
DAdd a semicolon after the for loop header
Attempts:
2 left
💡 Hint

Look carefully at parentheses and semicolons.

🚀 Application
expert
1:00remaining
How many segments light up for digit '8' on a common cathode display?

Given the standard seven-segment display segments labeled a to g, how many segments must be lit to display the digit '8'?

A7 segments
B5 segments
C6 segments
D4 segments
Attempts:
2 left
💡 Hint

Digit '8' lights all segments on a seven-segment display.