WEBVTT

00:00.330 --> 00:05.620
Let's now get the distance from an obstacle using ultrasonic sensor.

00:06.030 --> 00:08.510
So now you know basically how it works.

00:08.520 --> 00:13.740
And with this code, you will clearly understand what I previously explained in the last lesson.

00:14.070 --> 00:16.910
And then what you can do is watch, begin to listen.

00:17.070 --> 00:19.230
So everything will be super clear for you.

00:19.530 --> 00:25.170
So what we are going to do here is to print the distance we get from the ultrasonic sensor to these

00:25.170 --> 00:25.690
saleman.

00:26.190 --> 00:28.120
So how to start the program?

00:28.320 --> 00:35.290
Well, why not start by using define for the two digital pings we are going to control?

00:35.730 --> 00:41.940
So we have equal theme, which has been number three and define.

00:45.440 --> 00:47.700
In which he's been number four.

00:48.200 --> 00:50.810
All right, in the setup, I'm going to do.

00:51.890 --> 00:54.920
Most childhood begin.

00:56.720 --> 01:01.970
OK, where does moderate and then pink mold, so Echobrain.

01:04.650 --> 01:13.990
Needs to be set up as a input, be OK, because we are going to read from that B and then Pink Moon

01:14.550 --> 01:15.330
trigger happy.

01:18.490 --> 01:25.030
Is up because this is the pain we are going to use to communicate to the ultrasonic sensor.

01:25.300 --> 01:25.750
All right.

01:25.750 --> 01:27.910
And our setup is good.

01:28.240 --> 01:32.980
Now, what we're having to do is we're going to read some data from the ultrasonics.

01:32.980 --> 01:38.370
And so, Avery, let's say every 100 millisecond I saw in the loop.

01:39.160 --> 01:41.980
So what we could do is do the action.

01:42.260 --> 01:45.220
OK, and then delay 100.

01:45.820 --> 01:49.550
But I'm not going to use delay here for obvious reasons.

01:49.820 --> 01:52.760
Let's start the project with clean code.

01:52.960 --> 01:57.820
So what I'm going to do is create two variables here and signed long.

01:59.150 --> 02:01.070
That's the last time.

02:04.100 --> 02:04.670
So, Nick.

02:05.660 --> 02:06.220
Trigger.

02:06.290 --> 02:09.350
So last time we had to use the trigger on the ultrasonic sensor.

02:11.440 --> 02:15.150
And unsigned, long, ultra.

02:16.090 --> 02:16.640
So, Nick.

02:17.560 --> 02:25.210
Trigger delay, which is one hundred milliseconds, so if you remember from the last listen, we are

02:25.210 --> 02:27.630
going to do two things to get the distance.

02:27.640 --> 02:32.890
First, to trigger a trustfulness insult and then to read the data.

02:33.400 --> 02:38.870
So if we want to measure a distance every 100 milliseconds, we have to trigger the an extent.

02:39.060 --> 02:40.960
So every 100 milliseconds.

02:41.110 --> 02:43.930
So now what I'm going to do here is if.

02:45.100 --> 02:53.350
Time now, so if I want to use time now, let's do unsigned, long time now, please release.

02:54.480 --> 02:57.210
OK, minus the last time.

02:58.740 --> 03:04.600
This is the duration since the last time, if the duration is greater than the delay.

03:06.240 --> 03:15.540
What we do is we do last time and we add the delay, so I'm going quite fast here because that's something

03:15.540 --> 03:21.130
that we have seen in the time functionality section and we have also seen that multiple times since.

03:21.450 --> 03:27.840
So if it's not clear for you, please go back to the time section to practice more on this functionality

03:28.050 --> 03:30.730
and then here I can do the action.

03:31.320 --> 03:31.710
All right.

03:32.610 --> 03:38.740
There you can see once you know how to use this with the time functionality we've missed, it's really

03:38.760 --> 03:39.690
not that complicated.

