STEP1: Connect the servo to the pin P0 of micro: bit expansion board.
STEP2: Programming
1. Drag the “read acceleration” block in micro: bit module to script area. It can be used to get the acceleration of X, Y and Z axes.
2. The output value of the accelerometer ranges between -1023 and 1023, and we can use “display” block to check it.
3. When micro: bit board is titled to the left, a negative output value will be produced along the X direction, so we need the servo to rotate toward right to compensate this angle.
Here we have to use “map” function. Mapping the value of accelerometer’s X axis (-1023~1023) onto the degree that the servo rotates (0~180 degree). After mapping, when the board lays flat on the desktop, the angle of the servo will be 90 degrees. The corresponding relations show as Table 4.1.1
Find “map…from…to…” block in “Operators” module.
4. Click Expansions->Actuator->Micro Servo and back to the former interface.
5. Drag “set pin servo to… degree” block to script area, and put it inside the “forever” block, then nest the “map…” block into it. Shown as below:
6. Fix the servo onto the expansion board with double-side adhesive tape so that they both remain relatively still.