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:
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.
Can you modify the code so that it only requests (and registers) tilts to the right instead of to the left?
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)?
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 )