0
0
Embedded Cprogramming~10 mins

LED control patterns in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - LED control patterns
Start
Initialize LEDs
Set pattern index = 0
Loop Start
Turn ON LEDs according to pattern
Delay for visibility
Turn OFF LEDs
Increment pattern index
Check if pattern index < total patterns?
NoEnd
Back to Loop Start
The program initializes LEDs, then loops through different LED patterns turning them on and off with delays, cycling through all patterns before stopping.
Execution Sample
Embedded C
int patterns[] = {0x01, 0x02, 0x04};
int i = 0;
while(i < 3) {
  LED_PORT = patterns[i];
  delay();
  LED_PORT = 0;
  delay();
  i++;
}
This code cycles through three LED patterns turning each on and off with a delay.
Execution Table
StepiLED_PORT value (hex)ActionOutput
100x01Turn ON first LED patternLED1 ON
200x00Turn OFF LEDsAll LEDs OFF
310x02Turn ON second LED patternLED2 ON
410x00Turn OFF LEDsAll LEDs OFF
520x04Turn ON third LED patternLED3 ON
620x00Turn OFF LEDsAll LEDs OFF
73-Check loop condition i<3Condition false, exit loop
💡 i reaches 3, condition i<3 is false, loop ends
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
i01233
LED_PORT0x000x000x000x000x00
Key Moments - 3 Insights
Why does LED_PORT get set to 0 after each pattern?
Setting LED_PORT to 0 turns off all LEDs before the next pattern, as shown in steps 2, 4, and 6 in the execution_table.
What happens when i equals 3 in the loop?
At step 7, the condition i<3 is false, so the loop stops running, ending the LED pattern sequence.
Why do we increment i after turning LEDs off?
Incrementing i after turning LEDs off ensures the next pattern is selected in the next loop cycle, as seen between steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of LED_PORT at step 3?
A0x00
B0x01
C0x02
D0x04
💡 Hint
Check the LED_PORT value column at step 3 in the execution_table.
At which step does the loop condition become false?
AStep 7
BStep 6
CStep 3
DStep 1
💡 Hint
Look at the last row in the execution_table where the condition is checked.
If we add a fourth pattern 0x08, how many times will the loop run?
A3 times
B4 times
C5 times
D2 times
💡 Hint
Refer to variable_tracker for i increments and loop condition in execution_table.
Concept Snapshot
LED control patterns use arrays to store LED states.
Loop through each pattern, turn LEDs ON, delay, then OFF.
Increment index to move to next pattern.
Stop when all patterns are shown.
Use hex values to represent LEDs ON/OFF.
Full Transcript
This lesson shows how to control LEDs in patterns using embedded C. The program starts by initializing an array of LED patterns. It uses a loop to turn LEDs on according to each pattern, waits for a delay, then turns LEDs off. The index variable i tracks which pattern is active. After each pattern, i increments. When i reaches the number of patterns, the loop ends. The execution table traces each step, showing LED_PORT values and actions. Key moments clarify why LEDs turn off between patterns and when the loop stops. The quiz tests understanding of LED_PORT values, loop exit, and adding patterns. The snapshot summarizes the pattern control process simply.