Check the distance: Check the distance to the obstacle numerically
In the previous lesson, we confirmed in the browser that the button was pressed.
This time, let's check the distance with a browser using the distance sensor.
What is a distance sensor?
In the first place, what is a sensor is a device that detects external information and converts it into signals and data.Like the five human senses, it has the role of "perception" in machines.
Among such sensors, the distance sensor is, as the name implies, a sensor that measures distance.
An object is measured by generating a signal and detecting the time it takes for it to hit the object and bounce off, and the strength of the signal when it returns.
The output signal varies depending on the sensor such as laser, infrared ray, ultrasonic wave, etc., and the distance that can be measured and the usage situation vary depending on the signal.
Included in the Starter Kit is the ultrasonic sensor shown in the photo below.
Since ultrasonic waves are sound waves, they are not affected by the color of the object and have the advantage of being able to be used even in dark places.
Connection
There are four pins on the distance sensor, which are gnd, echo, triger, and vcc, respectively.
This time, let's connect as follows.
obniz pin number | Distance sensor pin |
---|---|
0 | gnd |
1 | echo |
2 | triger |
3 | vcc |
As you may have noticed in previous lessons, this pin number corresponds to the number entered in the "Connect to obniz" block during programming.
So, if you enter the numbers in this block correctly, it doesn't matter where you insert the part.
In future lessons, we will omit the connection table and images, so check the program to see which pin to insert.
Program
Now, let's make it possible to check the value measured by the distance sensor on the browser.
Again, we'll display the distance sensor value on the console.
It's the same as what we've done so far, so it might be easy. The point is to put it in a loop so that you can check it all the time.
Let's run it. If you move your hand or object closer to or further away from the distance sensor, the numbers will change.
Development: Make music play when you bring your hands closer
Since it is a special sensor, let's detect that your hand is approaching and play music.
In the block program, there are blocks that can play music. It is a block of "play song" in the "AI" category.
You can choose from two types of songs, "Happy Birthday" and "Jingle Bell". In the sample program, try to play the "jingle bell" when you get close to it.
The program that "plays music when you get close to your hand" is in the form of "if you do." It seems that the "IF block" used in Last lesson can be used.
If I want to put the condition "when my hand approaches" in the block condition (true part), but unlike the button, there is no block that can be used as it is.
Let's think about whether it is possible to create a condition "when the hand approaches" using the distance sensor.
As I saw on the console earlier, the value of the distance sensor becomes smaller as the object gets closer (naturally because the distance is measured).
Therefore, if we can create a condition that "when the value of the distance sensor becomes smaller than a certain value", it seems that we can judge that "the hand is approaching".
The "Logic" category, which contains blocks, contains four types of diamond-shaped blocks that can be used to create conditions.
This time, we will use the block at the top of which you can compare numbers.
In this block, you can select equal signs and inequality signs and compare the magnitude of the left and right numbers.
Select the inequality sign and enter the values in the left and right ellipses so that the conditional expression "when the value of the distance sensor becomes smaller than a certain value" is obtained.
If the number on the right is large, it will respond even if your hand is far away, and if it is small, it will not respond unless you get as close as possible.
Make your own adjustments while looking at the values displayed on the console to see what value is right for you.
Now that the conditions are complete, insert it into the diamond-shaped part of the block.
Bring your finger or mouse holding the block where you want it.
The rest is complete if you combine this block with the program you created first.
Don't forget to put it in a loop so that it will be judged many times.
Make sure the volume on your PC is not muted and try running it. Did the music play when you brought your hands closer?
By adjusting the installation location and numerical values, it seems possible to detect the arrival of a person and create a door open / close sensor.
If you measure the distance to the lid of the box, you can play Happy Birthday music when you open the box and use it for birthday surprises.(Reference: Birthday song that plays when you open the box).
You can do various things depending on your ideas, so please come up with some ideas and think of fun ways to use them!