03:39.690 --> 03:42.810
And you can create the set up for your project in no time.

03:43.140 --> 03:53.710
So now the action is what the action is, trigger sensor and then read the pulse on equal footing that

03:53.730 --> 03:54.990
we have two actions.

03:55.600 --> 04:01.710
What I'm going to do is to separate those two and I'm going to create a function first void.

04:02.680 --> 04:06.490
Trigger will try to sneak and some.

04:07.590 --> 04:13.860
They create a new function just so we can trigger the sensor so voided because, well, we don't need

04:13.860 --> 04:20.010
to return anything and no parameter because we have everything we need right here, we don't need to

04:20.010 --> 04:21.070
add any other parameter.

04:21.600 --> 04:29.160
So in this function, we are going to put the trigger pin too high for 10 microseconds.

04:29.460 --> 04:32.970
So we make sure that we have the PIN code trigger pin outputs.

04:33.000 --> 04:35.730
Yes, because if we do have these, then that will not work.

04:36.300 --> 04:38.310
So what I'm going to do first digital.

04:38.610 --> 04:39.240
Right?

04:40.690 --> 04:42.010
Trigger happy.

04:42.580 --> 04:48.700
Actually, I'm going to set it to low and then use Lake microseconds.

04:49.810 --> 04:52.750
We have two microseconds, why is that?

04:52.760 --> 04:56.650
Because because to trigger the sensor, you need to send a pulse.

04:56.720 --> 05:03.670
So you need to send a clear signal that goes from low to high or 10 microseconds and then to low again.

05:04.070 --> 05:10.700
The thing is that when you enter this function, you don't necessarily know what was the previous state

05:10.700 --> 05:11.360
for the trigger.

05:11.570 --> 05:14.730
So if it was already high, then that's not going to work.

05:14.960 --> 05:21.190
So we first set it too low for two microseconds, OK, just in case the signal was already high.

05:21.500 --> 05:26.150
So we can have a clear puts or the trigger and then digital.

05:26.270 --> 05:26.780
Right.

05:28.000 --> 05:32.110
Trigger in high and then we wait.

05:33.220 --> 05:34.810
Michael Tegan's.

05:36.040 --> 05:43.750
With then that's what we need, that's the minimum duration we need, and then, of course, don't forget

05:43.750 --> 05:45.280
digital rights.

05:46.490 --> 05:49.250
Trigger in law.

05:50.660 --> 05:57.890
So this would create a signal that goes low and then high for 10 microseconds and then low again, and

05:57.890 --> 06:00.420
this is the trigger for the ultrasonic sensor.

06:01.040 --> 06:04.640
So here we are going to do trigger.

06:05.940 --> 06:07.440
Ultra sonic.

06:09.320 --> 06:16.610
I'm going to call the function, so now that we have triggered the Senso, well, we need to read the

06:16.610 --> 06:24.380
A.P. to actually get the data from the DICENZO and then to convert this data into a reasonable distance

06:24.380 --> 06:26.040
that we human can reach.

06:26.300 --> 06:34.400
And I'm going to create a new function double get the truck, Sonique and distance.

06:36.220 --> 06:37.390
OK, why double?

06:37.400 --> 06:44.960
Because we are going to compute the distance in centimeters and we are going to compute actually a float

06:44.960 --> 06:46.100
or a double number.

06:46.400 --> 06:48.060
So a number with a comma.

06:48.290 --> 06:54.500
So if we have double at the end, I'm just putting a here so we don't forget we have the return with

06:54.500 --> 06:55.930
a double take.

06:56.430 --> 06:57.150
OK, void.

06:57.180 --> 06:59.450
You don't have any return but double you have a return.

06:59.810 --> 07:02.640
And now what are we going to do in this function?

07:03.080 --> 07:11.940
Well, the first thing we need to do is to get the data from the senso using the pulse in function.

07:11.960 --> 07:20.390
So it should arrange OK, so first and then uppercase I OK, both in and what are the parameters we

