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.
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
Step 1: A dark, mysterious screen
Start a new project called
ShakeIt. Set Screen1's Title toShake 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, andLabelAnswer, a big yellow question mark.App InventorMy ProjectsGuidePaletteSearch Components...User InterfaceButton?CheckBox?DatePicker?Image?Label?ListPicker?ListView?Notifier?PasswordTextBox?Slider?Spinner?Switch?TextBox?TimePicker?WebViewer?LayoutMediaDrawing and AnimationMapsSensorsSocialStorageConnectivityLEGO® MINDSTORMS®ExperimentalExtensionViewerDisplay hidden components in Viewer9:41Shake ItAsk a question, then shake the phone?ComponentsScreen1Label1LabelAnswerRenameDeletePropertiesLabelAnswer (Label)BackgroundColorNoneFontBoldFontItalicFontSize44FontTypefacedefaultHTMLFormatHasMarginsHeightAutomaticWidthAutomaticText?TextAlignmentleft : 0TextColorYellowVisibleA dark screen with a big question mark in the middle. 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.
App InventorMy ProjectsGuidePaletteSearch Components...User InterfaceLayoutMediaDrawing and AnimationMapsSensorsAccelerometerSensor?BarcodeScanner?Barometer?Clock?GyroscopeSensor?LightSensor?LocationSensor?OrientationSensor?Pedometer?ProximitySensor?SocialStorageConnectivityLEGO® MINDSTORMS®ExperimentalExtensionViewerDisplay hidden components in Viewer9:41Shake ItAsk a question, then shake the phone?Non-visible componentsAccelerometerSensor1TextToSpeech1Sound1ComponentsScreen1Label1LabelAnswerAccelerometerSensor1TextToSpeech1Sound1RenameDeletePropertiesSound1 (Sound)MinimumInterval500SourceNone...Three non visible components, under the phone. 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.
App InventorMy ProjectsGuideBlocksBuilt-inControlLogicMathTextListsDictionariesColorsVariablesProceduresScreen1Label1LabelAnswerAccelerometerSensor1TextToSpeech1Sound1Any componentRenameDeleteShow Warningsinitialize global answers to make a list“ Yes! ”“ No way! ”“ Maybe ”“ Ask again later ”“ Definitely ”“ I doubt it ”when AccelerometerSensor1.Shakingdoset LabelAnswer.Text to pick a random item list get global answersShake the phone: a random answer appears. Step 4: Speak and buzz
Add two more blocks to the Shaking event: speak the answer, and make the phone vibrate for 300 milliseconds.
App InventorMy ProjectsGuideBlocksBuilt-inControlLogicMathTextListsDictionariesColorsVariablesProceduresScreen1Label1LabelAnswerAccelerometerSensor1TextToSpeech1Sound1Any componentRenameDeleteShow Warningswhen AccelerometerSensor1.Shakingdoset LabelAnswer.Text to pick a random item list get global answerscall TextToSpeech1.SpeakmessageLabelAnswer.Textcall Sound1.Vibratemillisecs300The answer is shown, spoken, and felt. 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.App InventorMy ProjectsGuidePaletteSearch Components...User InterfaceButton?CheckBox?DatePicker?Image?Label?ListPicker?ListView?Notifier?PasswordTextBox?Slider?Spinner?Switch?TextBox?TimePicker?WebViewer?LayoutMediaDrawing and AnimationMapsSensorsSocialStorageConnectivityLEGO® MINDSTORMS®ExperimentalExtensionViewerDisplay hidden components in Viewer9:41Shake ItAsk a question, then shake the phone?Non-visible componentsAccelerometerSensor1TextToSpeech1Sound1ComponentsScreen1Label1LabelAnswerAccelerometerSensor1TextToSpeech1Sound1Button1RenameDeletePropertiesButton1 (Button)BackgroundColorDefaultEnabledFontBoldFontItalicFontSize18FontTypefacedefaultHeightAutomaticWidthAutomaticImageNone...ShaperoundedShowFeedbackTextAskTextAlignmentcenter : 1TextColorDefaultVisibleAn Ask button under the answer. App InventorMy ProjectsGuideBlocksBuilt-inControlLogicMathTextListsDictionariesColorsVariablesProceduresScreen1Label1LabelAnswerAccelerometerSensor1TextToSpeech1Sound1Button1Any componentRenameDeleteShow Warningswhen AccelerometerSensor1.Shakingdocall answerto answerdoset LabelAnswer.Text to pick a random item list get global answerscall TextToSpeech1.SpeakmessageLabelAnswer.Textcall Sound1.Vibratemillisecs300when Button1.Clickdocall answerOne procedure, called from two events. Step 6: Ask it anything
Connect the Companion, ask a question out loud, and shake the phone. Hold on tight!
9:41Shake ItAsk a question, then shake the phoneDefinitelyDefinitelyShake, 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.
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.
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.Less sensitive
Make it need a bigger shake.
Show a hint
Choose AccelerometerSensor1 in the Designer and change Sensitivity.
Notes for the teacher
Timing
- 10 minutes: steps 1 and 2.
- 20 minutes: steps 3 and 4.
- 15 minutes: step 5, the refactor into a procedure. Ask: why is one procedure better than the same blocks twice?
- 5 minutes: testing, with phones held firmly!
Mistakes to look out for
- Shaking does nothing in the emulator: that is why step 5 adds a button.
- No vibration on an iPad or iPod: they cannot vibrate.
- 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.