SIMON 3
In this project, we continue the development of our Simon-like pattern matching game by starting to write the game code.
Having issues? Check out the Troubleshooting Guide.

In our last project, we finished wiring up the four LEDs and tested to verify they work.  Assuming your buttons and LEDs are all working correctly, we're done with the circuit implementation for our Simon-like game.  Pretty amazing that you now know enough to build the hardware for an electronic toy you can buy at the store!  And in just a few projects, you'll be able to write all the code as well.

So, let's jump in to that code writing.

To start programming our game, the first thing we want to do is learn how to flash a sequence of LEDs, as this will be the basis for the entire game.  For this project, we'll start slow and just use a pre-defined list to create a planned sequence of LED flashes.  For the next project, we'll expand on that by making the flashing sequence random.

Programming Implementation

The first thing we're going to need to do is to import the modules and functions we'll be using and initialize our list of LEDs.  Here's what our initialization code should look like:



The sequence of LED flashes will be contained in a list, and the items in the list will be indexes to the LED list we implemented in the previous project.  In other words, we'll create a list of values between 0 and 3, each representing an LED.  Remember, lists start counting at 0, so the first LED is actually represented as the 0th element of the list.  The fourth LED is represented as the 3rd element of the list.

We'll call our list of LEDs play_order and we'll have it run through a quick sequence of 8 LED flashes hitting each LED twice.

Here's what our LED sequence list should look like:



Finally, we want to actually play that sequence of flashes on our LED.  First, we'll ensure that all the LEDs are turned off when we start, since we can't be sure of the previous state of the GPIOs.  Then, we'll use a for loop to run through our sequence list, turning the LEDs on one at a time for .4 seconds and then waiting .2 seconds between each flash.  Here's what the play sequence should look like:



Notice that we leave the LEDs on for .4 seconds and then have a .2 second delay between flashes.  

Here is our full code at this point:



prev| next