Microbit Accelerometer example

Accelerometer

This object gives you access to the on-board accelerometer. The accelerometer also provides convenience functions for detecting gestures. The recognised gestures are: up, down, left, right, face up, face down, freefall, 3g, 6g, 8g, shake.

Functions

microbit.accelerometer.get_x()
Get the acceleration measurement in the x axis, as a positive or negative integer, depending on the direction.

microbit.accelerometer.get_y()
Get the acceleration measurement in the y axis, as a positive or negative integer, depending on the direction.

microbit.accelerometer.get_z()
Get the acceleration measurement in the z axis, as a positive or negative integer, depending on the direction.

microbit.accelerometer.get_values()
Get the acceleration measurements in all axes at once, as a three-element tuple of integers ordered as X, Y, Z.

microbit.accelerometer.current_gesture()
Return the name of the current gesture.

Note

MicroPython understands the following gesture names: “up”, “down”, “left”, “right”, “face up”, “face down”, “freefall”, “3g”, “6g”, “8g”, “shake”. Gestures are always represented as strings.

microbit.accelerometer.is_gesture(name)
Return True or False to indicate if the named gesture is currently active.

microbit.accelerometer.was_gesture(name)
Return True or False to indicate if the named gesture was active since the last call.

microbit.accelerometer.get_gestures()
Return a tuple of the gesture history. The most recent is listed last. Also clears the gesture history before returning.

 

Code

This is a python example which was written in the Mu editor

[codesyntax lang=”python”]

from microbit import *

while True:
    if accelerometer.is_gesture("up"):
        display.show(Image.ARROW_S)
    elif accelerometer.is_gesture("right"):
        display.show(Image.ARROW_E)
    elif accelerometer.is_gesture("down"):
        display.show(Image.ARROW_N)
    elif accelerometer.is_gesture("left"):
        display.show(Image.ARROW_W)
    else:
        display.clear()
    sleep(20)

[/codesyntax]

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :