Okay, let's begin writing the Simon game clone!

Following the bottom-up approach that I described in the introduction, my first goal is going to be to implement the four lights of the original game. The first important decision to make is how the four light regions of the game console are going to be mapped to the ten LEDs in the Circuit Playground Express board. After thinking about it for some time, I decided to use the following layout:

gaming_simon-lights.jpg
Mapping of Simon light regions to the Circuit Playground Express LEDs

As you can see in the diagram, each of the four light regions is going to use three of the NeoPixel LEDs. Since there are only ten LEDs, the two LEDs that are in the center on each side will be used for both the top and bottom regions. Reusing LEDs for two regions is not a problem because in this game there is never more than one of the four light regions lit at any given time.

To implement these light groups I first need to show you how to work with the LEDs in this board. Make sure your board is connected to your computer and then start the Mu editor. In the editor, open the Serial panel to enter the REPL. If you don't get a Python prompt it is because the board is executing the older code.py file that is stored in it. In that case press Ctrl-C to interrupt the code as shown in the previous section.

The CircuitPython release that you are running on your board comes with preinstalled libraries to control the hardware. Things such as turning lights on an off and playing sounds are easy because of these libraries. In the REPL, you can import the hardware control library for the Circuit Playground Express board as follows:

>>> from adafruit_circuitplayground.express import cpx
>>>

After you enter the above statement, the cpx variable can be used to control all aspects of the hardware in your board. In particular, you can control the NeoPixel LEDs with the cpx.pixels attribute.

Let's see what is the state of these LEDs:

>>> cpx.pixels
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)]
>>>

Here you can see that the value of cpx.pixels is a list of 10 elements, one per LED. Each LED is represented by a 3-element tuple. The three numbers in each LED are the red, green and blue intensities, which combined can make it pretty much any color. In my case all the LEDs are set to the value (0, 0, 0), which means that the LEDs are all at their minimum intensity, or in other words, turned off.

Let's turn the first LED on to a full white, which is achieved by setting the red, green and blue intensities to the maximum value of 255:

>>> cpx.pixels[0] = (255, 255, 255)
>>>

You should now have the LED referenced by the index 0 turned on!

NeoPixel LEDs are very powerful, and you may find that turning them on to full intensity like I did above is uncomfortable to your eyes. One option that you have is to use lower intensity values. For example, using (128, 128, 128) would reduce the intensity to 50%, and (64, 64, 64) to 25%.

What I think is a more convenient option, however, is to use a global brightness control provided by the board in the cpx.pixels.brightness attribute. This attribute can be set to a value between 0 and 1 to control the intensity of all the LEDs together. I have found that a low setting of 0.1 (10% intensitty) is the best for my eyes:

>>> cpx.pixels.brightness = 0.1
>>>

As soon as you change the brightness value you should see the effect on the LED that is on. Feel free to experiment with the global brightness control until you find the setting that you like the best.

Let's enable two more LEDs, so that we start getting familiar with how LED index numbers in the code map to the actual LEDs on the board:

>>> cpx.pixels[1] = (255, 255, 255)
>>> cpx.pixels[2] = (255, 255, 255)
>>>

Now you should have three LEDs turned on to white, the ones known in the code as indexes 0, 1 and 2. If you hold the board with the USB cable on the bottom, then these three LEDs are the ones I want to use for the bottom right color region of the the game, which according to the light diagram above is the green section. So let's make these LEDs green instead of white:

>>> cpx.pixels[0] = (0, 255, 0)
>>> cpx.pixels[1] = (0, 255, 0)
>>> cpx.pixels[2] = (0, 255, 0)
>>>

You may find it a bit tedious to have to run an almost identical sentence three times to get the green area to light up. A better way to do this is with a for-loop:

>>> for led in [0, 1, 2]:
...     cpx.pixels[led] = (0, 255, 0)
... 
>>>

To enter loops like the above in the REPL you have to be aware of a trick. After you enter the cpx.pixels line and press enter, the cursor will go to the next line at the same indentation level, right below the c. To tell the REPL that the loop has ended, you have to press the Backspace or Delete keys, which will bring the cursor back to the leftmost position. At this point you can press enter to tell the REPL that you are done and want to start the execution of the loop.

You can now continue with the task of identifying the remaining light regions and how they map to LED indexes on your own if you like, but below you can see a summary of the four light regions, with the code required to light them up with the right color:

>>> for led in [5, 6, 7]:
...     cpx.pixels[led] = (255, 255, 0)
... 
>>>
>>> for led in [2, 3, 4]:
...     cpx.pixels[led] = (0, 0, 255)
... 
>>>
>>> for led in [7, 8, 9]:
...     cpx.pixels[led] = (255, 0, 0)
... 
>>>
>>> for led in [0, 1, 2]:
...     cpx.pixels[led] = (0, 255, 0)
... 
>>>

If you are trying to light up different regions, you surely noticed that when switching from one region to the next it is also necessary to turn off the old lights. This is easily accomplished by setting the intensity of the appropriate LEDs to (0, 0, 0). For example, To turn off the green light region, you would do this:

>>> for led in [0, 1, 2]:
...     cpx.pixels[led] = (0, 0, 0)
... 
>>>

At this point all the research necessary to implement the lights for the Simon game is complete. In the next section I will write some actual code!

This guide was first published on Jul 24, 2019. It was last updated on Mar 26, 2024.

This page (Light Design) was last updated on Mar 08, 2024.

Text editor powered by tinymce.