Welcome to Mini Microbit Handle repository
1.Microbit car
Handle control car
1. Learning objectives
In this class, learn to use Python programming to read the wireless data transmission function of micro:bit, so as to realize the wireless remote control of the microbit handle bit car.
2. Introduction to wireless communication principle
Through the radio module of microbit, different devices can work together through a simple wireless network. When the radio function of microbit is turned on, a simple wireless LAN will be generated. The microbit motherboard with the radio function turned on can communicate by setting parameters within the effective range.
Wireless communication is divided into two program blocks: sending and receiving. Set the wireless group of the radio to the same group, and two microbit motherboards can communicate.
3.Code and analysis:
Car code
First, import the libraries needed for this lesson from microbit: display is used for dot matrix display, Image calls the built-in image, and radio is for wireless communication function;
display.show(Image.HAPPY): Display a smile icon 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 set 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.
Note: The value of value needs to correspond to the value sent by the controller. Only the same value can receive and execute the command.
Handle control code:
Next, we will start to write the joystick control program. After writing, we need to download the program to the micro:bit mainboard of the controller.
First, import the libraries display, sleep, Image, ghandle and radio that you need to use;
radio.on(): turn on the wireless function;
radio.config(group=1): set the wireless group = 1, which is consistent with the group of the car;
If the detection ghandle.rocker(ghandle.up) is True, it means that the joystick of the handle is pushed up, so that the wireless sends the “A” command and displays an upward icon;
If the detection ghandle.rocker(ghandle.down) is True, it means that the joystick of the handle is pushed down, so that the wireless sends the “B” command and displays a downward icon;
If the detection 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 “D” command and displays a left icon;
If the detection 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 “C” command and displays a right icon;
If the detection ghandle.rocker(ghandle. pressed) is True, it means that the joystick of the handle is pressed, so that the wireless sends the “0” command and displays the “NO” icon;
The remote control sends “0” and clears the display when there is no operation;
Next, detect the buttons and send the “E”, “F”, “G”, and “I’ commands corresponding to B1 (red), B2 (green), B3 (blue), and B4 (turn off the light).
4. Download the program
After the program is written, connect the computer and the microbit mainboard with a microUSB data cable. Please click the Flash button to download the program to the micro:bit mainboard.
1. Open the Mu software and enter the code in the editing window. Note! All English and symbols should be entered in English. Use the Tab key for indentation. The last line ends with a blank program.
2. Click the thumb 'Check' button to check if there are any errors in our code. If a cursor or underline appears on a line, it means a syntax error. Please check and modify it. If there is no error, the lower left corner will prompt that there is no problem with the detection.
3. Click the 'REPL' button to check whether the buildingbit library has been downloaded. If not, please refer to the tutorial of pre-class preparation -> importing the buildingbit library.
4. After the code is written, please click the 'Flash' button to download the program to the microbit mainboard.
5. If the download fails, please make sure that the microbit is properly connected to the computer via the microUSB data cable and that the buildingbit library has been downloaded. If the buildingbit library is not available, please refer to Pre-class Preparation -> Import buildingbit library.
5. Experimental phenomenon
After the bit car is powered on, the button on the right side of the remote control is the light-on button, and B4 is the light-off button. Pushing the joystick forward, backward, left, and right controls the car's forward, backward, left, and right movements.