In this project, we begin creating an accelerometer game that commands the user to shake and tilt the CREATOR Kit in a random order. The challenge is to correctly shake/tilt the kit even as the game gets progressively faster.

For this project, we'll start simple... There will only be one action the user can take: tilt the kit left. In subsequent projects we'll add more functionality to complete the game.

This project will only require the accelerometer. Here is what your breadboard should look like once the accelerometer is properly wired:

For this first implementation of the game, we'll always request that the user tilts the kit left. Then, we'll print out if the user correctly tilted left, or made a mistake (didn't tilt left).

Here's what the code will look like:

from rstem.accel import Accel import time accel = Accel() NOACTION = 0 LEFT = 1 RIGHT = 2 UP = 3 SHAKE = 4 # Ready to play? SCORE_NEEDED = 10 period = 300 while True: request = LEFT action = NOACTION print(request) 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: print("CORRECT!") else: print("OOPS!") break time.sleep(0.5)

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

- On Lines 6 though 14, we initialize some variables we'll need later. The variables `NOACTION`, `LEFT`, `RIGHT`, `UP`, and `SHAKE` are used to keep track of which action is requested. And, for now, this game will only use `NOACTION` and `LEFT`.

- On Lines 16 through 18, at the start of the game loop, we define two variables: `request` and `action`. The `request` is what we want the user to do, and the `action` is what the user has actually done. For this first cut at the game, we will only request that they tilt left. And, we assume at first that they have taken `NOACTION`

- On Lines 20 through 30, we record what action the user has taken. To determine the action they've taken, we test the forces on the accelerometer. We'll keep score of every time they are tilting the accelerometer to the left, and increase the score. Once they've gotten over some threshold (SCORE_NEEDED), then we know they've taken that action. The reason we need to keep score (instead of just assuming that the user is tilting the kit the first time we read the accelerometer) is that sometimes a jiggle of the kit will read as a tilt. We want to make sure that the user really meant to tilt the kit by checking a number of times.

- On Lines 32 through 36, we check if the our `request` matches the user's `action`. If not, the game is over and we exit the while loop.

Try running the program and play around with tilting the kit to the left to get an idea of how sensitive it is to picking up and registering tilts.

1. The `SCORE_NEEDED` value affects how sensitive our code is at detecting tilts, and also affects how long it takes to detect the tilt. Try changing `SCORE_NEEDED` to a high or low value, and see how it affects the tilt detection.