5. Gestures
up
, down
, left
, right
, face up
, face down
, freefall
, 3g
, 6g
, 8g
, shake
.3g
, 6g
and 8g
gestures occur if the microbit experiences large levels of g-force as when the microbit is moved accelerated very rapidly.face up
occurs when the microbit is flat with the front facing upwards.face down
occurs when the microbit is flat with the front facing downwards.up
occurs when the top of the microbit is tilted upwards.down
occurs when the top of the microbit is tilted downwards.left
occurs when the left of the microbit is tilted downwards.right
occurs when the right of the microbit is tilted downwards.5.1. current_gesture
- current_gesture()
Return the name of the current gesture as a string. The gestures are:
"up"
,"down"
,"left"
,"right"
,"face up"
,"face down"
,"freefall"
,"3g"
,"6g"
,"8g"
,"shake"
.
from microbit import *
while True:
gesture = accelerometer.current_gesture()
display.scroll(gesture, delay=80)
sleep(50)
Exercises
Try getting the following gestures displayed:
"up"
,"down"
,"left"
,"right"
,"face up"
,"face down"
,"shake"
.
5.2. is_gesture
- is_gesture(name)
Return True or False to indicate if the named gesture is currently active.
from microbit import *
while True:
if accelerometer.is_gesture("up"):
display.show(Image.ARROW_S)
else:
display.clear()
sleep(100)
Tasks
Modify the code to display a North arrow when the microbit gesture is
down
.Modify the code to display a West arrow when the microbit gesture is
left
.Modify the code to display an East arrow when the microbit gesture is
right
.Modify the code to display an East or West arrows when the microbit gesture is
right
orleft
respectively.Modify the code to display an North, South, East or West arrows when the microbit gesture is
down
,up
,right
orleft
respectively.
Modify the code to display a North arrow when the microbit gesture is down
.
from microbit import *
while True:
if accelerometer.is_gesture("down"):
display.show(Image.ARROW_N)
else:
display.clear()
sleep(100)
Modify the code to display a West arrow when the microbit gesture is left
.
from microbit import *
while True:
if accelerometer.is_gesture("left"):
display.show(Image.ARROW_W)
else:
display.clear()
sleep(100)
Modify the code to display an East arrow when the microbit gesture is right
.
from microbit import *
while True:
if accelerometer.is_gesture("right"):
display.show(Image.ARROW_E)
else:
display.clear()
sleep(100)
Modify the code to display an East or West arrow when the microbit gesture is right
or left
respectively.
from microbit import *
while True:
if accelerometer.is_gesture("right"):
display.show(Image.ARROW_E)
elif accelerometer.is_gesture("left"):
display.show(Image.ARROW_W)
else:
display.clear()
sleep(100)
Modify the code to display a North, South, East or West arrow when the microbit gesture is down
, up
, right
or left
respectively.
from microbit import *
while True:
if accelerometer.is_gesture("down"):
display.show(Image.ARROW_N)
elif accelerometer.is_gesture("up"):
display.show(Image.ARROW_S)
elif accelerometer.is_gesture("right"):
display.show(Image.ARROW_E)
elif accelerometer.is_gesture("left"):
display.show(Image.ARROW_W)
else:
display.clear()
sleep(100)
5.3. is_gesture counts
from microbit import *
count = 0
display.show(count)
while True:
if accelerometer.is_gesture('right'):
count += 1
display.scroll(count, delay=60)
sleep(20)
Tasks
Modify the code to reset the count back to 0 when the A-button is pressed.
Modify the code to reset the count to a number 2 less than the current count when the B-button is pressed. Hint: use the max function.
Modify the code to reset the count to a number 2 less than the current count, but not lower than 0, when the B-button is pressed.
Modify the code to include both the A-button and the B-button actions.
Modify the code to reset the count back to 0 when the A-button is pressed.
from microbit import *
count = 0
display.show(count)
while True:
if button_a.is_pressed():
count = 0
display.scroll(count, delay=60)
sleep(200)
if accelerometer.is_gesture('right'):
count += 1
display.scroll(count, delay=60)
sleep(20)
Modify the code to reset the count to a number 2 less than the current count when the B-button is pressed. Hint: use the max function.
from microbit import *
count = 0
display.show(count)
while True:
if button_b.is_pressed():
count = count - 2
display.scroll(count, delay=60)
sleep(200)
if accelerometer.is_gesture('right'):
count += 1
display.scroll(count, delay=60)
sleep(20)
Modify the code to reset the count to a number 2 less than the current count, but not lower than 0, when the B-button is pressed.
from microbit import *
count = 0
display.show(count)
while True:
if button_b.is_pressed():
count = max(0, count - 2)
display.scroll(count, delay=60)
sleep(200)
if accelerometer.is_gesture('right'):
count += 1
display.scroll(count, delay=60)
sleep(20)
Modify the code to include both the A-button and the B-button actions.
from microbit import *
count = 0
display.show(count)
while True:
if button_a.is_pressed():
count = 0
display.scroll(count, delay=60)
sleep(200)
elif button_b.is_pressed():
count = max(0, count - 2)
display.scroll(count, delay=60)
sleep(200)
if accelerometer.is_gesture('right'):
count += 1
display.scroll(count, delay=60)
sleep(20)
5.4. was_gesture
- was_gesture(name)
- Return True or False to indicate if the named gesture was active since the last check.
was_gesture
will not return True again for the same gesture unless another gesture has occurred.
from microbit import *
while True:
if accelerometer.was_gesture("up"):
display.show(Image.ARROW_S)
else:
display.clear()
sleep(100)
Tasks
Modify the code to display a North arrow when the microbit gesture was
down
.Modify the code to display a West arrow when the microbit gesture was
left
.Modify the code to display an East arrow when the microbit gesture was
right
.Modify the code to display an North, South, East or West arrows when the microbit gesture was
down
,up
,right
orleft
respectively.
Modify the code to display a North arrow when the microbit gesture was down
.
from microbit import *
while True:
if accelerometer.was_gesture("down"):
display.show(Image.ARROW_N)
else:
display.clear()
sleep(100)
Modify the code to display a West arrow when the microbit gesture was left
.
from microbit import *
while True:
if accelerometer.was_gesture("left"):
display.show(Image.ARROW_W)
else:
display.clear()
sleep(100)
Modify the code to display an East arrow when the microbit gesture was right
.
from microbit import *
while True:
if accelerometer.is_gesture("right"):
display.show(Image.ARROW_E)
else:
display.clear()
sleep(100)
Modify the code to display a North, South, East or West arrow when the microbit gesture was down
, up
, right
or left
respectively.
from microbit import *
while True:
if accelerometer.was_gesture("down"):
display.show(Image.ARROW_N)
elif accelerometer.was_gesture("up"):
display.show(Image.ARROW_S)
elif accelerometer.was_gesture("right"):
display.show(Image.ARROW_E)
elif accelerometer.was_gesture("left"):
display.show(Image.ARROW_W)
else:
display.clear()
sleep(100)
5.5. was_gesture counts
from microbit import *
count = 5
display.scroll(count)
while True:
if accelerometer.was_gesture('right'):
count -= 1
display.scroll(count, delay=60)
sleep(20)
Tasks
Modify the code to reset the count back to 5 when the count gets to 0.
Keeping the modifications, modify the code further to reset the count to 5 when the A-button is pressed.
Keeping the modifications, modify the code further to raise the count by 2 when the B-button is pressed.
Modify the code to reset the count back to 5 when the count gets to 0.
from microbit import *
count = 5
display.scroll(count)
while True:
if accelerometer.was_gesture('right'):
count -= 1
display.scroll(count, delay=60)
sleep(20)
if count == 0:
count = 5
display.scroll(count, delay=60)
sleep(200)
Keeping the modifications, modify the code further to reset the count to 5 when the A-button is pressed.
from microbit import *
count = 5
display.scroll(count)
while True:
if button_a.is_pressed():
count = 5
display.scroll(count, delay=60)
sleep(200)
if accelerometer.was_gesture('right'):
count -= 1
display.scroll(count, delay=60)
sleep(20)
if count == 0:
count = 5
display.scroll(count, delay=60)
sleep(200)
Keeping the modifications, modify the code further to raise the count by 2 when the B-button is pressed.
from microbit import *
count = 5
display.scroll(count)
while True:
if button_a.is_pressed():
count = 5
display.scroll(count, delay=60)
sleep(200)
elif button_b.is_pressed():
count = count + 2
display.scroll(count, delay=60)
sleep(200)
if accelerometer.was_gesture('right'):
count -= 1
display.scroll(count, delay=60)
sleep(20)
if count == 0:
count = 5
display.scroll(count, delay=60)
sleep(20)
5.6. shake step counter
from microbit import *
count = 0
while True:
if accelerometer.was_gesture('shake'):
count += 1
display.show(count)
Tasks
Add code to scroll “win” and reset the count back to 0 when the shake count reaches 3.
Add code to scroll “win” and reset the count back to 0 when the shake count reaches 3.
from microbit import *
count = 0
while True:
if accelerometer.was_gesture('shake'):
count += 1
display.show(count)
if count == 3:
count = 0
display.scroll("win", delay=60)
sleep(20)
5.7. tilt sideways counter
or
which returns True if one of them is True.\
, is a continuation character, that breaks up long lines for easier reading.from microbit import *
count = 0
display.show(count)
while True:
if accelerometer.was_gesture('left') or \
accelerometer.was_gesture('right'):
count += 1
display.scroll(count, delay=60)
sleep(20)
Tasks
Add code to reset the count back to 0 when the A-button is pressed.
Further modify the code to reset the count to a number 2 less than the current count, but no lower than 0, when the B-button is pressed.
Further modify the code to count the total number of tilts up or down.
Further modify the code to count the total number of tilts to the left or right or up or down.
Add code to reset the count back to 0 when the A-button is pressed.
from microbit import *
count = 0
display.show(count)
while True:
if button_a.is_pressed():
count = 0
display.scroll(count, delay=60)
sleep(200)
if accelerometer.was_gesture('left') or \
accelerometer.was_gesture('right'):
count += 1
display.scroll(count, delay=60)
sleep(20)
Further modify the code to reset the count to a number 2 less than the current count, but no lower than 0, when the B-button is pressed.
from microbit import *
count = 0
display.show(count)
while True:
if button_a.is_pressed():
count = 0
display.scroll(count, delay=60)
sleep(200)
elif button_b.is_pressed():
count = max(0, count - 2)
display.scroll(count, delay=60)
sleep(200)
if accelerometer.was_gesture('left') or \
accelerometer.was_gesture('right'):
count += 1
display.scroll(count, delay=60)
sleep(20)
Further modify the code to count the total number of tilts to the front or back instead of left and right.
from microbit import *
count = 0
display.show(count)
while True:
if button_a.is_pressed():
count = 0
display.scroll(count, delay=60)
sleep(200)
elif button_b.is_pressed():
count = max(0, count - 2)
display.scroll(count, delay=60)
sleep(200)
if accelerometer.was_gesture('up') or \
accelerometer.was_gesture('down'):
count += 1
display.scroll(count, delay=60)
sleep(20)
Further modify the code to count the total number of tilts to the left or right or front or back.
from microbit import *
count = 0
display.show(count)
while True:
if button_a.is_pressed():
count = 0
display.scroll(count, delay=60)
sleep(200)
elif button_b.is_pressed():
count = max(0, count - 2)
display.scroll(count, delay=60)
sleep(200)
if (
accelerometer.was_gesture("left")
or accelerometer.was_gesture("right")
or accelerometer.was_gesture("up")
or accelerometer.was_gesture("down")
):
count += 1
display.scroll(count, delay=60)
sleep(20)
5.8. get_gestures()
- get_gestures()
- Return a tuple of the gesture history. The most recent is listed last.Also clears the gesture history before returning.
from microbit import *
display.show('-')
while True:
gestures = accelerometer.get_gestures()
if len(gestures) > 0:
display.show(len(gestures))
sleep(1000)
for g in gestures:
display.scroll(g, delay=60)
display.scroll('-')
sleep(2000)
Exercises
Try adjusting the sleep from 2 up to 5 seconds and spinning the microbit on its edge to give the gestures in order: right, down, left, up.
Try adjusting the sleep from 2 up to 5 seconds and spinning the microbit to give the gestures in order: face up, left, face down, right.