Washing machine (hierarchical state machine)
Nest Fill, Wash and Spin inside a Running state, so one rule pauses all three, and resume where it left off.
Firmware · Advanced
In a real product: Washing machine (Consumer electronics): Nested states pause the whole cycle with one rule.
A washing machine runs a cycle of fill, wash and spin, but some rules hold in every step of it: opening the door pauses, Pause pauses, and resuming continues the step that was interrupted. Written as a flat state machine, those rules are copied into every running state. A hierarchical state machine writes them once, on the parent.
What you'll learn
- A superstate (Running) whose transitions apply to every substate (Fill, Wash, Spin)
- Entry actions that run when a state is entered
- History: Paused remembers which substate to go back to
- Timing each step with
millis(), without blocking
How it works
The machine has three top-level states, Idle, Running and Paused, and Running has three substates, each with its own duration (3 s, 4 s and 3 s here, to keep the demo short). handle() first gives an event to the current substate; anything the substate does not use goes up to Running, which handles the door and the Pause button for all three.
Entering Paused saves the substate (its history) and how long it had left. Resuming re-enters that substate with the time it had left, not from the start of the cycle. Each substate's LED (pins 7, 8 and 9) shows where the cycle is, and pin 10's LED blinks while paused.
Try this
- Press Start, then Pause during Wash, then Start again: it finishes Wash, then spins.
- Open the door (the switch) during Spin: it pauses, and closing it alone does not restart it.
- Add a Rinse substate between Wash and Spin. Notice that the door and Pause rules need no change.
sketch.ino
// A washing machine as a hierarchical state machine: Running holds Fill, Wash and Spin. Start/Resume (2), Pause (3),
// door switch (4). LEDs: fill 7, wash 8, spin 9, paused 10.
enum Top { IDLE, RUNNING, PAUSED };
enum Step { FILL, WASH, SPIN };
const char *const STEP_NAMES[] = { "fill", "wash", "spin" };
const unsigned long STEP_MS[] = { 3000, 4000, 3000 };
const int START = 2, PAUSE = 3, DOOR = 4, LEDS[] = { 7, 8, 9 }, PAUSED_LED = 10;
Top top = IDLE;
Step step = FILL; // the current substate of Running, and its history while paused
unsigned long stepEnds = 0; // when the current step finishes
unsigned long remaining = 0; // a paused step's time left
enum Event { EV_START, EV_PAUSE, EV_DOOR_OPEN, EV_TIMEOUT };
void enterStep(Step s, unsigned long ms) {
step = s;
stepEnds = millis() + ms;
Serial.print("Running: ");
Serial.println(STEP_NAMES[s]);
}
// Running's own rules: they hold whatever the substate.
bool running(Event e) {
if (e == EV_PAUSE || e == EV_DOOR_OPEN) {
remaining = stepEnds - millis();
top = PAUSED;
Serial.print("Paused in ");
Serial.println(STEP_NAMES[step]);
return true;
}
return false;
}
// The substates: each only knows what comes next.
bool substate(Event e) {
if (e != EV_TIMEOUT) return false;
if (step == FILL) enterStep(WASH, STEP_MS[WASH]);
else if (step == WASH) enterStep(SPIN, STEP_MS[SPIN]);
else { top = IDLE; Serial.println("Cycle done"); }
return true;
}
void handle(Event e, bool doorOpen) {
switch (top) {
case IDLE:
if (e == EV_START && !doorOpen) { top = RUNNING; enterStep(FILL, STEP_MS[FILL]); }
break;
case RUNNING:
if (!substate(e)) running(e); // unhandled events go up to the parent
break;
case PAUSED:
if (e == EV_START && !doorOpen) { top = RUNNING; enterStep(step, remaining); } // history
break;
}
}
bool lastStart = HIGH, lastPause = HIGH, lastDoor = LOW;
void setup() {
Serial.begin(9600);
pinMode(START, INPUT_PULLUP);
pinMode(PAUSE, INPUT_PULLUP);
pinMode(DOOR, INPUT);
for (int pin : LEDS) pinMode(pin, OUTPUT);
pinMode(PAUSED_LED, OUTPUT);
lastDoor = digitalRead(DOOR);
Serial.println("Ready: close the door and press Start");
}
void loop() {
bool s = digitalRead(START), p = digitalRead(PAUSE), door = digitalRead(DOOR);
if (lastStart && !s) handle(EV_START, door);
if (lastPause && !p) handle(EV_PAUSE, door);
if (door && !lastDoor) handle(EV_DOOR_OPEN, door);
lastStart = s; lastPause = p; lastDoor = door;
if (top == RUNNING && (long)(millis() - stepEnds) >= 0) handle(EV_TIMEOUT, door);
for (int i = 0; i < 3; i++) digitalWrite(LEDS[i], top != IDLE && step == i);
digitalWrite(PAUSED_LED, top == PAUSED && (millis() / 300) % 2);
}