0
0
Embedded Cprogramming~20 mins

LED control patterns in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LED Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output pattern of this LED control code?

Consider an 8-LED array controlled by this code snippet. What pattern will the LEDs show after running the loop?

Embedded C
for (int i = 0; i < 8; i++) {
    PORT = (1 << i);
    _delay_ms(100);
}
ALEDs light up one by one from right to left, only one LED on at a time.
BAll LEDs turn on simultaneously and stay on.
CLEDs light up one by one from left to right, only one LED on at a time.
DLEDs blink all together on and off repeatedly.
Attempts:
2 left
💡 Hint

Look at the bit shift operation and how it affects the PORT value.

Predict Output
intermediate
2:00remaining
What will be the final PORT value after this LED pattern code?

This code runs a pattern on an 8-LED array. What is the final value of PORT after the loop finishes?

Embedded C
PORT = 0;
for (int i = 0; i < 8; i++) {
    PORT |= (1 << i);
    _delay_ms(50);
}
A1 (only the first LED on)
B128 (only the last LED on)
C0 (all LEDs off)
D255 (all LEDs on)
Attempts:
2 left
💡 Hint

Think about how the OR operation accumulates bits.

🔧 Debug
advanced
2:00remaining
Why does this LED blinking code fail to light LEDs correctly?

Identify the error in this LED blinking code that causes no LEDs to light up.

Embedded C
for (int i = 0; i < 8; i++) {
    PORT = (1 << i);
    PORT = 0;
    _delay_ms(100);
}
AThe PORT is cleared immediately after setting, so LEDs turn off too fast to see.
BThe bit shift operator is used incorrectly; it should be << instead of >>.
CThe loop counter i is not initialized properly.
DThe delay function is missing, so LEDs never light.
Attempts:
2 left
💡 Hint

Look at the order of setting and clearing PORT and the delay timing.

📝 Syntax
advanced
2:00remaining
Which option contains a syntax error in this LED pattern code?

Find the option that will cause a syntax error when compiling this LED control snippet.

Embedded C
for (int i = 0; i < 8; i++) {
    PORT = (1 << i);
    _delay_ms(100);
}
Afor (int i = 0; i < 8; i++) { PORT = (1 << i); _delay_ms(100); }
Bfor (int i = 0; i < 8 i++) { PORT = (1 << i); _delay_ms(100); }
C} ;)001(sm_yaled_ ;)i << 1( = TROP { )++i ;8 < i ;0 = i tni( rof
Dor (int i = 0; i < 8; i++) { PORT = (1 << i); _delay_ms(100); }
Attempts:
2 left
💡 Hint

Check the for loop syntax carefully.

🚀 Application
expert
3:00remaining
How many unique LED patterns are generated by this code?

This code cycles through LED patterns on an 8-LED array. How many unique patterns does it produce?

Embedded C
for (int i = 0; i < 8; i++) {
    PORT = (0xFF >> i) << i;
    _delay_ms(100);
}
A8 unique patterns
B7 unique patterns
C9 unique patterns
D16 unique patterns
Attempts:
2 left
💡 Hint

Analyze how the bit shifts affect the LED pattern each iteration.