In this project, we'll finish up our accelerometer shake game by randomizing the subsequent command to the user and verifying that the requested action was taken.

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:

Here is what the code for this project should look like at this point (with new changes highlighted with arrows):

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

The bulk of the changes above are to add 3 new tilt methods: tilt right, tilt up, and "shake me". After adding these, we'll now have a real game! For each new tilt type, we've duplicated the code that handles the left tilt, and changed the variable names and detection to handle right tilt, tilt up, and "shake me".

The "shake me" detection is a little different, and needs some explanation. To correctly do a "shake" you must keep the CREATOR Kit level, and shake up and down. This is necessary — if we allowed the kit to be tilted while it was shaken, we wouldn't be able to tell the difference between a tilt and a shake.

So, to test for "shaking" we test to make sure that x is small (it's not tilted left or right) and that y is small (it's not tiled up or down). Then we check that z is far away from 1 (which is its normal position). Remember: when the CREATOR Kit is sitting flat on a table, z will be 1 — it means that the force of the earth is pulling it down. When the kit is moved up quickly z will go a good bit higher than 1. When the kit is moved back down quickly z will go a good bit lower than 1.

Besides testing for the new tilts, there are some other minor changes:

Now that the game is complete, give it a try and see how well you can do!

  1. Can you make the game harder? Perhaps change all the actions to single word, like "LEFT" and "SHAKE" to speed up the command and having the time-out be shorter.

  2. Can you add a "tilt down" action that is the opposite of tilt up?

  3. Can you keep a count of how many correct actions were performed correctly, and print out the number of correct actions once the game is over?

  4. Instead of printing the number of correct actions (from the last question), can you have the code say the number out loud? (Hint: you may need to create a new Speech() for each number you want to say — so you might need to limit the answer to numbers under, say, 10)

  5. Can you add a sideways shake action (that is, shake left and right, while keeping the kit level)?