0
0
Arduinoprogramming~10 mins

State machine design for Arduino - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the initial state of the state machine.

Arduino
enum State { IDLE, RUNNING, ERROR };
State currentState = [1];
Drag options to blanks, or click blank then click option'
ARUNNING
BIDLE
CERROR
DSTART
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a state that is not defined in the enum.
Using a string instead of the enum value.
2fill in blank
medium

Complete the code to check if the current state is RUNNING.

Arduino
if (currentState == [1]) {
  // do something
}
Drag options to blanks, or click blank then click option'
ARUNNING
BIDLE
CERROR
DSTART
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string like "RUNNING" instead of the enum value.
Comparing to a state not defined in the enum.
3fill in blank
hard

Fix the error in the switch statement to handle the ERROR state.

Arduino
switch (currentState) {
  case IDLE:
    // do idle tasks
    break;
  case RUNNING:
    // do running tasks
    break;
  case [1]:
    // handle error
    break;
}
Drag options to blanks, or click blank then click option'
APAUSE
BSTOP
CSTART
DERROR
Attempts:
3 left
💡 Hint
Common Mistakes
Using a state name not defined in the enum.
Using a string instead of the enum value.
4fill in blank
hard

Fill both blanks to update the state to RUNNING when a button is pressed.

Arduino
if (digitalRead(buttonPin) == [1]) {
  currentState = [2];
}
Drag options to blanks, or click blank then click option'
AHIGH
BLOW
CRUNNING
DIDLE
Attempts:
3 left
💡 Hint
Common Mistakes
Using LOW instead of HIGH for button press.
Setting state to IDLE instead of RUNNING.
5fill in blank
hard

Fill all three blanks to create a state machine loop that handles IDLE and RUNNING states.

Arduino
void loop() {
  switch (currentState) {
    case [1]:
      // wait for start
      if (digitalRead(startPin) == HIGH) {
        currentState = [2];
      }
      break;
    case [3]:
      // running tasks
      // ...
      break;
  }
}
Drag options to blanks, or click blank then click option'
AIDLE
BRUNNING
CERROR
DSTART
Attempts:
3 left
💡 Hint
Common Mistakes
Using ERROR state instead of RUNNING for running tasks.
Mixing up the order of states.