07:20.390 --> 07:21.090
need to give.

07:21.290 --> 07:22.880
We need to give two parameters.

07:23.030 --> 07:25.400
First, the ping equal.

07:28.740 --> 07:35.460
So that's the pain we have defined here, and that's also a pain that we have set as inputs very important,

07:36.240 --> 07:38.310
the Philippines and then the mode.

07:39.270 --> 07:46.590
I'm going to the high, so if you remember, we can have Moad High on Moad look here because the signal

07:46.590 --> 07:49.880
is low by default and then the pulse is high.

07:50.370 --> 07:51.900
Then we put the most high.

07:52.080 --> 07:54.670
If it's the opposite, you put the low.

07:54.720 --> 07:57.640
But this depends on the sensor is that we don't have a choice.

07:57.750 --> 07:59.640
That is how the sensor works.

08:00.150 --> 08:00.840
Semicolon.

08:00.840 --> 08:02.380
And of course, I'm going to say this.

08:02.390 --> 08:06.770
So the pulsing will return and unsigned long.

08:06.780 --> 08:08.100
So that's duration.

08:10.120 --> 08:14.290
It will return duration in microseconds.

08:14.320 --> 08:18.130
OK, so you will get the duration in microseconds of the post.

08:18.580 --> 08:23.600
The thing is, I'm going directly to cast it as double that.

08:23.620 --> 08:31.410
What this will do is it will read this as an unsigned long and then because the tape here is double,

08:31.420 --> 08:34.210
it would cost unsigned long as a double.

08:34.390 --> 08:38.990
And we need this because then we are going to perform operations with double.

08:39.040 --> 08:44.130
OK, so that's better to perform an operation between doubles when you already have a double time.

08:45.050 --> 08:50.360
But we get the duration in microcircuitry that puts duration micros.

08:50.390 --> 08:58.280
So this is clearer and then we are going to measure so double distance we are going to measure the distance

08:58.490 --> 09:03.640
from the duration and how to measure the distance, what unfolds, going to give you the easy formula

09:04.310 --> 09:05.480
and then the explanation.

09:06.080 --> 09:12.310
If you want the distance in centimeters, you're going to do duration divided by fifty eight.

09:12.710 --> 09:15.420
And don't forget fifty eight point zero.

09:15.690 --> 09:18.190
OK, so this is a double.

09:18.260 --> 09:19.590
This is a double.

09:19.610 --> 09:26.870
So the result will be a double if you use unsigned, long integer or similar data type.

09:27.140 --> 09:32.660
The thing is that the computation during the computation, some numbers will be truncated.

09:33.020 --> 09:36.520
OK, so you may not have a very precise result here.

09:36.530 --> 09:38.870
We use doubles, so we use a number.

09:38.870 --> 09:45.290
We come every time, which means that we would get a precise computation and then return.

09:46.670 --> 09:47.300
Dist..

09:48.450 --> 09:49.610
All right, can remove this.

09:50.440 --> 10:00.090
OK, so this is four centimeter, if you want to get the distance in inches, you have to divide by

10:00.090 --> 10:02.280
one hundred and forty eight.

10:03.460 --> 10:11.980
You can just divide by 58, get the distance in centimeter and then convert centimeters, two inches,

10:12.000 --> 10:13.760
so four inches.

10:14.260 --> 10:16.470
So how do we get this 58?

10:17.200 --> 10:18.420
Because that value will work.

10:18.430 --> 10:25.480
But it's also nice if we understand why it's 58 and I'm not going to give you the complete mathematical

10:25.690 --> 10:33.190
computation to get to that number and that number simply just a quick few points that maybe will help

10:33.190 --> 10:35.170
you to understand where it comes from.

10:35.530 --> 10:45.280
So basically to measure the distance, you need to multiply the duration of the signal with the speed

10:45.280 --> 10:46.370
of the velocity.

