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:
float getTotalAccel() { // Compute total acceleration float X = 0; float Y = 0; float Z = 0; for (int i=0; i<10; i++) { X += CircuitPlayground.motionX(); Y += CircuitPlayground.motionY(); Z += CircuitPlayground.motionZ(); delay(1); } X /= 10; Y /= 10; Z /= 10; return 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 (getTotalAccel() < SHAKE_THRESHOLD) { // do nothing }
Page last edited April 04, 2017
Text editor powered by tinymce.