Consider an 8-LED array controlled by this code snippet. What pattern will the LEDs show after running the loop?
for (int i = 0; i < 8; i++) { PORT = (1 << i); _delay_ms(100); }
Look at the bit shift operation and how it affects the PORT value.
The code shifts a single bit from the least significant bit to the most significant bit, lighting one LED at a time from left to right.
This code runs a pattern on an 8-LED array. What is the final value of PORT after the loop finishes?
PORT = 0; for (int i = 0; i < 8; i++) { PORT |= (1 << i); _delay_ms(50); }
Think about how the OR operation accumulates bits.
The OR operation adds each bit one by one, so after 8 iterations all bits are set, making PORT 255.
Identify the error in this LED blinking code that causes no LEDs to light up.
for (int i = 0; i < 8; i++) { PORT = (1 << i); PORT = 0; _delay_ms(100); }
Look at the order of setting and clearing PORT and the delay timing.
PORT is set then immediately cleared before the delay, so LEDs turn off instantly and appear not to light.
Find the option that will cause a syntax error when compiling this LED control snippet.
for (int i = 0; i < 8; i++) { PORT = (1 << i); _delay_ms(100); }
Check the for loop syntax carefully.
Option B misses a semicolon between the loop conditions, causing a syntax error.
This code cycles through LED patterns on an 8-LED array. How many unique patterns does it produce?
for (int i = 0; i < 8; i++) { PORT = (0xFF >> i) << i; _delay_ms(100); }
Analyze how the bit shifts affect the LED pattern each iteration.
The loop runs 8 times, each time shifting the pattern, producing 8 unique LED patterns.