Your Turn: All Together Now

You can combine the xy calculated angle from Measuring Rotation Angles with the z angle introduced here to get a complete picture of how the micro:bit is oriented in space.


Example script: z_axis_and_xy_axis_angles_your_turn

  • Enter and save z_axis_and_xy_axis_angles_your_turn.
  • Flash it into the micro:bit.
# z_axis_and_xy_axis_angles_your_turn

from microbit import *
import math


while True:
    x = accelerometer.get_x()
    y = accelerometer.get_y()
    z = accelerometer.get_z()
    xy_angle = round( math.degrees( math.atan2(y, x) ) )

    if z > 1024:
        z = 1024
    elif z < -1024:
        z = -1024
    z_angle = round( math.degrees( math.acos( z/1024) ) )

    print("z_angle =", z_angle)
    print("xy_angle =", xy_angle)
  • Open the terminal and try each orientation shown in the pictures above.
  • How close can you get to the angles shown in the pictures’ terminal?