10:46.600 --> 10:51.100
And this is the speed of velocity of the sound in the air.

10:51.130 --> 10:53.970
OK, because this is an interesting sensor.

10:53.980 --> 10:58.730
It will send an ultrasound on each wave, which can be considered also as a sound.

10:59.260 --> 11:07.330
So this is the speed of the sound in the approximately three hundred and forty meters per second.

11:07.900 --> 11:14.120
Key thing is that the duration here is in microseconds and the distance you want is in centimeters.

11:14.440 --> 11:26.290
So if you translate meter the signal in centimeter per microsecond, then you get zero point zero thirty

11:26.290 --> 11:26.590
four.

11:27.250 --> 11:32.350
I'm not going to explain in detail how to go from there, but basically what you have to do then is

11:32.350 --> 11:36.870
to do duration times zero point zero thirty four.

11:37.360 --> 11:44.740
And this is actually the total duration for the wave from the senseor to the object and the object to

11:44.740 --> 11:47.830
the because the wave will bounce back and come back.

11:48.280 --> 11:55.840
So you need to divide the result by two to only get the duration of the wave from the sensor to the

11:55.840 --> 11:58.210
object, all just from the object to the sensor.

11:58.220 --> 11:59.310
OK, the same thing.

11:59.650 --> 12:00.970
And when you do this.

12:01.960 --> 12:03.310
OK, when you.

12:04.520 --> 12:10.610
Multiply by this number, this is the same you can see the contradiction, this is the same as dividing

12:10.610 --> 12:14.230
by about 58 or centimeters.

12:14.420 --> 12:18.440
And if you want to convert in inches by dividing by one hundred forty eight.

12:18.590 --> 12:18.890
All right.

12:18.890 --> 12:21.110
So I'm not going to go too deep on this.

12:21.440 --> 12:28.340
If you don't understand everything, that's quite OK, because practically just need to know you need

12:28.340 --> 12:31.670
to divide it by 58 or divide by 140.

12:32.210 --> 12:40.410
So in that get ultrasonic distance function, we first get the duration in micro of the pulse in function.

12:40.850 --> 12:45.000
We divide by 58 and this gives us the distance in centimeters.

12:45.350 --> 12:51.730
So then what we can do here is we can do tail that rings.

12:51.740 --> 12:55.940
Ellen, get ultra sonic.

12:57.770 --> 12:58.280
Dist..

12:59.360 --> 13:07.160
So we first trigger the senso and then we call to get ultrasonic distance function we triggers, and

13:07.160 --> 13:12.200
so we read the data from the sensor and we compute the distance.

13:12.200 --> 13:14.090
And because here we are right on a double.

13:14.130 --> 13:16.330
We just printed in the sale.

13:16.340 --> 13:17.320
We say the print.

13:17.870 --> 13:22.610
OK, note that maybe you have seen this and this seems weird to you.

13:22.620 --> 13:25.400
Why do we use daily microseconds in our code?

13:25.670 --> 13:29.130
Because I just said before that you should not use Gelee in your code.

13:29.420 --> 13:32.560
Well, the thing is that there are actually two exceptions to that.

13:32.570 --> 13:38.240
If Charamba one is to use the in the setup function because this is just the setup function that is

13:38.240 --> 13:39.800
going to be executed once.

13:39.980 --> 13:42.710
So if you need to wait two or three seconds here, that's OK.

13:42.720 --> 13:45.860
That's just going to happen once and then that's it.

13:46.280 --> 13:52.670
And in the loop function because this would happen in the function you can use if you need to, a small

13:52.670 --> 13:54.590
amount of delay microseconds.

13:54.680 --> 13:56.570
OK, here it's two and 10.

13:56.590 --> 14:00.080
So that would be 12 microseconds that you spend here at least.

14:00.290 --> 14:01.400
Now, this is very short.

14:01.430 --> 14:06.470
So this will block the program, of course, but for a very, very, very short amount of time, which

14:06.470 --> 14:07.310
is not a problem.

