10. iot:kit case05: A self defense monitoring station

10.1. Our goal


  • Let’s make a self defense monitoring station.

10.2. Required materials


10.3. Background


What is self defense?

  • You need to set a self defense device when your environmental monitor station is out of doors to prevent device damage and to warn people keep away from this device.

10.4. 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.

https://raw.githubusercontent.com/elecfreaks/learn-cn/master/microbitKit/iot_kit/images/case_05_01.png

10.5. Software


makecode

10.6. Coding


Step 1

  • Click on “Advanced” in the MakeCode Drawer to see more code sections.

https://raw.githubusercontent.com/elecfreaks/learn-cn/master/microbitKit/iot_kit/images/iot_bit_11.jpg

  • We need to add an extension for coding to the IOT. Click on the “Extension” at bottom of coding drawer, then Search for “IOT” and click on the IOT package to add it to your project. (As below picture)

https://raw.githubusercontent.com/elecfreaks/learn-cn/master/microbitKit/iot_kit/images/iot_bit_12.jpg

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. https://raw.githubusercontent.com/elecfreaks/learn-cn/master/microbitKit/iot_kit/images/case_05_02.png

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


It will play a remind sound when detecting creatures approching in a near distance.

10.7. Think


10.8. Questions


10.9. More Information