Recently CPC had a great time at Makerfaire UK and we took along a few games for the public to play. One of those games was a clone of the popular 80s game SIMON.
For those a little too young to remember. SIMON was a memory game that challenged the player to remember a sequence of lights that were underneath four coloured buttons around the perimeter of the game. The game increased in difficulty and speed as you progressed through the levels, before finally beating you…that part was inevitable.
For this project you will need
- A Raspberry Pi 3
- A 5V 4A Power Supply
- An IEC cable
- A barrel to screw terminal adapter
- 4 x Arcade Buttons
- A 50L Really Useful Box
- Lots of RGB LEDs / Neopixels
- A drill and drillbit to make holes in the box
For our version of the game we thought “out of the box”, as hackers / makers a little problem solving and sideways thinking is part of our workflow. So what did we use? Firstly we needed a box to contain the project and we needed a BIG box, so a 50L Really Useful Box was called for.
To power our RGB LEDs we needed a powerful power supply and for that we used a 5v 4A supply.
We then connected the barrel of the power supply to a female barrel jack connector which then broke out the connections to two screw terminals, + and GND.
What we needed now were our inputs, and we needed a robust solution, and that is where Kitronik’s range of arcade buttons came in. These buttons act as a momentary switch that are triggered when pressed firmly. Yellow, Pink, Green, Blue
With our inputs complete, now we need to work on our outputs, the SIMON game used just four LEDs, so we needed to go big! With 150 RGB LED! These RGB LED are super bright, in fact we had to reduce their output to save our eyes!
With our inputs and outputs selected we needed to find a platform that we could code to tie the project together, and that platform had to be the new Raspberry Pi 3.
We started our hardware build by laying out the RGB LED strip to ensure that it comfortably fit inside the box. With a little insulation tape we created a snaking strip of light ready for testing. We connected the strip to our Raspberry Pi, specifically GPIO18. We then connected the GND of the strip and the GND of our power supply to the Raspberry Pi GND pin using a male to female jumper wire and a terminal block to hold them together. We now had a common ground (GND). The + from our power supply was connected to the + of our LED strip using terminal block. With our LED now powered and connected to the Raspberry Pi we now move on to creating our method of control, using our arcade buttons.
Arcade buttons are relatively simple to work with. They have no polarity, meaning that there is no specific way to wire them up. We like to be consistent so chose one pin on the button that would act as ground and use that pin across the buttons. With this choice made we cut lengths of single core wire to length, we used black wire to identify our GND wire. To hold the wire in place we used a soldering iron and solder. Next we cut more coloured wire to length and soldered that to the arcade buttons. We now had four buttons soldered and ready for use, but how do we connect them to the Raspberry Pi?
The humble breadboard, used for decades was the piece of our puzzle that brought everything together. Using a breadboard and a terminal strip we connected the buttons of our game to their corresponding GPIO pins.
The complete diagram illustrates the electrical connections that we made.
We then plugged in our Raspberry Pi and started to write some code.
We used the Raspberry Pi 3 and its Raspbian operating system as there is so much support out there. We built the code for our project in Python but before we started to code we needed to install a few extra libraries for working with our RGB LEDs so here is how we did it.
To install the software you will need to connect your Raspberry Pi to the internet. Open a terminal, you can find the icon for which in the top left of the desktop.
In the terminal type the following to update the list of software that can be installed on your Raspberry Pi.
With the list up to date we now download the software and install.
This may take a little while to complete, depending on the speed of your internet connection and Raspberry Pi.
Next we download a copy of the source code that enables us to work with the RGB LEDs from the Pi. We use git clone to download the latest version of the code, then we change our directory so that we are inside the directory that we have just downloaded. Lastly we run scons to compile the code ready for installation.
To install the code we now need to change directory to the python directory and then run a Python script to install the library.
After a few minutes the library is installed and we are ready to start coding.
All of the code for this project can be downloaded via this link
To code in Python we need a Python editor, and we can find this in the main menu, under Programming,at the top left of the screen. Controversially we are using the Python 2 editor as the library we are using is written especially for this version. When the Python editor opens we will need to click on File » New File to create a new window. For ease we would recommend saving the blank document now with the filename pimon.py. Subsequent saves will now be a lot quicker.
We start the project by importing four libraries, we import all of the neopixel library using * so that we do not need to reference the neopixel library when calling classes / functions from it. On the next line we import the Button class from the new GPIO Zero library, a library designed to enable anyone to hack with the Raspberry Pi GPIO. From the time library we import the sleep function, used to control the pace of our project. Lastly we import the random library, a collection of classes and functions that we can use to add a random element of chance to our game.
Next we use four variables to identify the colour of the buttons attached to a particular GPIO pin. Here we use the GPIO Zero Button class to identify that it is a button attached to a certain pin. For example we have attached our Green arcade button to GPIO pin 4.
We now configure the variables for our RGB LEDs, for starters we set the number of LEDs in the strip, in this case 150. We earlier attached our RGB LEDs to GPIO18 of our Raspberry Pi and now we instruct the code to find the connection. We next set the frequency at which we control the LED, in this case 800khz. We next set the DMA channel used to generate the signal. Next we set the brightness of the LED. RGB LEDs are really bright, so as a matter of safety it is prudent to limit their brightness to 50%, which is 128. 100% brightness is 255. If we need to invert the signal for NPN transistors then we set the invert to True, but in this case we use False.
We now create a function to control the RGB LEDs, this function called “neo”. This function has two arguments, the color of the LEDs and the time that it should on for. The function contains a series of if..elseif conditional tests. These tests check the colour passed by the user as an argument and if the colour is identified then the LEDs are updated to show that colour. We use a for loop to update the LEDs one by one, once each LED is updated we need to instruct the strip to show the changes made. We then sleep for however long (n) may be. Then we turn off each LED using a similar for loop construct. Here we see all of the code for every colour.
Next we create another function that will flash all of the LEDs green if the correct sequence is input by the user.
We also create a function to flash all of the LEDs red.
With our functions created we now move on to the main body of code that will control the logic for our game.
We start by setting up the strip of RGB LEDs and passing the information on the strip to a variable. We then initialise the strip, strip.begin() before using a try, except construction to handle any errors. Here we create a list and store the colours used for the lights.
Next we create a while True loop that will constantly check to see if the green button has been pressed. This event is a blocker and will prevent the code from progressing further until the button has been pressed.
Once the button has been pressed we enter into a for loop that will iterate 16 times. The loop will flash the colours contained in the “lights” list for a tenth of a second.
When the for loop ends the script randomly shuffles the contents of the “lights” list and prints the contents to the screen, which we can see in the Python shell. The code sleeps for half a second before creating a new blank list called player, which will store the button presses of the player.
We next play the new randomised light sequence back to the user, the pace is rather quick at 0.3 of second per flash, but this can be changed to suit the player. We then use a for loop to reset the strip so that every LED is off.
We now create a while loop and this loop will test the length of the player list. While the list is less than four items in length it will check for a button press and then add that press to the player list, thus giving us the players choice of colours. The colour choice is repeated to the player as a means of feedback, indicating that their choice has been stored. We also print the choice to the Python shell for debug purposes.
When the player has finished entering their choices, the code will play their choices back to them. If those choices matched the original randomised choices then the correct function is called and the game will flash all of the LEDs green 9 times. But if the player choices are incorrect then the wrong function is called and the LEDs will flash red 9 times.
Our last section of code handles any errors and enables us to exit out of the project by pressing CTRL + C, should the need arise. By exiting we turn off all of the RGB LEDs and the game stops.
With our code and hardware complete we are now ready to play a simple game of Pymon.
Here is a complete code listing to use as a reference.