Script: accelerometer_servo_points_up
- Enter the accelerometer_servo_points_up script into the python.microbit.org/v/2 editor.
- Set the Script Name field to accelerometer_servo_points_up.
- Click Load/Save, and then click Download Project Hex to save your work.
- Close the Load/Save dialog box after downloading.
- In the python.microbit.org editor, click: Connect, Flash, Disconnect.
# 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