How the Script Works

How test_tilts_intro works

The accelerometer methods are part of the microbit module, so no other imports are needed.  The instruction sleep(1000) is always recommended at the beginning of any script that prints messages to the terminal.  After that, the main loop begins.

# test_tilts_intro

from microbit import *

sleep(1000)

while True:

With the accelerometer held still, a script can get information from each axis about how much it is aligned with gravity.  Regardless of whether it’s accelerometer.get_x(), accelerometer.get_y(), or accelerometer.get_z(), the method call returns a value in the range of 1024 to 0 to -1024.

When the measurement is 1024, it means that the accelerometer’s sensing axis is aligned with gravity.  As the sensing axis is tilted further out of alignment with gravity, the value decreases.  When it’s level (pointing sideways and not at all up or down), the value is zero.  As it starts to point away from gravity, the value becomes negative.  When it’s aligned with gravity, but pointing the opposite direction, the value is -1024.   

    x = accelerometer.get_x()
    y = accelerometer.get_y()

The accelerometer x and y values are printed, and after a 0.75 second pause, the main loop repeats.

    print("x =", x, ", y =", y)
    
    sleep(750)