OK, so now we can play a melody and have it stop after a random amount of time (number of notes). But how do we start the whole thing and get the game rolling? Using the buttons on the Circuit Playground would be a great way to do this. However, as you will see when we make the 'potato', the buttons may not be easily accessible. Instead, let's use the accelerometer. Then we'll just shake to start.

To do this, we can just re-use the 'shake detect' method discussed in the Circuit Playground D6 Dice guide. We just need a function that returns the total acceleration value:

def get_total_accel():
    # Compute total acceleration
    X = 0
    Y = 0
    Z = 0
    for count in range(10):
        x,y,z = cpx.acceleration
        X = X + x
        Y = Y + y
        Z = Z + z
        time.sleep(0.001)
    X = X / 10
    Y = Y / 10
    Z = Z / 10

    return math.sqrt(X*X + Y*Y + Z*Z)

Then to wait for shaking, we just check the value and do nothing until it exceeds a preset value. Yes, this is pretty boring.

    # Wait for shaking
    while get_total_accel() < SHAKE_THRESHOLD:
        pass  # do nothing

This guide was first published on Apr 05, 2017. It was last updated on Apr 05, 2017.

This page (Shake to Start) was last updated on Nov 30, 2017.

Text editor powered by tinymce.