0
0
Arduinoprogramming~10 mins

State machine design for Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - State machine design for Arduino
Start
Initialize State
Read Inputs
Check Current State
If State A
Stay in State A
If State B
Stay in State B
Perform Actions
Loop Back to Read Inputs
The Arduino program starts by setting an initial state, then repeatedly reads inputs, checks the current state, decides if it should change state based on conditions, performs actions for that state, and loops back.
Execution Sample
Arduino
enum State {IDLE, LED_ON};
State currentState = IDLE;

void setup() {
  pinMode(2, INPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  if (currentState == IDLE && digitalRead(2) == HIGH) {
    currentState = LED_ON;
  } else if (currentState == LED_ON && digitalRead(2) == LOW) {
    currentState = IDLE;
  }
  digitalWrite(13, currentState == LED_ON ? HIGH : LOW);
}
This code switches an LED on pin 13 on or off based on a button on pin 2 using a simple two-state machine.
Execution Table
StepcurrentStatedigitalRead(2)Condition CheckedState ChangeLED Output
1IDLELOWIDLE && HIGH? FalseNo changeLOW
2IDLEHIGHIDLE && HIGH? TrueChange to LED_ONHIGH
3LED_ONHIGHLED_ON && LOW? FalseNo changeHIGH
4LED_ONLOWLED_ON && LOW? TrueChange to IDLELOW
5IDLELOWIDLE && HIGH? FalseNo changeLOW
💡 Loop runs continuously; no exit in Arduino loop function.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
currentStateIDLEIDLELED_ONLED_ONIDLEIDLE
digitalRead(2)N/ALOWHIGHHIGHLOWLOW
LED OutputLOWLOWHIGHHIGHLOWLOW
Key Moments - 3 Insights
Why does the LED turn on only when the button is pressed?
Because in the execution_table at Step 2, when digitalRead(2) is HIGH and currentState is IDLE, the state changes to LED_ON, which sets the LED output HIGH.
What happens if the button stays pressed for multiple loops?
As shown in Step 3, if the button remains HIGH and the state is LED_ON, no state change occurs, so the LED stays on.
Why is there no exit from the loop?
Arduino's loop() function runs forever, so the execution_table notes that the loop continues indefinitely, cycling through states based on input.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2. What is the currentState after this step?
ALED_ON
BIDLE
CHIGH
DLOW
💡 Hint
Check the 'State Change' column at Step 2 in the execution_table.
At which step does the LED turn off according to the execution_table?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look at the 'LED Output' column and find when it changes to LOW.
If digitalRead(2) is always LOW, what will be the currentState after Step 5?
ALED_ON
BIDLE
CHIGH
DUndefined
💡 Hint
Refer to the variable_tracker for currentState values when digitalRead(2) is LOW.
Concept Snapshot
State machine design in Arduino:
- Define states using enum or constants.
- Use a variable to track current state.
- In loop(), read inputs and check conditions.
- Change state based on conditions.
- Perform actions based on current state.
- Loop runs forever, updating state and outputs.
Full Transcript
This example shows how to design a simple state machine on Arduino. We start with an initial state called IDLE. The program reads a button input on pin 2. If the button is pressed (input HIGH) while in IDLE, the state changes to LED_ON, turning on an LED on pin 13. If the button is released (input LOW) while in LED_ON, the state changes back to IDLE, turning the LED off. The loop repeats forever, checking inputs and updating the state and LED accordingly. The execution table traces each step, showing the state, input, condition checks, state changes, and LED output. The variable tracker shows how currentState and LED output change over time. This design helps organize code clearly and makes it easy to add more states or inputs later.