Welcome to Building:bit Superkit repository
3.5 Swimming robot
Swimming robot
Swimming robot1. Learning Objectives2. Building Blocks3. Sensor Wiring4. Code Analysis5. Write and download the program6. Experimental phenomenon
1. Learning Objectives
In this course, we mainly learn how to use Python programming to achieve Water Monster Aerial Object Avoidance.
2. Building Blocks
For the building block steps, please refer to the installation drawings or building block installation brochures of [Assembly Course]--[Swimming robot avoid objects] in the materials.
3. Sensor Wiring
The left motor wiring 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 right motor wiring 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 infrared module is connected to the P1P2 interface.

4. Code Analysis
For the program of this course, please see the Swimming-robot.py file.
from microbit import *import WOM_Sensor_Kitimport musicimport superbitFirst, import the libraries needed for this lesson from microbit: WOM_Sensor_Kit library is used for sensors; superbit library is dedicated to superbit expansion board; music library is used to play music.
xxxxxxxxxxsuperbit.motor_control(superbit.M1, 0, 0)superbit.motor_control(superbit.M3, 0, 0)Initialize the motor to keep M1 and M3 stationary;
xxxxxxxxxxwhile True: if WOM_Sensor_Kit.WOM_ir(pin1) == 0: display.show(Image.YES) music.play(music.JUMP_UP) superbit.motor_control(superbit.M1, -255, 0) superbit.motor_control(superbit.M3, -255, 0) sleep(2000) else: display.show(Image.NO) superbit.motor_control(superbit.M1, 0, 0) superbit.motor_control(superbit.M3, 0, 0)In an infinite loop, check whether the infrared sensor (pin1) detects an object and controls the feedback. If detected (the value is 0), display the smiley YES pattern, play the jumping sound effect, and the motors M1 and M3 reverse (-255) for 2 seconds; otherwise, display the NO pattern, and the motors M1 and M3 stop
5. Write and download the program
- Open the Mu software and enter the code in the editing window. Note! All English and symbols should be entered in English mode, use the Tab key for indentation, and the last line ends with a blank program.
- Click the thumb 'Check' button to check if there are any errors in our code. If a cursor or underline appears in 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.

- Click the ‘REPL’ button to check whether the Superbit library has been downloaded. If not, please refer to [Preparation before class] --> [2.4 Python Programming Guide].

- After the program is written, connect the computer and the microbit mainboard with a microUSB data cable, and click the ‘Flash’ button to download the program to the micro:bit mainboard. (You need to click the ‘REPL’ button again to turn off the import library file function before you can download the program normally).

- If the download fails, please confirm whether the microbit is properly connected to the computer via the microUSB data cable and the Superbit Python library has been imported.
6. Experimental phenomenon
After the program runs successfully, the microbit dot matrix displays ×. When the infrared module of the water monster detects an object above, the dot matrix displays √, the water monster issues a jump up alarm and paddles forward for 2 seconds, otherwise the water monster is in a motionless state and the microbit dot matrix displays ×.