Script and Tests

Script: accelerometer_servo_points_up

# accelerometer_servo_points_up

from microbit import *
import math

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

    needle = ( angle - 90 + 15 ) // 30
    display.show(Image.ALL_CLOCKS[needle])
    
    if angle >= 0:
        angle = 180 - angle
    else:
        continue

    value = 0.5689 * angle + 25.6

    pin16.write_analog(value)

Tests

It's time to verify that your micro:bit LED matrix and servo behavior resemble what’s shown in the animation.

  • Hold the breadboard vertical, like it’s a car’s steering wheel.
  • Rotate it like in the animation. (See the .mp4 version here.)
  • Verify that the servo points upward as you rotate the micro:bit and servo through the 180° range.
  • Verify that the LEDs on the micro:bit LED matrix also point up as the micro:bit and servo are rotated