Welcome to Building:bit Superkit repository
3.Micro:bit handle control
Microbit handle control
Microbit handle control1. Learning objectives2. Building blocks3. Servo wiring4. Code analysis4.1 Face-changing king4.2 Handle5. Write and download the program6. Experimental phenomenon
1. Learning objectives
In this course, we mainly learn how to use Python programming to control the face-changing king with the micro:bit handle.
2. Building blocks
For the building blocks steps, please refer to the installation drawings of [Assembly course]-[Changing face] or the building blocks installation album in the materials.
3. Servo wiring
Insert the building blocks servo wiring into the S1 interface of the Super:bit expansion board, and the orange servo wiring into the yellow pin of S1.
As shown in the figure below:

! Notes:
When taking the course related to the building block servo for the first time, we need to remove the white building blocks installed on the servo and upload the program of this course to the micro:bit; then turn on the power switch of the Super:bit expansion board and wait for the building block servo to turn to the initial position; then, we can turn off the power, adjust the mask of the face-changing king to the closed state, as shown in the figure below, and then install the building blocks. (If you have used the program related to the face-changing king and the servo before, you can skip this step)

4. Code analysis
4.1 Face-changing king
For the program, please refer to the Changing Face code.py file.
from microbit import *import superbitimport radioimport neopixelFirst, import the libraries needed for this lesson from microbit: the superbit library is dedicated to the superbit expansion board; neopixel is used to control RGB lights; radio is used for the wireless communication function of micro:bit.
xxxxxxxxxxradio.on()radio.config(group=1)display.show(Image.HEART)np = neopixel.NeoPixel(pin12, 4)superbit.servo270(superbit.S1, 50)isplay.show(Image.HEART): Display the heart pattern on the microbit dot matrix; np = neopixel.NeoPixel(pin12, 4): RGB light initialization settings, there are 4 RGB lights, connected to the P12 pin of the microbit motherboard (you can check the hardware interface manual); superbit.servo270(superbit.S1, 90): Initialize the servo to 90 degrees;
radio.on(): Turn on the wireless function. Because the wireless function consumes more power and occupies more memory, it is turned off by default. You can also use radio.off() to turn off the wireless function;
radio.config(group=1): Configure wireless group=1, so that other microbit devices with wireless group=1 can communicate with each other. The default is 0, and the selectable group is 0~255. The set group value needs to be consistent with the handle setting, otherwise it will not communicate normally;
superbit.servo270(superbit.S1, 50): Initialize the building block servo to rotate to 50°.
xxxxxxxxxxwhile True:incoming = radio.receive()if incoming == 'up':superbit.servo270(superbit.S1, 140)elif incoming == 'down':superbit.servo270(superbit.S1, 50)...In the main loop, determine whether the Face Changing King receives the command sent by the handle, and control the movement state of the Face Changing King and the color of the RGB light.
incoming = radio.receive(): Receive the data transmitted wirelessly and save it to the incoming variable; if incoming is 'up', open the Face Changing King mask, and 'down' close the Face Changing King mask; if incoming is 'left', let the dot matrix display the duck pattern, and 'right' let the dot matrix display the cow pattern;
If incoming is 'R', the body RGB lights up red, 'G' makes the body RGB light up green, 'B' makes the body RGB light up blue, and 'Y' makes the body RGB light up yellow.
! Note:
The incoming value must correspond to the value sent by the handle. Only the same value can receive and execute commands.
4.2 Handle
Please refer to the Handle code.py file for the program.
xxxxxxxxxxfrom microbit import display, Imageimport ghandleimport radioFirst, import the libraries needed for this lesson from microbit: the ghandle library is dedicated to the micro:bit hand; radio is used for the wireless communication function of micro:bit.
xxxxxxxxxxdisplay.show(Image.HEART)radio.on()radio.config(group=1)display.show(Image.HEART): Display a heart pattern on the microbit dot matrix;
radio.on(): Turn on the wireless function. Because the wireless function consumes more power and occupies more memory, it is turned off by default. You can also use radio.off() to turn off the wireless function;
radio.config(group=1): Configure wireless group=1, so that other microbit devices with wireless group=1 can communicate with each other. The default is 0, and the selectable group is 0~255. The set group value needs to be consistent with the handle setting, otherwise it will not communicate normally;
xwhile True:if ghandle.rocker(ghandle.up):radio.send('up')display.show(Image.ARROW_N)elif ghandle.rocker(ghandle.down):radio.send('down') display.show(Image.ARROW_S) elif ghandle.rocker(ghandle.left): radio.send('left') display.show(Image.ARROW_W) elif ghandle.rocker(ghandle.right): radio.send('right') display.show(Im