14:07.440 --> 14:13.520
OK, and here anyway, you need to wait 10 microseconds between those two instructions so you could

14:13.520 --> 14:18.330
do it in a different way, but that would complicate the code a lot.

14:18.740 --> 14:25.940
So here basically you are going to wait for 12 microseconds every 100 millisecond.

14:26.510 --> 14:33.530
OK, so the ratio, if we compute the ratio, you are going to wait 12 microseconds every one hundred

14:33.530 --> 14:40.940
milliseconds, which is I'm going to add three zeros, which is 100000 microseconds.

14:41.310 --> 14:45.010
I'm going to multiply that by 100 to get a percentage.

14:45.440 --> 14:53.270
You are going to wait for zero point zero 12 percent of the time, which is really, really not that

14:53.270 --> 14:53.690
much.

14:56.170 --> 15:00.820
So in this case, you can see using de microsurgeons will not be a problem for program.

15:01.450 --> 15:01.890
All right.

15:02.380 --> 15:05.020
So the code seems to be good.

15:05.560 --> 15:08.660
And now let's applaud the program here.

15:09.400 --> 15:11.710
Let's limit the truck.

15:12.900 --> 15:13.260
Nick.

15:15.910 --> 15:17.180
OK, we have an error.

15:17.440 --> 15:22.600
Duration was not declared in this scope, yes, because, of course, if I don't use the same variable

15:22.600 --> 15:24.180
name, that's not going to work.

15:24.970 --> 15:25.930
Let's upload.

15:28.700 --> 15:34.670
So it's done compiling and uploading, done uploading, and now I'm going to happen to say Moniteau.

15:35.780 --> 15:36.240
Right.

15:36.260 --> 15:43.280
So we have some values here, and you can see I have one hundred and forty centimeters, as you can

15:43.280 --> 15:48.810
see there is nothing in front of the SENSEOR but actually on this side, OK.

15:48.830 --> 15:55.130
So in front of the center, on this side of the room, there is a wall in the room, my arm and the

15:55.130 --> 15:58.850
wall is actually at one hundred and forty centimeters.

15:58.850 --> 16:00.110
So that's why you have this video.

16:00.500 --> 16:07.610
If the wall was not here, I would read maybe the maximum value, which is 400 centimetre or four meters.

16:07.910 --> 16:10.870
So if you have nothing, you will reach the maximum value.

16:10.910 --> 16:17.120
OK, now I'm going to put my hand here in front of the stencil and you can see I have No.

16:17.390 --> 16:21.900
Eight, about eight centimetre between eight and nine.

16:22.190 --> 16:28.460
I'm going to go closer and you can see that five six is closer.

16:28.610 --> 16:35.660
We get five and you can see the closer I get, we start to get some weird values.

16:35.960 --> 16:43.970
OK, but actually you can see that the two centimeter minimum that is advertised by DICENZO is not ready

16:43.970 --> 16:46.550
to centimeter in mage's.

16:47.000 --> 16:53.290
Well, the president I can get to six or five centimeters, not not less than six or five.

16:53.300 --> 16:54.770
So that is the practical value.

16:54.800 --> 17:01.190
OK, you should always measure the practical value compared to the theoretical one that is usually different.

17:01.610 --> 17:07.950
And as you can see, this is Korecki walking when I put an obstacle in front of the stencil.

17:07.970 --> 17:10.700
Well, you can see the value decreases.

17:10.700 --> 17:12.650
We have the great value in centimeters.

17:13.070 --> 17:16.010
If you want, you can measure exactly with the rule.

17:16.010 --> 17:21.620
You can you can measure the distance you get and you can see if the result is correct.

17:22.430 --> 17:22.820
All right.

17:22.830 --> 17:31.640
So now you can measure a distance from your Arduino secretes and an object on a wall that is in the

17:31.640 --> 17:35.840
under unmount of the Adreno using the ultrasonic sensor.
