0
0
Arduinoprogramming~30 mins

State machine design for Arduino - Mini Project: Build & Apply

Choose your learning style9 modes available
State machine design for Arduino
📖 Scenario: You are building a simple traffic light controller using an Arduino. The traffic light has three states: Green, Yellow, and Red. Each state lasts for a specific time before switching to the next state in a cycle.This project will teach you how to design a state machine in Arduino code to control the traffic light.
🎯 Goal: Create an Arduino program that uses a state machine to cycle through the traffic light states: Green, Yellow, and Red. Each state should last a set amount of time, and the program should print the current state to the Serial Monitor.
📋 What You'll Learn
Create a variable to hold the current state of the traffic light
Create constants for the duration of each state in milliseconds
Use a state machine with a switch-case to change states based on elapsed time
Print the current state to the Serial Monitor
💡 Why This Matters
🌍 Real World
State machines are used in embedded systems like traffic lights, vending machines, and home appliances to control behavior step-by-step.
💼 Career
Understanding state machines is essential for embedded programming jobs, robotics, and IoT device development.
Progress0 / 4 steps
1
Set up the initial state and durations
Create an int variable called currentState and set it to 0 to represent the Green state. Also, create three const unsigned long variables called greenDuration, yellowDuration, and redDuration with values 5000, 2000, and 5000 respectively to represent the duration of each state in milliseconds.
Arduino
Need a hint?

Use int currentState = 0; to start with Green. Use const unsigned long for durations in milliseconds.

2
Add a variable to track the last state change time
Add a unsigned long variable called lastChangeTime and set it to 0. This will store the time when the state last changed.
Arduino
Need a hint?

Use unsigned long lastChangeTime = 0; to track when the state last changed.

3
Implement the state machine logic in loop()
Inside the loop() function, get the current time using millis() and check if the elapsed time since lastChangeTime is greater than the duration of the current state. Use a switch statement on currentState with cases 0, 1, and 2 for Green, Yellow, and Red respectively. When the time is up, update currentState to the next state (Green -> Yellow -> Red -> Green) and update lastChangeTime to the current time.
Arduino
Need a hint?

Use switch (currentState) and check elapsed time with millis(). Change currentState and update lastChangeTime when duration ends.

4
Print the current state to the Serial Monitor
Inside the loop() function, after the switch statement, add a switch on currentState to print "Green Light", "Yellow Light", or "Red Light" to the Serial Monitor using Serial.println().
Arduino
Need a hint?

Use a switch (currentState) and Serial.println() to print the current state.