Microbit handle control1. Learning objectives2. Building blocks3. Motor wiring4. Code analysis4.1 Water Monster4.2 Handle5. Write and download the program6. Experimental phenomenon
In this course, we mainly learn how to use Python programming to realize the microbit handle to control the water monster.
For the building blocks steps, please refer to the installation drawings of [Assembly course]-[Freestyle] in the materials or the building block installation album.
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 line 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 line is close to the battery side;
As shown below:
For the program, please refer to the Freestyle code.py file.
from microbit import *
import superbit
import radio
import neopixel
import microbit
First, import the libraries needed for this lesson from microbit: the superbit library is dedicated to the superbit expansion board; neopixel is used to control the RGB light; radio is used for the wireless communication function of micro:bit.
xnp = neopixel.NeoPixel(pin12, 4)
display.show(Image.HEART)
microbit.sleep(1000)
radio.on()
radio.config(group=1)
mode = 1
np = neopixel.NeoPixel(pin12, 4): Initialize the RGB light settings. There are 4 RGB lights in total, connected to the P12 pin of the microbit motherboard (you can check the hardware interface manual for details);
display.show(Image.HEART): Display a heart pattern on the microbit dot matrix;
microbit.sleep(1000): Delay for 1 second;
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 normal communication will not be possible;
mode = 1: Initialize the variable mode to 1.
xxxxxxxxxx
while True:
display.show(mode)
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 the command sent by the handle to control the movement state of the water monster 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', the motor will rotate, and if it is 'stop' and mode = 1, the motor will stop rotating;
If incoming is 'R', the body RGB will light up red, 'G' will light up green, 'B' will light up blue, and 'Y' will light up yellow.
xxxxxxxxxx
if incoming == 'turn_off':
np.clear()
np.show()
if mode == 1:
mode = 2
else:
mode = 1
Switch the current mode.
! Note:
The value of incoming needs to correspond to the value sent by the handle. Only the same value can receive and execute the command.
For the program, please refer to the Handle code.py file.
xxxxxxxxxx
from microbit import display, Image
import ghandle
import radio
First, import the libraries needed in 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.
xxxxxxxxxx
display.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;
xxxxxxxxxx
while 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, send 'stop' and clear the display;
Special processing:
xxxxxxxxxx
if ghandle.rocker(ghandle.pressed):
if rocker_key == 0:
rocker_key = 1
radio.send('turn_off')
display.show(Image.NO)
else:
rocker_key = 0
When the joystick is pressed, we use a variable rocker_key to ensure that only one turn_off command is sent. Corresponding to the program of the water monster, change the value of mode once.
xxxxxxxxxx
if ghandle.B1_is_pressed():
radio.send('R')
display.show("R")
if ghandle.B2_is_pressed():
radio.send('G')
display.show("G")
if ghandle.B3_is_pressed():
radio.send('B')
display.show("B")
if ghandle.B4_is_pressed():
radio.send('Y')
display.show("Y")
Next, detect the buttons and send R', 'G', 'B', 'Y' commands to B1 (red), B2 (green), B3 (blue), and B4 (yellow).
We need to download the Freestyle code.py file to the micro:bit motherboard of the water monster, turn on the power switch of the water monster, and we can see a smiley face pattern displayed on the micro:bit dot matrix;
Download the Handle code.py file to the micro:bit motherboard of the handle, turn on the power switch of the handle, we can see that the micro:bit dot matrix will be initialized to display a heart pattern, and then an "X" pattern will be displayed, indicating that the handle is in the default state and no data is sent.
The two will automatically complete the pairing, and then we can start to remotely control the water monster.
The handle function is as follows.
Handle joystick control:
After the handle and the water monster are successfully paired, we can see that the number 1 is displayed on the micro:bit dot matrix of the water monster, indicating that it is in mode 1 at this time.
In mode 1:
We can press the joystick to switch to mode 2. At this time, we can see the number 2 displayed on the micro:bit dot matrix of the paddle monster, indicating that it is in mode 2.
In mode 2:
Each time you press the joystick, it will switch back and forth between mode 1 and mode 2, and the RGB light will go out.
Handle gravity control:
After the handle and the water monster are paired successfully, we can see the number 1 displayed on the micro:bit dot matrix of the water monster.
(Note: There is only one mode for handle gravity control, and mode 2 cannot be switched)