Try Embedded

Microwave controller (event-driven state machine)

Turn buttons, a door switch and a timer into events in a queue, and let one state machine decide what each means.

Firmware · Intermediate

In a real product: Microwave oven (Consumer electronics): Buttons, the door and a timer become events for one state machine.

A microwave has only a few inputs, but what each means depends on what it is doing: Start cooks when it is idle, adds 30 seconds while cooking, and resumes when paused. Opening the door pauses it; Stop pauses, then cancels. An event-driven state machine handles this without a tangle of flags.

What you'll learn

  • Separating noticing things (edges, ticks) from deciding what they mean
  • A ring buffer as an event queue, so no press is lost while the machine is busy
  • A transition table written as a switch on the state, then on the event

How it works

The input side only produces events. A button's falling edge is EV_START or EV_STOP, the door switch's change is EV_DOOR_OPEN or EV_DOOR_CLOSED, and once a second there is an EV_TICK. Each goes into a small ring buffer.

The machine takes one event at a time and looks it up by state:

StateStartStopDoor opensTick
Idlecook 30 s
Cooking+30 spausepausecount down; at 0, done
Pausedresume (door shut)cancel
Doneback to idleback to idle

The lamp (pin 7) is on while cooking or with the door open, the magnetron LED (pin 8) only while cooking, and the buzzer beeps when it is done.

Try this

  • Press Start twice quickly: both presses land in the queue, so it cooks for 60 s.
  • Open the door while cooking, close it, press Start: it resumes where it was.
  • Add a state for a child lock (hold Stop for 3 s): only the table grows.

sketch.ino

// A microwave: Start (2), Stop (3) and the door switch (4) become events in a queue, with a 1 s tick; one state
// machine handles them. Lamp LED 7, magnetron LED 8, buzzer 9.
enum Event : uint8_t { EV_START, EV_STOP, EV_DOOR_OPEN, EV_DOOR_CLOSED, EV_TICK };
enum State : uint8_t { IDLE, COOKING, PAUSED, DONE };
const char *const STATE_NAMES[] = { "idle", "cooking", "paused", "done" };

// ---- the event queue: a ring buffer of 8 ----
Event queue[8];
uint8_t head = 0, tail = 0;
bool push(Event e) {
  uint8_t next = (head + 1) % 8;
  if (next == tail) return false;  // full: drop it
  queue[head] = e;
  head = next;
  return true;
}
bool pop(Event &e) {
  if (tail == head) return false;
  e = queue[tail];
  tail = (tail + 1) % 8;
  return true;
}

// ---- noticing: edges and ticks become events ----
const int START = 2, STOP = 3, DOOR = 4, LAMP = 7, MAGNETRON = 8, BUZZER = 9;
bool lastStart = HIGH, lastStop = HIGH, lastDoor = LOW;
unsigned long lastTick = 0;

void sense() {
  bool s = digitalRead(START), p = digitalRead(STOP), d = digitalRead(DOOR);
  if (lastStart && !s) push(EV_START);
  if (lastStop && !p) push(EV_STOP);
  if (d != lastDoor) push(d ? EV_DOOR_OPEN : EV_DOOR_CLOSED);
  lastStart = s; lastStop = p; lastDoor = d;
  if (millis() - lastTick >= 1000) { lastTick += 1000; push(EV_TICK); }
}

// ---- deciding: the state machine ----
State state = IDLE;
int seconds = 0;
bool doorOpen = false;

void enter(State next) {
  state = next;
  Serial.print("-> ");
  Serial.print(STATE_NAMES[state]);
  Serial.print(", ");
  Serial.print(seconds);
  Serial.println(" s");
  if (state == DONE) tone(BUZZER, 2000, 400);
}

void handle(Event e) {
  if (e == EV_DOOR_OPEN) doorOpen = true;
  if (e == EV_DOOR_CLOSED) doorOpen = false;
  switch (state) {
    case IDLE:
      if (e == EV_START && !doorOpen) { seconds = 30; enter(COOKING); }
      break;
    case COOKING:
      if (e == EV_START) { seconds += 30; enter(COOKING); }
      else if (e == EV_STOP || e == EV_DOOR_OPEN) enter(PAUSED);
      else if (e == EV_TICK && --seconds == 0) enter(DONE);
      break;
    case PAUSED:
      if (e == EV_START && !doorOpen) enter(COOKING);
      else if (e == EV_STOP) { seconds = 0; enter(IDLE); }
      break;
    case DONE:
      if (e == EV_STOP || e == EV_DOOR_OPEN) { seconds = 0; enter(IDLE); }
      break;
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(START, INPUT_PULLUP);
  pinMode(STOP, INPUT_PULLUP);
  pinMode(DOOR, INPUT);
  pinMode(LAMP, OUTPUT);
  pinMode(MAGNETRON, OUTPUT);
  lastDoor = digitalRead(DOOR);
  doorOpen = lastDoor;
  Serial.println("Ready: press Start");
}

void loop() {
  sense();
  Event e;
  while (pop(e)) handle(e);
  digitalWrite(MAGNETRON, state == COOKING);
  digitalWrite(LAMP, state == COOKING || doorOpen);
}

Lessons behind it