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?
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 }
Look at the binary pattern and which segments correspond to each bit.
The binary pattern 0b01011011 corresponds to segments a, b, d, e, and g lit, which forms the digit '2' on a common cathode display.
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?
Remember the order is a, b, c, d, e, f, g in the array.
The array index 6 corresponds to segment 'g'. Since arrays start at 0, segment 'g' is at segmentPins[6], which is pin 8.
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?
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 }
Check how HIGH and LOW relate to turning on segments for common cathode vs anode.
For a common cathode display, segments turn on when the pin is set HIGH. The code sets pins LOW to turn on segments, so no segments light up.
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
Look carefully at parentheses and semicolons.
The original code misses a closing parenthesis for digitalWrite's arguments, causing a syntax error.
Given the standard seven-segment display segments labeled a to g, how many segments must be lit to display the digit '8'?
Digit '8' lights all segments on a seven-segment display.
The digit '8' lights all seven segments (a, b, c, d, e, f, g) on the display.
