The previous page Did You Know? This Way Up [1] explained how to use arccos to calculate the angle between the micro:bit’s z sensing axis and gravity.
When the LED display is facing straight down, so is the z sensing axis. So the angle between z and gravity is zero.
As the angle between the z-axis and gravity increases, so does the measurement. When the LED display (and z-axis) is facing straight up, it’s angle is 180°. Again, rotating past 180°, the angle decreases, like the last example where it’s back down to 135° again.
# z_axis_degree_of_tilt_try_this from microbit import * import math sleep(1000) while True: z = accelerometer.get_z() if z > 1024: z = 1024 elif z < -1024: z = -1024 z_angle = round( math.degrees( math.acos( z/1024) ) ) print("z =", z, ", z_angle =", z_angle) print() sleep(750)
z | z_angle | Direction LEDs are facing |
---|---|---|
1024 | 0 | Straight down |
728 | 45 | Half way between straight down and sideways |
Sideways | ||
-728 | 135 | Half way between sideways and straight up |
-1024 | 180 | Straight up |
Half way past straight up on way to sideways | ||
Tilted toward you by 45° | ||
Tilted away from you by 45° | ||
GND corner tilted down by 45° | ||
0 corner tilted down by 45° |
Links
[1] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-tilt-control/z-axis-which-way/did-you-know-way