pxt-ev3/docs/tutorials/security-alert.md

106 lines
3.6 KiB
Markdown
Raw Permalink Normal View History

2018-04-11 18:31:53 +02:00
# Security Alert
## Introduction @unplugged
2018-06-12 15:46:44 +02:00
The Infrared Sensor uses infrared light waves to detect proximity to the robot. Build an security alert using the Infrared Sensor.
2018-04-11 18:31:53 +02:00
![Brick in simulator with infrared sensor](/static/tutorials/security-alert/security-alert.gif)
## Step 1
2018-06-12 15:46:44 +02:00
Open the ``||sensors:Sensors||`` Toolbox drawer. Drag out an ``||sensors:on infrared object||`` block onto the Workspace (you can place this anywhere). Select ``detected`` from the second dropdown menu.
2018-04-11 18:31:53 +02:00
![Sensor detect method dropdown selections](/static/tutorials/security-alert/detect-method-dropdown.png)
```blocks
sensors.infrared1.onEvent(InfraredSensorEvent.ObjectDetected, function () {
})
```
## Step 2
2018-06-12 15:46:44 +02:00
Open the ``||brick:Brick||`` Toolbox drawer. From the **Screen** section, drag out a ``||brick:show image||`` block onto the Workspace, and drop it into the ``||sensors:on infrared object||`` block.
```blocks
sensors.infrared1.onEvent(InfraredSensorEvent.ObjectDetected, function () {
brick.showImage(images.expressionsBigSmile)
})
```
## Step 3
In the ``||brick:show image||`` block, use the drop-down menu to select the **STOP** sign image.
2018-04-11 18:31:53 +02:00
![Screen image selections](/static/tutorials/security-alert/show-image-dropdown.png)
```blocks
sensors.infrared1.onEvent(InfraredSensorEvent.ObjectDetected, function () {
brick.showImage(images.informationStop1)
})
```
## Step 4
2018-06-12 15:46:44 +02:00
Open the ``||brick:Brick||`` Toolbox drawer. From the **Buttons** section, drag out a ``||brick:set status light to||`` block onto the Workspace, and drop it after the ``||brick:show image||`` block.
```blocks
sensors.infrared1.onEvent(InfraredSensorEvent.ObjectDetected, function () {
brick.showImage(images.informationStop1)
brick.setStatusLight(StatusLight.Orange)
})
```
## Step 5
2018-06-12 15:46:44 +02:00
In the ``||brick:set status light to||`` block, use the drop-down menu to select the ``red flash`` light
2018-04-11 18:31:53 +02:00
![Status light selection dropdown list](/static/tutorials/security-alert/set-status-light-dropdown.png)
```blocks
sensors.infrared1.onEvent(InfraredSensorEvent.ObjectDetected, function () {
brick.showImage(images.informationStop1)
brick.setStatusLight(StatusLight.RedFlash)
})
```
## Step 6
2018-06-12 15:46:44 +02:00
Open the ``||loops:Loops||`` Toolbox drawer. Drag a ``||loops:repeat||`` loop onto the Workspace, and drop it after the ``||brick:set status light to||`` block.
```blocks
sensors.infrared1.onEvent(InfraredSensorEvent.ObjectDetected, function () {
brick.showImage(images.informationStop1)
brick.setStatusLight(StatusLight.RedFlash)
for (let i = 0; i < 4; i++) {
}
})
```
## Step 7
Open the ``||music:Music||`` Toolbox drawer. Drag a ``||music:play sound effect until done||`` block onto the Workspace, and drop it into the ``||loops:repeat||`` loop.
```blocks
sensors.infrared1.onEvent(InfraredSensorEvent.ObjectDetected, function () {
brick.showImage(images.informationStop1)
brick.setStatusLight(StatusLight.RedFlash)
for (let i = 0; i < 4; i++) {
music.playSoundEffectUntilDone(sounds.animalsCatPurr)
}
})
```
## Step 8
In the ``||music:play sound effect until done||`` block, use the drop-down menu to select ``information error alarm`` sound effect.
2018-04-11 18:31:53 +02:00
![Sound effect dropdown selections](/static/tutorials/security-alert/play-sound-effect-dropdown.png)
## Step 9
2018-06-12 15:46:44 +02:00
Now, plug your EV3 Brick into the computer with the USB cable, and click the **Download** button at the bottom of your screen. Follow the directions to save your program to the EV3 Brick.
2018-06-12 15:46:44 +02:00
Attach an Infrared Sensor to Port 1 of your Ev3 Brick. Test your program by putting an object increasingly closer to the Infrared Sensor your Intruder Alert should trigger when you get too close!