2-switch Finite State Machine (XOR Lightbulb and Switches)

Current state: Off

Lightbulb: Off

The Math Behind It

A finite state machine can be defined as M = (S, I, O, f, g, s_0), where:

  • S is the set of all states
  • I is the set of all inputs
  • O is the set of all outputs
  • f is the transition function S times I -> S that finds the next state given an input
  • g is the output function S -> O that maps states to outputs
  • s_0 is the initial state

This program models a simple memory-less finite state machine with in a situation where two switches are wired to a lightbulb with an XOR gate. This means if one and only one switch is on, the lightbulb will light, otherwise it is off.

In our situation, the set of all states is simply [Off, On], our inputs are [00,01,10,11], our outputs are identical to the states [Off, On] (making our machine a Moore Machine by definition).

While this is not an especially complex example, it was a good way for me to learn the concept without getting too into the weeds.

It also provided me a chance to practice integrating simple Javascript with this webpage.