5. Gestures

The accelerometer returns the following gestures when titlting and moving the microbit: up, down, left, right, face up, face down, freefall, 3g, 6g,

8g, shake. | Gestures are always represented as strings. | 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".

The code below displays the current gesture.
It can be laggy, with the scrolled text lagging behind the actual tilt of the microbit.
from microbit import *

while True:
    gesture = accelerometer.current_gesture()
    display.scroll(gesture, delay=80)
    sleep(50)

Exercises

  1. 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.

The code below displays an arrow indicating the direction of tilt when the top is tilted up with the bottom being tilted down.
The arrow will continue to be displayed when the microbit is continuously tilted with the top up.
from microbit import *

while True:
    if accelerometer.is_gesture("up"):
        display.show(Image.ARROW_S)
    else:
        display.clear()
    sleep(100)

Tasks

  1. Modify the code to display a North arrow when the microbit gesture is down.

  2. Modify the code to display a West arrow when the microbit gesture is left.

  3. Modify the code to display an East arrow when the microbit gesture is right.

  4. Modify the code to display an East or West arrows when the microbit gesture is right or left respectively.

  5. Modify the code to display an North, South, East or West arrows when the microbit gesture is down, up, right or left 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

The code below keeps track of tilting to the right.
Best results are seen when tilting the microbit to the right, then returning it back to a flat position.
Each new tilt to the right increases the count.
Maintaining the tilt causes the count to increase while tilted.
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

  1. Modify the code to reset the count back to 0 when the A-button is pressed.

  2. 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.

  3. 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.

  4. Modify the code to include both the A-button and 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 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.
The code below displays an arrow indicating the direction of tilt when the top of the microbit is tilted up with the bottom being tilted down.
The arrow will only be displayed briefly when the microbit is continuously tilted with the top up.
from microbit import *

while True:
    if accelerometer.was_gesture("up"):
        display.show(Image.ARROW_S)
    else:
        display.clear()
    sleep(100)

Tasks

  1. Modify the code to display a North arrow when the microbit gesture was down.

  2. Modify the code to display a West arrow when the microbit gesture was left.

  3. Modify the code to display an East arrow when the microbit gesture was right.

  4. Modify the code to display an North, South, East or West arrows when the microbit gesture was down, up, right or left 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

The code below starts at 5 then counts down 1 with each tilt to the right.
Maintaining the tilt does not change the count further.
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

  1. Modify the code to reset the count back to 5 when the count gets to 0.

  2. Keeping the modifications, modify the code further to reset the count to 5 when the A-button is pressed.

  3. 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

The code below checks for a shake gesture and adds 1 to the count variable if the microbit was shaken.
from microbit import *

count = 0
while True:
    if accelerometer.was_gesture('shake'):
        count += 1
        display.show(count)

Tasks

  1. 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

The code below checks for a sideways tilt and adds 1 to the count variable if the microbit has been tilted left or right.
The two calls to the accelerometer are connected by a logical or which returns True if one of them is True.
The backslash, \, 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

  1. Add code to reset the count back to 0 when the A-button is pressed.

  2. 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.

  3. Further modify the code to count the total number of tilts up or down.

  4. 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.
The code below will typically get 4 to 8 gestures with a 2 sec sleep.
The gestures tuple can be displayed by using a for-loop to each item in the tuple for display.
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

  1. 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.

  2. 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.