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've now completed the hardware for our Simon Says game and are ready to move on to writing our game code.

The hardware setup for this project should be the same setup that was completed for the project. Here's a reminder:

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 predefined list of LED flashes that we make up and write the code to execute that list of flashes. In the next project, we'll expand on that by making the flashing sequence random.

Add Initialization Code

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 (this should look familiar from previous projects):

Create a List of LED Flashes

For our game, we'll store the sequence of LED flashes in a list. Each item in that list will refer to one of the LEDs in our lights[] list above. Remember, the first item in a list is considered the zeroth element, so if we want to refer to the first LED in lights[], it would be lights[0]. The second LED would be lights[1], and so on.

So, our list of LED flashes will simply be a list of values between 0 and 3, each representing an LED in lights[]. Let's call our list sequence of LEDs play_order and we'll start by having it run through a sequence of LED flashes that we make up (we'll use 8 LED flashes, hitting each LED twice).

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

Play the List of LED Flashes

Finally, we want to actually play that sequence of flashes on our LEDs. To do that, we'll first 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.

Comments

Often when you are writing code, its helpful to put some notes or comments about what the code is doing, or why it was written the way it was, or how it works. All programming languages provide a way to do this, but for each language the format is different.

In Python, if a line ever starts with a hash ('#'), the line is a comment line. In a comment line you can write anything you want, and the computer will just ignore it. Its purpose is only to help out a person who is reading the code.

For example, the follow is some code preceded by a comment:

# Ensure all lights are off
for light in lights:
    light.off()

Its good practice to write comments that explain generally how the code works, or why it is being written, instead of focusing on the nitty-gritty details. For example, an unnecessary comment might be:

# Assign the variable meaning_of_life the value 42
meaning_of_life = 42

Here's what the play sequence should look like:

Put It All Together

Here is our full code at this point — when you run it, you will see the four LEDs flash in the hard-coded pattern that we've added to the code:

  1. The play_order only includes numbers 0 through 3, that correspond to each of the 4 LEDs. What happens if you use a number in play_order greater than 3? Why?

  2. Can you make the LEDs play in the order 0, 1, 2, 3, 0, 1, 2, 3... forever?

  3. The example in this section lights up one LED for each item in play_order. Can you modify the code to light more than one LED each time through the final for loop? For example, first light LEDs 0, 1, and 2; then 2 and 3, then 0, 2 and 3; and so on...