In this project, we'll add sounds to our accelerometer shake game. Specifically, we'll use text-to-speech like we used in the project to command the user to do different actions throughout the game, and let them know if they've succeeded.

You'll need to wire up the Accelerometer as done in previous projects. Here is what your breadboard should look like once the accelerometer is properly wired:

In addition, you'll need the speaker hooked up — refer back to the project if you need help.

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 our new TTS sounds added:

from rstem.accel import Accel *from rstem.sound import Speech import time *ready_tts = Speech("Ready to play?") * *tilt_left_tts = Speech("Tilt left!") *no_action_tts = Speech("No action!") * *detect_tts = Speech("I detected:") *wrong_tts = Speech("Oops!") *correct_tts = Speech("Correct!") *try_again_tts = Speech("Try again another time!") * *actions_tts = [no_action_tts, tilt_left_tts] accel = Accel() NOACTION = 0 LEFT = 1 RIGHT = 2 UP = 3 SHAKE = 4 # Ready to play? SCORE_NEEDED = 10 period = 300 *ready_tts.play().wait() while True: request = LEFT action = NOACTION * actions_tts[request].play().wait() tilt_left_score = 0 for i in range(period): x, y, z = accel.forces() if x > 0.75: tilt_left_score += 1 if tilt_left_score > SCORE_NEEDED: action = LEFT break time.sleep(0.01) if request == action: * # Correct action performed! * correct_tts.play().wait() else: * # Timeout - let user know what we detected * wrong_tts.play().wait() * detect_tts.play().wait() * actions_tts[action].play().wait() break time.sleep(0.5) *try_again_tts.play().wait()

Now, let's take a look at what our new code is doing:

- On Lines 5 through 15, we create the `Speech()` that we'll play throughout the program. We'll also set up a list, `actions_tts`, that is the speech for each action that a user can take. The list is in the same order as the variables `NOACTION`, `LEFT`, etc, which is important. We'll use this list to let the user know which action we've detected.

- On Line 15, we let the user know the game is starting.

- On Line 32, we request the user to perform an specific tilt. Of course, in this version of the game, the tilt is still always just LEFT! (Don't worry, we'll change it next project).

- On Lines 47 through 53, instead of just printing out if the right or wrong action was performed, we speak to the user. And, it was wrong, we tell them the action that we think they did.

- On Line 58, we let the user know the game has ended.

Give it a try. The game is still exceptionally easy — you are only requested to tilt left, so it's hard to fail. We'll make this into a real game in the next project.

1. Can you modify the code so that it only requests (and registers) tilts to the right instead of to the left?

2. As is, you have to tilt the kit more than about 45 degrees (halfway between flat and fully on its side) before the tilt registers. Can you modify the code to make it more sensitive (less tilt to register) or less sensitive (more tilt to register)?

3. The speaker is unamplified, and sounds are sometime hard to hear. Can you modify all of the sounds to have an overdriven volume? (Hint: see )