Bird
Raised Fist0
Embedded Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using LED control patterns in embedded C programming?
easy
A. To create visual effects by turning LEDs on and off in specific sequences
B. To increase the processing speed of the microcontroller
C. To store data in the microcontroller's memory
D. To connect the microcontroller to the internet

Solution

  1. Step 1: Understand LED control patterns

    LED control patterns are used to create visual effects by controlling LEDs.
  2. Step 2: Identify the purpose in embedded systems

    They help show device status or create decorative light sequences by turning LEDs on and off.
  3. Final Answer:

    To create visual effects by turning LEDs on and off in specific sequences -> Option A
  4. Quick Check:

    LED patterns = visual effects [OK]
Hint: LED patterns control lights, not speed or memory [OK]
Common Mistakes:
  • Confusing LED patterns with data storage
  • Thinking LED patterns improve processing speed
  • Assuming LED patterns connect to the internet
2. Which of the following is the correct syntax to turn on an LED connected to pin 5 using embedded C?
easy
A. digitalWrite(HIGH, 5);
B. digitalWrite(5, ON);
C. digitalWrite(5, HIGH);
D. digitalWrite(ON, 5);

Solution

  1. Step 1: Recall digitalWrite syntax

    The function digitalWrite(pin, value) sets the pin to HIGH or LOW.
  2. Step 2: Match correct parameters

    Pin number comes first, then the value HIGH to turn LED on.
  3. Final Answer:

    digitalWrite(5, HIGH); -> Option C
  4. Quick Check:

    digitalWrite(pin, HIGH) = turn LED on [OK]
Hint: Pin number first, then HIGH or LOW [OK]
Common Mistakes:
  • Swapping pin and value order
  • Using ON instead of HIGH
  • Using lowercase for HIGH
3. What will be the output on the LED connected to pin 3 after running this code snippet?
for(int i = 0; i < 3; i++) {
  digitalWrite(3, HIGH);
  delay(500);
  digitalWrite(3, LOW);
  delay(500);
}
medium
A. LED stays OFF continuously
B. LED stays ON continuously for 1.5 seconds
C. LED blinks once and stays OFF
D. LED blinks 3 times with 0.5 second ON and 0.5 second OFF

Solution

  1. Step 1: Analyze the loop iterations

    The loop runs 3 times, each time turning LED ON then OFF with delays.
  2. Step 2: Understand delay timing

    Each ON and OFF lasts 500ms, so LED blinks 3 times with equal ON/OFF intervals.
  3. Final Answer:

    LED blinks 3 times with 0.5 second ON and 0.5 second OFF -> Option D
  4. Quick Check:

    Loop 3 times with delay = 3 blinks [OK]
Hint: Count loop runs and delays for blink count [OK]
Common Mistakes:
  • Ignoring the loop count
  • Confusing delay units
  • Assuming LED stays ON
4. Identify the error in this LED blinking code:
void loop() {
  digitalWrite(2, HIGH);
  delay(1000);
  digitalWrite(2, HIGH);
  delay(1000);
}
medium
A. delay() function is used incorrectly
B. LED never turns OFF because digitalWrite(2, HIGH) is used twice
C. Pin 2 is not set as OUTPUT
D. digitalWrite() syntax is wrong

Solution

  1. Step 1: Check LED ON/OFF commands

    Both digitalWrite calls set pin 2 to HIGH, so LED stays ON.
  2. Step 2: Identify missing OFF command

    LED never turns OFF because digitalWrite(2, LOW) is missing.
  3. Final Answer:

    LED never turns OFF because digitalWrite(2, HIGH) is used twice -> Option B
  4. Quick Check:

    Same HIGH twice = no OFF [OK]
Hint: LED needs HIGH then LOW to blink [OK]
Common Mistakes:
  • Assuming delay is wrong
  • Forgetting to set pin mode
  • Thinking syntax is incorrect
5. You want to create a running LED pattern on 4 LEDs connected to pins 2, 3, 4, and 5. Which code snippet correctly implements this pattern?
hard
A. for(int i = 2; i <= 5; i++) { digitalWrite(i, HIGH); delay(200); digitalWrite(i, LOW); }
B. for(int i = 2; i <= 5; i++) { digitalWrite(i, LOW); delay(200); digitalWrite(i, HIGH); }
C. digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, HIGH); digitalWrite(5, HIGH); delay(200); digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW);
D. for(int i = 5; i >= 2; i--) { digitalWrite(i, HIGH); delay(200); }

Solution

  1. Step 1: Understand running LED pattern

    Running LED means turning ON one LED at a time in sequence, then OFF before next.
  2. Step 2: Analyze each option

    for(int i = 2; i <= 5; i++) { digitalWrite(i, HIGH); delay(200); digitalWrite(i, LOW); } loops from pin 2 to 5, turns each LED ON, delays, then OFF, creating running effect.
  3. Final Answer:

    for(int i = 2; i <= 5; i++) { digitalWrite(i, HIGH); delay(200); digitalWrite(i, LOW); } -> Option A
  4. Quick Check:

    Loop ON then OFF per LED = running pattern [OK]
Hint: Turn ON then OFF each LED in order [OK]
Common Mistakes:
  • Turning all LEDs ON at once
  • Not turning off the LED after delay
  • Turning LEDs OFF before ON
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard