Mobile apps with App Inventor

Shake It

Make a fortune telling app: ask a question, shake the phone, and it shows, speaks and buzzes its answer.

Day 5 45 to 60 minutes 6 steps Key Stage 3
Start step 1
Shake, and the phone gives its answer.

Ask the app anything. Will it rain tomorrow? Will I win the match? Shake the phone and it answers: on the screen, out loud, and with a buzz. Its answers are random, of course, so do not plan your life by it.

You will learn

  • Use the phone's accelerometer
  • Pick a random item from a list
  • Use speech and vibration together
  • Tidy repeated blocks into a procedure

You will need

  • A computer with a web browser
  • A phone or tablet with the MIT AI2 Companion app
  • Days 1 and 4, for TextToSpeech and lists

Words to know

accelerometer
A sensor that feels the phone move, tilt and shake.
pick a random item
A Lists block that gives any one item from a list.
refactor
Tidy code so it does the same thing in a better way.
Vibrate
A Sound block that makes the phone buzz for a number of milliseconds.

Step by step

  1. Step 1: A dark, mysterious screen

    Start a new project called ShakeIt. Set Screen1's Title to Shake It, both AlignHorizontal and AlignVertical to Center, and BackgroundColor to a dark purple: choose Custom in the colour list. Add two Labels: an instruction in white, and LabelAnswer, a big yellow question mark.

    A dark screen with a big question mark in the middle.
  2. Step 2: Three hidden helpers

    Add three non visible components: an AccelerometerSensor from Sensors, which feels the phone move, and a TextToSpeech and a Sound from Media, for speaking and buzzing.

    Three non visible components, under the phone.
  3. Step 3: Answers on a shake

    In Blocks, make a global list of answers. Then when AccelerometerSensor1.Shaking, set LabelAnswer's text to pick a random item from the list.

    Shake the phone: a random answer appears.
  4. Step 4: Speak and buzz

    Add two more blocks to the Shaking event: speak the answer, and make the phone vibrate for 300 milliseconds.

    The answer is shown, spoken, and felt.
  5. Step 5: A button too

    Not every device can be shaken easily, so add an Ask button as well. Both should do exactly the same thing, so move the three blocks into a procedure called answer, and call it from the Shaking event and from the button. Now there is only one place to change if you improve it.

    An Ask button under the answer.
    One procedure, called from two events.
  6. Step 6: Ask it anything

    Connect the Companion, ask a question out loud, and shake the phone. Hold on tight!

    Shake, and the phone gives its answer.

Test it

Tick each one when it works. If one does not, look back at the step it comes from.

Stretch challenges

Finished early? Try these. Each one has a hint if you get stuck.

  1. Your own answers

    Write six answers of your own, as funny as you like.

    Show a hint

    Change the text blocks in the list. Use the gear to add more sockets.

  2. A colour for every answer

    Change the background to a random colour on every answer.

    Show a hint

    In answer, set Screen1's BackgroundColor to a pick a random item from a list of colour blocks.

  3. Less sensitive

    Make it need a bigger shake.

    Show a hint

    Choose AccelerometerSensor1 in the Designer and change Sensitivity.

Notes for the teacher

Timing

  1. 10 minutes: steps 1 and 2.
  2. 20 minutes: steps 3 and 4.
  3. 15 minutes: step 5, the refactor into a procedure. Ask: why is one procedure better than the same blocks twice?
  4. 5 minutes: testing, with phones held firmly!

Mistakes to look out for

  1. Shaking does nothing in the emulator: that is why step 5 adds a button.
  2. No vibration on an iPad or iPod: they cannot vibrate.
  3. The answer is spoken before it changes: the set block must come first.

Going further

This is refactoring: changing how code is organised without changing what it does. Programmers do it all the time to keep code easy to change.