Our goal


Required materials


Background


What is self defense?

Hardware connection


As below picture, let the ultrasonic module be connected to P1.

Let the human infrared sensor module be connected to P10.

Let the onboard buzzer be connected to P0.

Software


makecode

Coding


Step 1

Note:If you get a warning telling you some packages will be removed because of incompatibility issues, either follow the prompts or create a new project in the Project file menu.

Step 2

Get value from P1 and assignment it to IR block uner forever, to judge if IR is equal to 1.

If the IR parameter is equal to 1, get the ultrasonic returned value again and assignment to the ultrasonic variable.

If the ultrasonic variable is lower than 30, play a ba ding to warning.

Program

Program link:https://makecode.microbit.org/_05sYuyciH93g

If you don’t want to type these code by yourself, you can directly download the whole program from the link below:

Result


Think


Questions


More Information