0
0
Arduinoprogramming~20 mins

State machine design for Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Arduino State Machine Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple Arduino state machine

Consider this Arduino code snippet implementing a simple state machine with three states. What will be printed to the Serial Monitor after 5 iterations of loop()?

Arduino
enum State {IDLE, RUNNING, STOPPED};
State currentState = IDLE;

void setup() {
  Serial.begin(9600);
}

void loop() {
  switch(currentState) {
    case IDLE:
      Serial.println("Idle state");
      currentState = RUNNING;
      break;
    case RUNNING:
      Serial.println("Running state");
      currentState = STOPPED;
      break;
    case STOPPED:
      Serial.println("Stopped state");
      currentState = IDLE;
      break;
  }
  delay(10); // simulate some delay
}
AStopped state\nIdle state\nRunning state\nStopped state\nIdle state
BIdle state\nIdle state\nIdle state\nIdle state\nIdle state
CRunning state\nStopped state\nIdle state\nRunning state\nStopped state
DIdle state\nRunning state\nStopped state\nIdle state\nRunning state
Attempts:
2 left
💡 Hint

Look at how the currentState changes after each loop() call.

🧠 Conceptual
intermediate
1:30remaining
Understanding state transitions in Arduino

In an Arduino state machine, what is the main purpose of using a switch statement inside the loop() function?

ATo select and execute code based on the current state of the machine
BTo pause the program until a button is pressed
CTo initialize hardware components only once
DTo declare global variables for the program
Attempts:
2 left
💡 Hint

Think about how the program decides what to do next depending on the state.

🔧 Debug
advanced
2:00remaining
Identify the error in this Arduino state machine code

What error will this Arduino code produce when compiled?

Arduino
enum State {START, PROCESS, END};
State currentState = START;

void setup() {
  Serial.begin(9600);
}

void loop() {
  switch(currentState) {
    case START:
      Serial.println("Start");
      currentState = PROCESS;
      break;
    case PROCESS:
      Serial.println("Process");
      currentState = END;
      break;
    case END:
      Serial.println("End");
      currentState = START;
      break;
  }
  delay(1000);
}
ASyntaxError: missing semicolon after break in case START
BTypeError: cannot assign enum to variable
CRuntimeError: infinite loop without delay
DNo error, code compiles and runs correctly
Attempts:
2 left
💡 Hint

Check the syntax carefully after each break statement.

🚀 Application
advanced
1:30remaining
Number of states in a traffic light Arduino state machine

You design a traffic light controller with states: RED, RED_YELLOW, GREEN, and YELLOW. How many states should the enum have to represent this system?

A2
B3
C4
D5
Attempts:
2 left
💡 Hint

Count each unique light combination as one state.

Predict Output
expert
2:30remaining
Output of nested state machine with timing

What will be the output on the Serial Monitor after running this Arduino code for 3 iterations of loop()?

Arduino
enum State {WAIT, ACTION};
State currentState = WAIT;
int counter = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  switch(currentState) {
    case WAIT:
      Serial.print("Waiting ");
      Serial.println(counter);
      if (counter >= 2) {
        currentState = ACTION;
      } else {
        counter++;
      }
      break;
    case ACTION:
      Serial.println("Action executed");
      currentState = WAIT;
      counter = 0;
      break;
  }
  delay(10);
}
AWaiting 0\nWaiting 1\nWaiting 2\nAction executed
BWaiting 0\nWaiting 1\nWaiting 2
CWaiting 0\nWaiting 1\nAction executed
DAction executed\nWaiting 0\nWaiting 1
Attempts:
2 left
💡 Hint

Trace the value of counter and state changes carefully.