Finite State Machine

My monthly middle school library code club met this week on April Fools Day.  I thought it would be fun to make a joke or prank project but I wanted to give some boundaries to the students, too. I was a little bit worried that we might end up with some inappropriate projects.

I kept thinking about one project that a middle school student made during the Winter Carnival coding sessions.  In addition to his animation, he had a simple button on the screen that said: “Don’t press”. Of course, users are going to press it.  Pressing it made a really annoying and alarming sound. I thought this type of project would make a great April Fools project for my group.

When I started to make my own version of the “Do Not Press” project,

I realized that I wanted different things to happen: first one thing would happen, then something else, and so on. Each time you pressed the button something new would happen. I would need a way to check how many times the button had been pressed and use that to decide what would happen. I needed a finite state machine (at least a very simple one).

Screen Shot 2019-04-03 at 9.58.31 PM

A finite state machine, FSM, is a basic computer science concept for controlling the logic of a program. The concepts center around keeping track of the state and what it takes to move between different states. This example is the simplest form of FSM. Now I had a lesson to teach the students and a fun prank project for them to make, too.

Using a single variable to keep track of the state of the project can come in handy for many types of projects. I had a couple of the coders in my mind who would find learning this concept useful in the programming they like to do.

The coders did find the project fun.  A few of them expanded on my starter project and a few them took the concept and used it in their own original way.

I only wish I had not made the final state check “if state > 3” but “if state = 4” in my example.  That would have made it easier for the coders to add on more states.