Complete the code to define the initial state of the state machine.
enum State { IDLE, RUNNING, ERROR };
State currentState = [1];The initial state is usually set to IDLE to indicate the system is waiting to start.
Complete the code to check if the current state is RUNNING.
if (currentState == [1]) { // do something }
To check if the state machine is in the RUNNING state, compare currentState to RUNNING.
Fix the error in the switch statement to handle the ERROR state.
switch (currentState) {
case IDLE:
// do idle tasks
break;
case RUNNING:
// do running tasks
break;
case [1]:
// handle error
break;
}The switch statement must use the enum value ERROR to handle the error state correctly.
Fill both blanks to update the state to RUNNING when a button is pressed.
if (digitalRead(buttonPin) == [1]) { currentState = [2]; }
When the button is pressed (reads HIGH), the state changes to RUNNING.
Fill all three blanks to create a state machine loop that handles IDLE and RUNNING states.
void loop() {
switch (currentState) {
case [1]:
// wait for start
if (digitalRead(startPin) == HIGH) {
currentState = [2];
}
break;
case [3]:
// running tasks
// ...
break;
}
}The loop waits in IDLE state, switches to RUNNING when start is pressed, and handles running tasks in RUNNING state.