Welcome to Building:bit Superkit repository
3.6 Spiders sense human body
Spiders sense human body
Spiders sense human body1. 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 realize the spider's automatic sensing of the human body moving forward.
2. Building Blocks
For the building block steps, please refer to the installation drawings of [Assembly Course]-[Fleeing spider] in the materials or the building block installation album.
3. Sensor 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 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;
The human infrared sensing module is connected to the P1P2 interface.

4. Code Analysis
For the program of this course, please see the Spiders-sense-human-body.py file.
from microbit import *import WOM_Sensor_Kitimport superbitFirst, import the libraries needed for this lesson from microbit: WOM_Sensor_Kit library is used for sensors; superbit library is used for superbit expansion board.
xxxxxxxxxxwhile True:if WOM_Sensor_Kit.WOM_pir(pin1) == 1:display.show(1)superbit.motor_control(superbit.M1, -255, 0)superbit.motor_control(superbit.M3, -255, 0)sleep(2000)else:display.show(0)superbit.motor_control(superbit.M1, 0, 0)superbit.motor_control(superbit.M3, 0, 0)In an infinite loop, check whether the human infrared sensor detects a human body. If a person is detected, display the number 1, and let the M1 and M3 motors reverse at a speed of 255 for 2 seconds; if no person is detected, display the number 0, and the two motors stop rotating.
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 whether 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 microbit mainboard with a microUSB data cable, please 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 connected to the computer normally via the microUSB data cable and the Superbit Python library has been imported.
6. Experimental Phenomenon
After the spider is powered on, the microbit mainboard will display 0 after initialization. When the human infrared module detects an obstacle behind it, the dot matrix displays 1 and the spider moves forward for 2 seconds. Otherwise, the spider remains motionless and the microbit mainboard displays 0.