A state machine helps a program remember what it is doing and decide what to do next. Using switch-case makes it easy to check the current state and act accordingly.
Simple state machine with switch-case in Embedded C
switch (state) {
case STATE1:
// actions for STATE1
break;
case STATE2:
// actions for STATE2
break;
// add more states as needed
default:
// default action
break;
}Each case matches a state value.
Use break; to stop checking other cases after one matches.
switch (state) {
case IDLE:
// wait for event
break;
case RUNNING:
// perform task
break;
}switch (state) {
case RED:
// turn on red light
state = GREEN;
break;
case GREEN:
// turn on green light
state = YELLOW;
break;
case YELLOW:
// turn on yellow light
state = RED;
break;
}This program cycles through three states: OFF, ON, and BLINK. It prints the current state and then moves to the next one. It repeats this 5 times.
#include <stdio.h> // Define states #define STATE_OFF 0 #define STATE_ON 1 #define STATE_BLINK 2 int main() { int state = STATE_OFF; int count = 0; while (count < 5) { switch (state) { case STATE_OFF: printf("State: OFF\n"); state = STATE_ON; break; case STATE_ON: printf("State: ON\n"); state = STATE_BLINK; break; case STATE_BLINK: printf("State: BLINK\n"); state = STATE_OFF; break; default: printf("Unknown state\n"); state = STATE_OFF; break; } count++; } return 0; }
Always include a default case to catch unexpected states.
Remember to update the state inside each case to move the machine forward.
A state machine keeps track of what the program is doing.
Use switch-case to handle different states clearly.
Change the state inside cases to move between states.
Practice
What is the main purpose of using a switch-case statement in a simple state machine?
Solution
Step 1: Understand the role of switch-case in state machines
The switch-case statement lets the program choose what to do based on the current state value.Step 2: Recognize how states are handled
Each case corresponds to a state, so the program runs code specific to that state.Final Answer:
To handle different states clearly and execute code based on the current state -> Option BQuick Check:
switch-case manages states = To handle different states clearly and execute code based on the current state [OK]
- Thinking switch-case creates loops
- Confusing variable declaration with state handling
- Assuming switch-case stops the program
Which of the following is the correct syntax for a switch statement in embedded C?
switch(state) {
case 0:
// code
break;
case 1:
// code
break;
default:
// code
}Solution
Step 1: Check the syntax structure of switch-case
The switch statement requires parentheses around the variable and braces around the cases.Step 2: Verify the use of colons and breaks
Each case must have a colon and usually ends with a break to prevent fall-through.Final Answer:
switch(state) { case 0: ... break; case 1: ... break; default: ... } -> Option DQuick Check:
Correct switch-case syntax = switch(state) { case 0: ... break; case 1: ... break; default: ... } [OK]
- Omitting parentheses after switch
- Missing colons after case labels
- Forgetting break statements
What will be the output of this code snippet?
int state = 1;
switch(state) {
case 0:
printf("State is zero\n");
break;
case 1:
printf("State is one\n");
break;
default:
printf("Unknown state\n");
}Solution
Step 1: Identify the value of state
The variable state is set to 1 before the switch statement.Step 2: Match the case with state value
The switch matches case 1, so it prints "State is one" and then breaks.Final Answer:
State is one -> Option CQuick Check:
state=1 prints "State is one" [OK]
- Confusing case 0 with case 1
- Forgetting break causes fall-through
- Assuming default runs when case matches
Find the error in this state machine code snippet:
int state = 0;
switch(state) {
case 0:
printf("Start\n");
case 1:
printf("Running\n");
break;
default:
printf("Unknown\n");
}Solution
Step 1: Check each case for break statements
Case 0 does not have a break, so after printing "Start", it continues to case 1.Step 2: Understand fall-through effect
This causes both "Start" and "Running" to print when state is 0, which may be unintended.Final Answer:
Missing break after case 0 causes fall-through -> Option AQuick Check:
Missing break causes fall-through = Missing break after case 0 causes fall-through [OK]
- Ignoring missing break leads to unexpected output
- Thinking default must be first case
- Assuming uninitialized variable error
You want to design a simple state machine with three states: INIT (0), PROCESS (1), and DONE (2). The machine should start at INIT, then move to PROCESS, and finally to DONE. Which code snippet correctly updates the state inside the switch-case?
int state = 0;
switch(state) {
case 0:
// Move to PROCESS
state = 1;
break;
case 1:
// Move to DONE
state = 2;
break;
case 2:
// Finished
break;
}Solution
Step 1: Check state transitions in each case
Case 0 sets state to 1 (PROCESS), case 1 sets state to 2 (DONE), case 2 does nothing (finished).Step 2: Confirm the order of states
The code moves from INIT to PROCESS to DONE in order, updating the state variable correctly.Final Answer:
Correctly updates state from INIT to PROCESS to DONE -> Option AQuick Check:
State updates follow sequence = Correctly updates state from INIT to PROCESS to DONE [OK]
- Not updating state variable inside cases
- Skipping intermediate states
- Forgetting break causing unexpected behavior
Ready to prove it?
Pass the test and this pattern moves unknown
