📖 Scenario: Imagine you are building a simple traffic light controller for a small intersection. The traffic light changes from green to yellow to red and then back to green in a loop. To manage these changes clearly and safely, you use a state machine.
🎯 Goal: You will create a simple state machine in C that controls the traffic light states. This will help you understand why state machines are useful in embedded systems for managing different modes or states clearly and reliably.
📋 What You'll Learn
Create an enum to represent the traffic light states
Create a variable to hold the current state
Write a function to move to the next state
Print the current state to show the state machine working
💡 Why This Matters
🌍 Real World
State machines are used in embedded systems like traffic lights, washing machines, and elevators to control different modes and actions in a clear, predictable way.
💼 Career
Understanding state machines is important for embedded developers to write reliable and maintainable code that controls hardware behavior step-by-step.
Progress0 / 4 steps
1
Create the traffic light states
Create an enum called TrafficLightState with these exact states: RED, GREEN, and YELLOW.
Embedded C
Hint
An enum lets you name states with words instead of numbers. This makes your code easier to read.
2
Create a variable for the current state
Create a variable called currentState of type TrafficLightState and set it to RED.
Embedded C
Hint
This variable will remember which light is currently on.
3
Write a function to change to the next state
Write a function called nextState that changes currentState from RED to GREEN, from GREEN to YELLOW, and from YELLOW back to RED.
Embedded C
Hint
The switch statement helps you decide what the next state should be based on the current state.
4
Print the current state
Write a main function that calls nextState() three times and prints the currentState after each call. Use printf to print "RED", "GREEN", or "YELLOW" depending on the state.
Embedded C
Hint
Use a loop to call nextState() and a switch to print the current state as text.
Practice
(1/5)
1. Why are state machines commonly used in embedded systems?
easy
A. To reduce the physical size of the embedded device
B. To increase the clock speed of the processor
C. To organize device behavior into clear, manageable steps
D. To store large amounts of data efficiently
Solution
Step 1: Understand the purpose of state machines
State machines break down complex device behavior into smaller, clear steps called states.
Step 2: Identify the benefit in embedded systems
This organization helps manage complexity and makes the system easier to design and debug.
Final Answer:
To organize device behavior into clear, manageable steps -> Option C
Quick Check:
State machines = clear steps [OK]
Hint: State machines simplify complex behavior into steps [OK]
Common Mistakes:
Confusing state machines with hardware speed improvements
Thinking state machines reduce device size
Assuming state machines store data
2. Which of the following is the correct way to represent a state machine in embedded C?
easy
A. Using a switch-case statement to handle different states
B. Using a for loop to iterate through states
C. Using a recursive function to call states
D. Using a global variable to store sensor data
Solution
Step 1: Identify common state machine structure
State machines in embedded C are often implemented with switch-case to handle each state clearly.
Step 2: Compare other options
For loops and recursion are not typical for state transitions; global variables store data but don't represent states.
Final Answer:
Using a switch-case statement to handle different states -> Option A
Quick Check:
Switch-case = state machine structure [OK]
Hint: Switch-case is classic for state handling [OK]
Common Mistakes:
Using loops instead of switch-case for states
Confusing recursion with state transitions
Mixing data storage with state control
3. Consider this embedded C code snippet implementing a simple state machine:
enum State {IDLE, RUNNING, STOPPED};
State currentState = IDLE;
switch(currentState) {
case IDLE:
currentState = RUNNING;
break;
case RUNNING:
currentState = STOPPED;
break;
case STOPPED:
currentState = IDLE;
break;
}
printf("%d", currentState);
What will be the output?
medium
A. 0
B. 1
C. 2
D. Compilation error
Solution
Step 1: Trace the initial state and switch
currentState starts at IDLE (0). The switch changes it to RUNNING (1).
Step 2: Determine the printed value
After the switch, currentState is RUNNING, so printf prints 1.
Final Answer:
1 -> Option B
Quick Check:
State IDLE (0) -> RUNNING (1) printed [OK]
Hint: Follow state changes step-by-step [OK]
Common Mistakes:
Assuming output is initial state value
Thinking code causes error due to enum
Confusing state values with strings
4. This embedded C code tries to implement a state machine but has a bug:
enum State {START, PROCESS, END};
State current = START;
switch(current) {
case START:
current = PROCESS;
case PROCESS:
current = END;
break;
case END:
current = START;
break;
}
What is the main problem?
medium
A. Missing break after case START causes fall-through
B. Enum values are not initialized
C. Variable current is not declared
D. Switch statement syntax is incorrect
Solution
Step 1: Check switch-case structure
Case START lacks a break, so execution falls through to PROCESS case.
Step 2: Understand effect of missing break
This causes current to skip PROCESS state and jump directly to END, breaking intended flow.
Final Answer:
Missing break after case START causes fall-through -> Option A
Quick Check:
Missing break = fall-through bug [OK]
Hint: Always add break after each case [OK]
Common Mistakes:
Ignoring missing break causes fall-through
Thinking enum needs explicit values
Assuming variable declaration error
5. You want to design an embedded system that controls a traffic light with states: RED, GREEN, YELLOW. Which reason best explains why using a state machine is the best approach?
hard
A. It increases the brightness of the traffic light LEDs
B. It reduces the power consumption of the traffic light hardware
C. It allows the traffic light to store vehicle counts efficiently
D. It clearly defines each light state and transitions, making timing and control simple
Solution
Step 1: Identify traffic light control needs
The system must cycle through RED, GREEN, YELLOW states in order with timing control.
Step 2: Understand state machine benefits
State machines organize these states and transitions clearly, simplifying timing and logic.
Final Answer:
It clearly defines each light state and transitions, making timing and control simple -> Option D
Quick Check:
State machine = clear control flow [OK]
Hint: State machines map clear steps and transitions [OK]
Common Mistakes:
Confusing state machines with hardware power savings
Thinking state machines store data counts
Assuming state machines affect LED brightness
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.