Object near tutorial (#445)

* Add 'Object Near?' tutorial

* Add to SUMMARY

* Fix screen line index

* Redo tutorial

* Paste fail typo
This commit is contained in:
Galen Nickel 2018-04-06 13:09:27 -07:00 committed by GitHub
parent ecc880cf8d
commit c90f00f6d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 175 additions and 6 deletions

View File

@ -17,6 +17,7 @@
* [What Color?](/tutorials/what-color) * [What Color?](/tutorials/what-color)
* [Line Following](/tutorials/line-following) * [Line Following](/tutorials/line-following)
* [Green Light, Red Light](/tutorials/greenlight-redlight) * [Green Light, Red Light](/tutorials/greenlight-redlight)
* [Object Near?](/tutorials/object-near)
* [Coding](/coding) * [Coding](/coding)
* [Autonomous Parking](/coding/autonomous-parking) * [Autonomous Parking](/coding/autonomous-parking)

BIN
docs/static/tutorials/object-near.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

View File

@ -1,9 +1,8 @@
# Tutorials # Tutorials
## Tutorials
Step by step guides to coding your @boardname@. Step by step guides to coding your @boardname@.
## Brick
```codecard ```codecard
[{ [{
@ -30,13 +29,25 @@ Step by step guides to coding your @boardname@.
"cardType": "tutorial", "cardType": "tutorial",
"url":"/tutorials/mindstorms-music", "url":"/tutorials/mindstorms-music",
"imageUrl":"/static/tutorials/mindstorms-music.png" "imageUrl":"/static/tutorials/mindstorms-music.png"
}, { }]
```
## Motors
```codecard
[{
"name": "Run Motors", "name": "Run Motors",
"description": "Use the buttons to start and stop the large and medium motors.", "description": "Use the buttons to start and stop the large and medium motors.",
"cardType": "tutorial", "cardType": "tutorial",
"url":"/tutorials/run-motors", "url":"/tutorials/run-motors",
"imageUrl":"/static/tutorials/run-motors.png" "imageUrl":"/static/tutorials/run-motors.png"
}, { }]
```
## Touch Sensor
```codecard
[{
"name": "Touch to Run", "name": "Touch to Run",
"description": "Press the Touch sensor and run a motor.", "description": "Press the Touch sensor and run a motor.",
"cardType": "tutorial", "cardType": "tutorial",
@ -48,7 +59,13 @@ Step by step guides to coding your @boardname@.
"cardType": "tutorial", "cardType": "tutorial",
"url":"/tutorials/touch-sensor-values", "url":"/tutorials/touch-sensor-values",
"imageUrl":"/static/tutorials/touch-sensor-values.png" "imageUrl":"/static/tutorials/touch-sensor-values.png"
}, { }]
```
## Color Sensor
```codecard
[{
"name": "What Color?", "name": "What Color?",
"description": "Use the Color sensor to detect different colors.", "description": "Use the Color sensor to detect different colors.",
"cardType": "tutorial", "cardType": "tutorial",
@ -67,4 +84,16 @@ Step by step guides to coding your @boardname@.
"url":"/tutorials/redlight-greenlight", "url":"/tutorials/redlight-greenlight",
"imageUrl":"/static/tutorials/redlight-greenlight.png" "imageUrl":"/static/tutorials/redlight-greenlight.png"
}] }]
``` ```
## Ultrasonic Sensor
```codecard
[{
"name": "Object Near?",
"description": "Build a program that will detect when an object is nearby.",
"cardType": "tutorial",
"url":"/tutorials/object-near",
"imageUrl":"/static/tutorials/object-near.png"
}]
```

View File

@ -0,0 +1,139 @@
# Object Near?
## Introduction @fullscreen
The digital Ultrasonic Sensor generates sound waves and reads their echoes to detect and measure distance from objects in centimeters. Build a program that will detect when an object is nearby.
![Simualtor with brick and ultrasonic sensor](/static/tutorials/object-near/ultrasonic-near.gif)
## Step 1
Open the ``||variables:Variables||`` Toolbox drawer. Pull a ``||variables:set item to||`` block onto the Workspace and place it into the ``||loops:forever||`` block.
```blocks
let item = 0
forever(function () {
item = 0
})
```
## Step 2
Go to the ``||variables:set item to||`` block and click the variable name dropdown and select "Rename variable...". In the window that pops up, rename the variable to ``nearness`` and click **Ok**.
```blocks
let nearness = 0
forever(function () {
nearness = 0
})
```
## Step 3
Open the ``||sensors:Sensors||`` Toolbox drawer. Drag the ``||sensors:ultrasonic distance||`` out and use it to replace the `0` in the ``||variables:set nearness to||`` block.
```blocks
let nearness = 0
forever(function () {
nearness = sensors.ultrasonic4.distance()
})
```
## Step 4
Get an ``||logic:if then||`` block from the ``||logic:Logic||`` drawer and place it below ``||variables:set nearness to||``. Find the ``||logic:0 < 0||`` conditional block and replace the ``true`` value of the ``||logic:if then||`` condition with it.
```blocks
let nearness = 0
forever(function () {
nearness = sensors.ultrasonic4.distance()
if (0 < 0) {
}
})
```
## Step 5
In the ``||logic:0 < 0||`` conditional, put the ``||variables:nearness||`` variable from the ``||variables:Variables||`` drawer in the first slot. Change the value of `0` in the second slot to `50`. This sets our range for a near object.
```blocks
let nearness = 0
forever(function () {
nearness = sensors.ultrasonic4.distance()
if (nearness < 50) {
}
})
```
## Step 6
Go get a ``||brick:show value||`` block from the ``||brick:Brick||`` Toolbox drawer and put it inside the ``||logic:if then||``. Set the string in the first slot to `"Distance (cm)"`. Get another ``||variables:nearness||`` and drop it into the second slot.
```blocks
let nearness = 0
forever(function () {
nearness = sensors.ultrasonic4.distance()
if (nearness < 50) {
brick.showValue("Distnace (cm)", nearness, 1)
}
})
```
## Step 7
Now, let's add a sound as an alert when something is near. In the ``||music:Music||`` drawer, get the ``||music:play sound effect||`` and put it just below the ``||brick:show value||``.
```blocks
let nearness = 0
forever(function () {
nearness = sensors.ultrasonic4.distance()
if (nearness < 50) {
brick.showValue("Distnace (cm)", nearness, 1)
music.playSoundEffect(sounds.animalsCatPurr)
}
})
```
## Step 8
In the ``||music:play sound effect||`` block, use the drop-down menu to select the ``information detected`` sound effect.
![Select sound effect from dropdown](/static/tutorials/object-near/play-sound-effect-dropdown.png)
When an object is near, our brick will display the distance away that the object is detected in centimeters and then say `"detected"`.
```blocks
let nearness = 0
forever(function () {
nearness = sensors.ultrasonic4.distance()
if (nearness < 50) {
brick.showValue("Distnace (cm)", nearness, 1)
music.playSoundEffect(sounds.informationDetected)
}
})
```
## Step 9
To give a little time to see the message, put a ``||loops:pause||`` after the ``||music:play sound effect||`` block. Change the time from `100` to `1500` milliseconds. Pull out a ``||brick:clear screen||`` and put it after the ``||loops:pause||``.
```blocks
let nearness = 0
forever(function () {
nearness = sensors.ultrasonic4.distance()
if (nearness < 50) {
brick.showValue("Distnace (cm)", nearness, 1)
music.playSoundEffect(sounds.informationDetected)
pause(1500)
brick.clearScreen()
}
})
```
## Step 10
Now, plug your @boardname@ 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 brick.
Attach an Ultrasonic Sensor to Port 4 of your brick. Test your program by putting an object at different distances in front of the Ultrasonic Sensor an object 50 centimeters or closer should be detected.