AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Make it, judge it, improve it

Week 10 · Unit 7.2: Decomposing problems · 3 lessons
2

Shake, tilt and flip

Computing 7: Unit 7.2, pp. 44–81
GoalUse the accelerometer’s gestures in if statements and predict what a gesture program does.
New words
gesture · a particular way of moving the micro:bit, such as a shakeaccelerometer · the sensor that notices movement and which way the board is facingwas_gesture · a question that is true if the gesture has happened since the last time it was askedfreefall · falling, with nothing holding the board up
Explanation

The accelerometer notices how the micro:bit moves and which way it is facing. We will use eight of its gestures: shake, up, down, left, right, face up, face down and freefall. The question accelerometer.was_gesture("shake") is true if the board has been shaken since the last time it was asked. The gesture name is a string, so it goes inside quotation marks and is written in lower case; face up has a space in it. Each gesture can have its own if statement and its own output. As with other sensors, the questions go inside while True so that the program keeps checking.

GestureWhat you do with the micro:bit
shakeshake it
uphold it upright with the logo at the top
downhold it upside down with the logo at the bottom
lefttilt it to the left
righttilt it to the right
face uplay it flat with the lights facing up
face downlay it flat with the lights against the table
freefallthe board is falling through the air (never drop a real one)
Worked examples
Python
1from microbit import *
2while True:
3    if accelerometer.was_gesture("face up"):
4        display.show(Image.ASLEEP)
5    if accelerometer.was_gesture("shake"):
6        display.show(Image.SURPRISED)
Output
face up: a sleeping face. Shake: a surprised face.
A desk toy goes to sleep when it is laid flat and wakes up with a surprise when it is shaken.
Python
1from microbit import *
2import music
3while True:
4    if accelerometer.was_gesture("freefall"):
5        display.show(Image.NO)
6        music.play(music.WAWAWAWAA)
Output
a cross and a sad tune when the parcel falls
A parcel carries a micro:bit that raises the alarm if the parcel is dropped. The gesture freefall is detected, and then the program gives two outputs: a picture and a sound. Note the import line.
EventWhich if is true?Picture shown
lies face upface upsleeping face
shakenshakesurprised face
lies face up againface upsleeping face
To predict a gesture program, write the events in order and the picture after each one. The board lies face up, then it is shaken, then it lies face up again.
Class activity

Gesture charades (no computer). Use a book as a pretend micro:bit and mark the logo end with a sticker or a drawing. Your partner calls out gesture names and you act them out with the book: up, down, left, right, face up, face down. Then your partner makes up a short program, for example “left shows an arrow, right shows a different arrow”, and calls gestures while you say what appears. For the freefall gesture, only talk about it – never drop a real device. If you have a micro:bit and an adult to help, try the gestures on the real board.

Practice
1
Copy the table and complete the three missing gestures from their descriptions.
DescriptionGesture
tilt it to the left?
lay it flat with the lights facing up?
the board is falling through the air?
shake itshake
2
The board is held upright and then turned upside down. What scrolls, in order?
Python
1from microbit import *
2while True:
3    if accelerometer.was_gesture("up"):
4        display.scroll("Up")
5    if accelerometer.was_gesture("down"):
6        display.scroll("Down")
3
Find the mistake in this line and write the correct line.
Python
1from microbit import *
2while True:
3    if accelerometer.was_gesture(left):
4        display.show(Image.ARROW_W)
4
Write a program that shows Image.YES when the board lies face up and Image.NO when it lies face down.