0
0
Embedded Cprogramming~10 mins

Simple state machine with switch-case in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Simple state machine with switch-case
Start
Current State
switch-case on State
State A
Action
Update State
Loop back to Current State
The program checks the current state, runs code for that state, updates the state, and repeats.
Execution Sample
Embedded C
enum State {STATE_A, STATE_B, STATE_C};
enum State current = STATE_A;

switch(current) {
  case STATE_A: current = STATE_B; break;
  case STATE_B: current = STATE_C; break;
  case STATE_C: current = STATE_A; break;
}
This code cycles through three states A, B, and C in order.
Execution Table
StepCurrent Stateswitch-case branchActionNext State
1STATE_Acase STATE_ASet current = STATE_BSTATE_B
2STATE_Bcase STATE_BSet current = STATE_CSTATE_C
3STATE_Ccase STATE_CSet current = STATE_ASTATE_A
4STATE_Acase STATE_ASet current = STATE_BSTATE_B
💡 The state machine loops indefinitely cycling through states A, B, and C.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
currentSTATE_ASTATE_BSTATE_CSTATE_ASTATE_B
Key Moments - 2 Insights
Why does the state change after each switch-case?
Because each case sets 'current' to the next state before breaking, as shown in the execution_table rows 1-3.
What happens if there is no 'break' after a case?
Without 'break', execution would continue to the next case, causing unexpected state changes. The execution_table shows one case per step due to breaks.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'current' after Step 2?
ASTATE_C
BSTATE_B
CSTATE_A
DUndefined
💡 Hint
Check the 'Next State' column for Step 2 in the execution_table.
At which step does the state machine return to STATE_A?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Next State' column in the execution_table to find when 'current' becomes STATE_A.
If the 'break' statements were removed, what would happen in the execution_table?
AEach step would only execute one case as before.
BThe state would update multiple times per step, skipping states.
CThe state would never change.
DThe program would crash.
💡 Hint
Recall that without 'break', switch-case falls through multiple cases in C.
Concept Snapshot
Simple state machine uses a variable to track state.
Use switch-case to run code based on current state.
Each case updates the state variable.
Use break to prevent fall-through.
Loop to cycle through states repeatedly.
Full Transcript
This example shows a simple state machine in embedded C using switch-case. The variable 'current' holds the current state. The switch-case checks 'current' and runs code for that state. Each case sets 'current' to the next state and breaks to stop fall-through. The execution table shows step-by-step how 'current' changes from STATE_A to STATE_B, then STATE_C, then back to STATE_A, cycling indefinitely. Key points include the importance of break statements to avoid running multiple cases at once. The visual quiz tests understanding of state changes and switch-case behavior.