Welcome to Building:bit Superkit repository
4.Micro:bit handle control
Microbit handle control
Microbit handle control1. Learning objectives2. Building blocks3. Motor wiring4. Code analysis4.1 Transport Master4.2 Handle5. Write and download the program6. Experimental phenomenon
1. Learning objectives
In this course, we mainly learn how to use Python programming to realize the remote control of the transporter with the micro:bit handle.
2. Building blocks
For the building blocks steps, please refer to the installation drawings of [Assembly course]-[Proficient carrier] or the building blocks installation album in the materials.
3. Motor wiring
The motor wiring on the left side of the car is inserted into the M1 interface of the Super:bit expansion board, and the black wire is close to the battery side;
The motor wiring on the right side of the car is inserted into the M3 interface of the Super:bit expansion board, and the black wire is close to the battery side;
The building block servo wiring is inserted into the S1 interface of the Super:bit expansion board, and the orange servo wiring is inserted 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 gear on the servo first 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 angle of the shovel of the car to be parallel to the ground, and then install the servo gear. (If you have used the program related to the transport master and the servo before, you can skip this step)

4. Code analysis
4.1 Transport Master
For the program, please refer to the Proficient carrier 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.
xRed = (255, 0, 0)Orange = (255, 165, 0)Yellow = (255, 255, 0)Green = (0, 255, 0)Blue = (0, 0, 255)Violet = (148, 0, 211)White = (255, 255, 255)color_lib = { 'Red': Red, 'Orange': Orange, 'Yellow': Yellow, 'Green': Green, 'Blue': Blue, 'Violet': Violet, 'White': White}def RGBLight_more_show(first, num, color): global np np.clear() for i in range(first, first + num): np[i] = color_lib[color] np.show()This section of the program is used to define different colors of RGB lights, and define the function RGBLight_more_show to control the color of the RGB light. This function will be called in the following main loop.
xxxxxxxxxxnp = neopixel.NeoPixel(pin12, 4)display.show(Image.HEART)superbit.servo270(superbit.S1, 120)radio.on()radio.config(group=1)display.show(Image.HAPPY): Display a smiley face pattern on the microbit dot matrix;
np = neopixel.NeoPixel(pin12, 4): Initialize the RGB lights. There are 4 RGB lights in total, connected to the P12 pin of the microbit motherboard (check the hardware interface manual);
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, 120): Initialize the servo to rotate to 120°.
xxxxxxxxxxwhile True:incoming = radio.receive()if incoming == 'up':superbit.motor_control(superbit.M1, 255, 0)superbit.motor_control(superbit.M3, 255, 0)...In the main loop, determine whether the car receives the command sent by the handle, and control the movement state of the car 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', let the transporter move forward, 'down' let the transporter move backward, 'left' let the transporter rotate left, 'right' let the transporter rotate again, and 'stop' let the transporter stop;
If incoming is 'R', the body RGB lights up red and the shovel is unloaded, 'G' let the body RGB light up green and the shovel is placed flat, 'B' let the body RGB light up blue, 'Y' let the body RGB light up yellow and the shovel is raised.
! Note:
The value of incoming needs to 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;
xxxxxxxxxxwhile 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(Image.ARROW_E) elif ghandle.rocker(ghandle.pressed): radio.send('turn_off') display.show(Image.NO) else: radio.send('stop') display.clear()If ghandle.rocker(ghandle.up) is True, it means that the joystick of the handle is pushed up, so that the wireless sends the 'up' command and displays an upward icon;
If ghandle.rocker(ghandle.down) is True, it means that the joystick of the handle is pushed down, so that the wireless sends the 'down' command and displays a downward icon;
If ghandle.rocker(ghandle.left) is True, it means that the joystick of the handle is pushed to the left, so that the wireless sends the 'left' command and displays a left icon;
If ghandle.rocker(ghandle.right) is True, it means that the joystick of the handle is pushed to the right, so that the wireless sends the 'right' command and displays a right icon;
If ghandle.rocker(ghandle. pressed) is True, it means that the joystick of the handle is pressed, so the wireless sends the 'pressed' command and displays the 'X' icon;
If the remote control has no operation, it sends 'stop' and clears the display;
xxxxxxxxxxif ghandle.