In this project, we continue the development of our Simon Says pattern matching game by adding sound to our LED flashes.

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

Here is what the code for this project should look like at this point (with new changes highlighted with arrows) — it should run similarly to the previous project, but with sound added:

# ################################## # Import Modules and Initialize Game # ################################## from rstem.button import Button from rstem.gpio import Output *from rstem.sound import Note from random import randrange import time buttons = [Button(14), Button(15), Button(23), Button(17)] lights = [Output(4), Output(18), Output(24), Output(27)] *notes = [Note('C'), Note('D'), Note('E'), Note('F')] for light in lights: light.off() play_order = [] failed = False # ################################## # Main while: loop for game # ################################## while not failed: # Add another random LED to sequence play_order.append(randrange(4)) # Play LED sequence for i in play_order: lights[i].on() * notes[i].play(0.4).wait() lights[i].off() time.sleep(0.2) # ############################# # for: loop to get player input # ############################# for i in play_order: # Get the user's button press button_pressed = Button.wait_many(buttons, timeout=3) # If user enters incorrect button, end game if button_pressed != i: failed = True break # If user enters correct button, flash LED and play tone lights[button_pressed].on() * notes[button_pressed].play(duration=None) buttons[button_pressed].wait(press=False) time.sleep(0.2) lights[button_pressed].off() * notes[button_pressed].stop() if not failed: time.sleep(1.0)

Next, we need to insert our sounds into two locations — where we flash the sequence of LEDs for the player and where the player presses a button while repeating the sequence. Here is what those two code blocks should be updated to:

Here, we've added code to play sounds:

- On Line 13, we initialize `Notes()`s, one note that corresponds to each LED.

- On Line 34, we play the note corresponding to the LED in the sequence. At this line, we previously just paused with a `time.sleep()`. Now, we play the correct note for exactly the amount of time that we previously were pausing.

- On Lines 54 and 58, we play the note that corresponds to the button that was pressed. The first line we've added starts the note playing, and the second line stops it. This way, the note will play for the entire time the user has the button pressed.

1. The notes we've chosen are at a relatively low volume for the small speaker in the CREATOR Kit. Higher pitch notes will sound louder on the speaker. Try changing the pitch of all of the notes to the same notes two octaves higher.

2. When the user is trying to repeat the sequence, the notes will play for as long as the button is pressed. Instead, change the code to only play the note for exactly 0.4 seconds.

3. Instead of notes, change the code to use `Speech()` that corresponds to the color of each led. That is, when you press the button next to the red LED, have the speaker say the word "Red".

4. Instead of notes, change the code to use `Sound()`. You could have an animal sound for each button. Some animal sounds available are: "Meow.wav", "Horse.wav", "Rooster.wav", "Kitten.wav", "Cricket.wav", "Dog1.wav", "Dog2.wav", "Goose.wav", "Owl.wav", "Bird.wav", and "Duck.wav".