Let's now get the distance from an obstacle using ultrasonic sensor.
So now you know basically how it works.
And with this code, you will clearly understand what I previously explained in the last lesson.
And then what you can do is watch, begin to listen.
So everything will be super clear for you.
So what we are going to do here is to print the distance we get from the ultrasonic sensor to these
saleman.
So how to start the program?
Well, why not start by using define for the two digital pings we are going to control?
So we have equal theme, which has been number three and define.
In which he's been number four.
All right, in the setup, I'm going to do.
Most childhood begin.
OK, where does moderate and then pink mold, so Echobrain.
Needs to be set up as a input, be OK, because we are going to read from that B and then Pink Moon
trigger happy.
Is up because this is the pain we are going to use to communicate to the ultrasonic sensor.
All right.
And our setup is good.
Now, what we're having to do is we're going to read some data from the ultrasonics.
And so, Avery, let's say every 100 millisecond I saw in the loop.
So what we could do is do the action.
OK, and then delay 100.
But I'm not going to use delay here for obvious reasons.
Let's start the project with clean code.
So what I'm going to do is create two variables here and signed long.
That's the last time.
So, Nick.
Trigger.
So last time we had to use the trigger on the ultrasonic sensor.
And unsigned, long, ultra.
So, Nick.
Trigger delay, which is one hundred milliseconds, so if you remember from the last listen, we are
going to do two things to get the distance.
First, to trigger a trustfulness insult and then to read the data.
So if we want to measure a distance every 100 milliseconds, we have to trigger the an extent.
So every 100 milliseconds.
So now what I'm going to do here is if.
Time now, so if I want to use time now, let's do unsigned, long time now, please release.
OK, minus the last time.
This is the duration since the last time, if the duration is greater than the delay.
What we do is we do last time and we add the delay, so I'm going quite fast here because that's something
that we have seen in the time functionality section and we have also seen that multiple times since.
So if it's not clear for you, please go back to the time section to practice more on this functionality
and then here I can do the action.
All right.
There you can see once you know how to use this with the time functionality we've missed, it's really
not that complicated.
And you can create the set up for your project in no time.
So now the action is what the action is, trigger sensor and then read the pulse on equal footing that
we have two actions.
What I'm going to do is to separate those two and I'm going to create a function first void.
Trigger will try to sneak and some.
They create a new function just so we can trigger the sensor so voided because, well, we don't need
to return anything and no parameter because we have everything we need right here, we don't need to
add any other parameter.
So in this function, we are going to put the trigger pin too high for 10 microseconds.
So we make sure that we have the PIN code trigger pin outputs.
Yes, because if we do have these, then that will not work.
So what I'm going to do first digital.
Right?
Trigger happy.
Actually, I'm going to set it to low and then use Lake microseconds.
We have two microseconds, why is that?
Because because to trigger the sensor, you need to send a pulse.
So you need to send a clear signal that goes from low to high or 10 microseconds and then to low again.
The thing is that when you enter this function, you don't necessarily know what was the previous state
for the trigger.
So if it was already high, then that's not going to work.
So we first set it too low for two microseconds, OK, just in case the signal was already high.
So we can have a clear puts or the trigger and then digital.
Right.
Trigger in high and then we wait.
Michael Tegan's.
With then that's what we need, that's the minimum duration we need, and then, of course, don't forget
digital rights.
Trigger in law.
So this would create a signal that goes low and then high for 10 microseconds and then low again, and
this is the trigger for the ultrasonic sensor.
So here we are going to do trigger.
Ultra sonic.
I'm going to call the function, so now that we have triggered the Senso, well, we need to read the
A.P. to actually get the data from the DICENZO and then to convert this data into a reasonable distance
that we human can reach.
And I'm going to create a new function double get the truck, Sonique and distance.
OK, why double?
Because we are going to compute the distance in centimeters and we are going to compute actually a float
or a double number.
So a number with a comma.
So if we have double at the end, I'm just putting a here so we don't forget we have the return with
a double take.
OK, void.
You don't have any return but double you have a return.
And now what are we going to do in this function?
Well, the first thing we need to do is to get the data from the senso using the pulse in function.
So it should arrange OK, so first and then uppercase I OK, both in and what are the parameters we
need to give.
We need to give two parameters.
First, the ping equal.
So that's the pain we have defined here, and that's also a pain that we have set as inputs very important,
the Philippines and then the mode.
I'm going to the high, so if you remember, we can have Moad High on Moad look here because the signal
is low by default and then the pulse is high.
Then we put the most high.
If it's the opposite, you put the low.
But this depends on the sensor is that we don't have a choice.
That is how the sensor works.
Semicolon.
And of course, I'm going to say this.
So the pulsing will return and unsigned long.
So that's duration.
It will return duration in microseconds.
OK, so you will get the duration in microseconds of the post.
The thing is, I'm going directly to cast it as double that.
What this will do is it will read this as an unsigned long and then because the tape here is double,
it would cost unsigned long as a double.
And we need this because then we are going to perform operations with double.
OK, so that's better to perform an operation between doubles when you already have a double time.
But we get the duration in microcircuitry that puts duration micros.
So this is clearer and then we are going to measure so double distance we are going to measure the distance
from the duration and how to measure the distance, what unfolds, going to give you the easy formula
and then the explanation.
If you want the distance in centimeters, you're going to do duration divided by fifty eight.
And don't forget fifty eight point zero.
OK, so this is a double.
This is a double.
So the result will be a double if you use unsigned, long integer or similar data type.
The thing is that the computation during the computation, some numbers will be truncated.
OK, so you may not have a very precise result here.
We use doubles, so we use a number.
We come every time, which means that we would get a precise computation and then return.
Dist..
All right, can remove this.
OK, so this is four centimeter, if you want to get the distance in inches, you have to divide by
one hundred and forty eight.
You can just divide by 58, get the distance in centimeter and then convert centimeters, two inches,
so four inches.
So how do we get this 58?
Because that value will work.
But it's also nice if we understand why it's 58 and I'm not going to give you the complete mathematical
computation to get to that number and that number simply just a quick few points that maybe will help
you to understand where it comes from.
So basically to measure the distance, you need to multiply the duration of the signal with the speed
of the velocity.
And this is the speed of velocity of the sound in the air.
OK, because this is an interesting sensor.
It will send an ultrasound on each wave, which can be considered also as a sound.
So this is the speed of the sound in the approximately three hundred and forty meters per second.
Key thing is that the duration here is in microseconds and the distance you want is in centimeters.
So if you translate meter the signal in centimeter per microsecond, then you get zero point zero thirty
four.
I'm not going to explain in detail how to go from there, but basically what you have to do then is
to do duration times zero point zero thirty four.
And this is actually the total duration for the wave from the senseor to the object and the object to
the because the wave will bounce back and come back.
So you need to divide the result by two to only get the duration of the wave from the sensor to the
object, all just from the object to the sensor.
OK, the same thing.
And when you do this.
OK, when you.
Multiply by this number, this is the same you can see the contradiction, this is the same as dividing
by about 58 or centimeters.
And if you want to convert in inches by dividing by one hundred forty eight.
All right.
So I'm not going to go too deep on this.
If you don't understand everything, that's quite OK, because practically just need to know you need
to divide it by 58 or divide by 140.
So in that get ultrasonic distance function, we first get the duration in micro of the pulse in function.
We divide by 58 and this gives us the distance in centimeters.
So then what we can do here is we can do tail that rings.
Ellen, get ultra sonic.
Dist..
So we first trigger the senso and then we call to get ultrasonic distance function we triggers, and
so we read the data from the sensor and we compute the distance.
And because here we are right on a double.
We just printed in the sale.
We say the print.
OK, note that maybe you have seen this and this seems weird to you.
Why do we use daily microseconds in our code?
Because I just said before that you should not use Gelee in your code.
Well, the thing is that there are actually two exceptions to that.
If Charamba one is to use the in the setup function because this is just the setup function that is
going to be executed once.
So if you need to wait two or three seconds here, that's OK.
That's just going to happen once and then that's it.
And in the loop function because this would happen in the function you can use if you need to, a small
amount of delay microseconds.
OK, here it's two and 10.
So that would be 12 microseconds that you spend here at least.
Now, this is very short.
So this will block the program, of course, but for a very, very, very short amount of time, which
is not a problem.
OK, and here anyway, you need to wait 10 microseconds between those two instructions so you could
do it in a different way, but that would complicate the code a lot.
So here basically you are going to wait for 12 microseconds every 100 millisecond.
OK, so the ratio, if we compute the ratio, you are going to wait 12 microseconds every one hundred
milliseconds, which is I'm going to add three zeros, which is 100000 microseconds.
I'm going to multiply that by 100 to get a percentage.
You are going to wait for zero point zero 12 percent of the time, which is really, really not that
much.
So in this case, you can see using de microsurgeons will not be a problem for program.
All right.
So the code seems to be good.
And now let's applaud the program here.
Let's limit the truck.
Nick.
OK, we have an error.
Duration was not declared in this scope, yes, because, of course, if I don't use the same variable
name, that's not going to work.
Let's upload.
So it's done compiling and uploading, done uploading, and now I'm going to happen to say Moniteau.
Right.
So we have some values here, and you can see I have one hundred and forty centimeters, as you can
see there is nothing in front of the SENSEOR but actually on this side, OK.
So in front of the center, on this side of the room, there is a wall in the room, my arm and the
wall is actually at one hundred and forty centimeters.
So that's why you have this video.
If the wall was not here, I would read maybe the maximum value, which is 400 centimetre or four meters.
So if you have nothing, you will reach the maximum value.
OK, now I'm going to put my hand here in front of the stencil and you can see I have No.
Eight, about eight centimetre between eight and nine.
I'm going to go closer and you can see that five six is closer.
We get five and you can see the closer I get, we start to get some weird values.
OK, but actually you can see that the two centimeter minimum that is advertised by DICENZO is not ready
to centimeter in mage's.
Well, the president I can get to six or five centimeters, not not less than six or five.
So that is the practical value.
OK, you should always measure the practical value compared to the theoretical one that is usually different.
And as you can see, this is Korecki walking when I put an obstacle in front of the stencil.
Well, you can see the value decreases.
We have the great value in centimeters.
If you want, you can measure exactly with the rule.
You can you can measure the distance you get and you can see if the result is correct.
All right.
So now you can measure a distance from your Arduino secretes and an object on a wall that is in the
under unmount of the Adreno using the ultrasonic sensor.