Iran Macedo
4 min readSep 5, 2018

Hello everyone, this is my very first article!

In this project we’re going to practice concepts about real time systems. Event time measurement to be more specific. Hope you all enjoy!

The code complete is at the end if you want to jump tutorial.

Do you all remember Simon? Simon was a very popular game in the 80’s and 90’s in Brazil (Simon game was known as Genius game in Brazil) although he began to be sold in other countries about the 70’s (Pretty old, huh?!). The game was based in memorize and reproduce a sequence of colors and sounds that takes longer and longer every round, if you pushed the wrong color or took to long to push a button, you’re out and the game started again. You can find more information in this link: https://en.wikipedia.org/wiki/Simon_(game)

Image from https://en.wikipedia.org/wiki/Simon_(game)

So, if you are looking for an easy project to train your DIY or Python abilities this is absolutely for you.

In this tutorial we will use:

  • BeagleBone Black (Or any other board that you have that runs Python)
  • 4 Colorfull LEDs
  • 4 buttons
  • 8x 330 Ohms Resistors
  • Some jumpers

Now, let’s do it!

Building the Toy

Beaglebone has lots of resources to work with, but we will only use the GPIOs pins project this game. Take a look at the schematics:

After plug everything let’s go to the Code

Coding the game

BeagleBone has support to many programming languages that could be used to code the game. With BlackLib is possible to develop codes in c++, for example. I had tried to code this game in c++ before trying Python but I got lots of bugs everywhere and no reason why. This sucks! So I decided to search for other ways to do. And then I found Python… But it doesn’t mean that is the best way to do.

We’re gonna use Python’s Adafruit Library:
[1] — Documentation
[2] — Repository.

This isn’t a instalation tutorial, so you can check how to do on github link and any doubt about the methods, just check [1]. Ok, no more talking. Let’s code!

Step 1: Constants declaration

As mentioned before we gonna need 4 pins to the buttons and more 4 pins to the leds so we gonna first declarate which pins are going to be INPUT and OUTPUT as the schematics. (Check BeagleBone’s pinout)

Importing modules and doing the setup fo GPIO pins

Step 2: Game loop

The game loop contains the game sequence and to get a clean code I tried to named the functions to be easier to indentify the propose by its name.

At first is necessary to the player understand that the game/round is initiating after some action. In this case, the action that begin the game is to press any button. Before that the while True ensures that the game never ends (evil laughs). Flag function as mentioned before is a way to show the player that the round is initiating. In next steps is showed the other function’s implementation.

game loop

Step 3: Functions

After initiated the game has to generate sequences that start with one led and then on every round will be added a new led on the sequence. So in the fuction to generate_current_round uses random to get a new led mapped in LEDS vector and add at the game_sequence that store the sequence vector of that match.

Now it’s time to the game capture player’s play. To do that we have to detect which button was pressed. I couldn’t implement this functionality throught GPIO’s event (check documentation) cause once the event is set, is set forever. So we’re checking in lines below every pin if it is high or low. This routine keeps going on until the time to answer ends. Number_of_plays count how many buttons was pressed and the sleep function works as a debounce interval.

The validation is made comparing item by item the game’ sequence and player’s sequence.

OBS: Important to mention that where are the while True loops with a eternal blink indicate that the game ended by player’s mistake or he/she surpassed the limit time to make a move.

In our case the limit time to make a move is 3 seconds (line 76)

(Ignore the lines which been already shown, focus on the variables not showed before as game_sequence and player_sequence)

It’s DONE!

Any doubt or sugestion please post a comment or send me an email :)

Complete code

Iran Macedo

A writer to himself. A curious, challenger lover Software Developer at Avenue Code trying to improve my abilities and available to share knowledge