Add a Shaking Event

Use the accelerometer sensor of your phone.

Step 1

Go back to the Designer editor. From the Sensors palette, drag and drop the AccelerometerSensor component onto the Viewer. 

Step 2

Go to the Blocks editor to program the new accelerometer sensor that you just added. Drag and drop the when AccelerometerSensor1.Shaking do block onto the Viewer. This is an event handler block that will tell your phone what to do in the event of shaking.

Step 3

What do we want the app to do when the accelerometer detects shaking? Let’s program it to speak a different phrase.

You can save time by copying and pasting blocks of code:

  • Click on the purple call TextToSpeech1.Speak block to select it
  • Press Ctrl+C on your key board (a common shortcut for “Copy”)
  • Press Ctrl+V (a common shortcut for “Paste”)
  • The copied code will appear in the Viewer
  • Drag the copied code and snap it to the "do" section of the when AccelerometerSensor1.Shaking block

Change the phrase to something funny when the phone responds to shaking.

Step 4

Test it out! You can now shake your phone and it should respond by saying the new phrase.