This is the first step of the final project of the cool's, so what I have done here first is I have
created a new project, a new empty project.
So in this step, we're going to do something that we already did before.
And actually we are going to set up the ultrasonic sensor and read the distance every 60 milliseconds
and we are going to use into it.
So let's get started.
And I'm going to go quite quickly here because, as I said, we already did that before.
So first, I am going to do some design for the things of the ultrasonic sensor.
We have to echo the in, which is here on being number three, which is an interactive and define trigger
pin.
On PIN number four and then to be able to say, I'm going first to trigger the ultrasonic sensor and
then to read the list, so to trigger the ultrasonic, since I'm going to add unsigned, long last time
ultrasonic trigger that's initialized to Mitty's all the zero to the same here and unsigned long ultrasonic.
Trigger delay, which is 60 milliseconds, so then in the void.
Look, I am going to do.
And signed long time now is released, so that time, no, we are going to use it for everything in
the world and then if time now minus the last time we have triggered the ultrasonic sensor.
So this is the duration.
If this is greater than the delay we have said, which we do is we do last time plus equal.
DeLay, and we can now so trigger the trust on each sensor, so what I'm going to do in this project
is in the voice loop.
I'm going to do the time functionalities and called the different actions.
But every action we do, I'm going to create a function here outside of the loop.
So not to trigger the ultrasonic sensor, I'm going to create.
Void.
Trigger Sonic Tensas, and let's so if we want to be able to use digital right on a pin, we need to
set it up.
So I'm going to do spin mode.
So we said the two pins actually equal pin, which will be input and spin mode trigger pin.
This one is output.
OK, and now here I am going to do digital rights with the trigger thing, I'm going to say it to low
first for two microseconds.
So delay.
Microseconds.
Two, and then I'm going to do digital right with.
Hidesign and then did a microsurgeons for 10 microseconds and finally we sit back dippin too low.
OK, so I explained why we did that actually in the ultrasonic sensor section.
So please go back to this section if you don't understand this code.
So now I'm going to call trigger ultrasonic sensor here.
And all right, that's it for the trigger, so now we are going to trigger sensor every six milliseconds
and what we need to do now is to read actually that distance that we get after we trigger the sensor.
But we are not going to use the pulse in function, which will block the program for too long.
We are going to use an interrupt.
So I'm going to create here void equal in.
Interrupt, so we are going to get a value on the equipping, which is set now inputs very important.
So this will be an indirect function with void and new parameter.
And so so this is the interactive FOLOTYN and going to do here at attach, interact with digital, begin
to interact.
Equal pain and then so let's go back to a new line to make it nicer to read Equal Pean.
Interact with no parent.
This is just the name of the function, and then we're going to use change, OK, change because we
want the arising mode and the following mode.
So when it's surprising we start the timer, when it's falling, we stop the timer and we measure the
duration.
And in this interim function, what we are going to do is first we are going to read.
So if.
Digital read things the way we do that, because, well, we use the change modes, so we don't necessarily
know if the signal is rising or falling here.
So by reading the print again, we can know this, too.
If the signal is high, it means that the signal is rising because we have a change here.
And if it's heights rising, if it's low, it's falling.
So if it's high, it means that the pulse has just.
It and then as soon as if it's low, they'll need to precise.
That means that both.
So now we're trying to do is to measure to get the time here and give the time here, and then once
we have the two times we can complete the duration of the polls and from that duration, measure the
distance.
So I'm going to create two new variables here, actually.
Three.
Which are so unsigned, long because in time, Megan.
And let's not initialize them.
We're going to initialize them and set them in the interrupt, unsigned, long spells in time and and
I'm also going to do bull new distance with the label, but this one will be false by default.
So we are going to measure the time between here and here.
When this both stopped and when the police stopped and we get the two values, I'm going to set this
flag to true.
So in the loop, we can read the distance.
And because we are going to modify over time, because we are going to modify those variables inside
an indirect output volatile in front of them.
So both in time being here will be.
Microsoft use Microsoft because it's more convenient and more precise here and then pulls in and is
Microsoft's will and then we need to do new.
Dist..
The label is.
All right, so the Internet is set up.
We have everything we need and now in the loop, what I can do simply is check if you distance available.
If we have a new distance, I can read the distance.
And I'm also going to set the flag to folks so we don't continuously come inside this, if only when
we have a valid direction.
OK, from the Internet.
And then so this step is almost done now.
We are going to create a new function, for example, here.
So double because we're going to return double get.
Ultra sonic distance, for example, and here we need to just compute the duration between those two
times, OK?
So those two are unsigned long, so it will be long, but I'm going directly to put it as double, so
they're not going to make computation which doubles it would be more convenient.
So duration micros is equal to the first in time and minus two percent in time between.
And then double distance is equal to I'm going to duration micros divided by 58 or zero, which is the
number to convert to centimeters.
So in this project, we're going to toggle between centimeters and inches and you could see and the
video.
But actually when we toggle between centimeters and inches, it's only for the display for the user.
OK, for inside the code, I'm going to keep using centimeters, OK?
Using just one unit inside the program will make the program much simpler to handle.
OK, so just one unit and then when we display on the LCD, we can print as many units and just convert
to as many units as we want.
So here I'm sticking to some images and we get the distance.
I can do retune.
Distance, but I'm also going to add some filtering, OK, in case we have a sense of that gives value
that can be quite erotic.
So cute.
I'm going to check if the distance is greater than four hundred, which should not happen, but can
happen, then return the previous distance that we actually need to set.
Yes, I'm going to create.
Double.
Previous dist..
That's initialized to 400 people just to give a number.
So if the distance is greater than the maximum we reach on the previous distance, if not, we can just
see previous.
Distance is equal to the distance we have measured right now, and then before that, I'm going to add
also a complementary filter.
So let's say previous.
Distance with 60 percent.
And the cheering distance with 40 percent, OK, and we read at 60 minutes ago, that should update
quite quickly.
All right.
So again, if you haven't dealt with this, please go back to the ultrasonic section and know that I
have this.
I'm going to be able to call it here double.
Distance is get ultrasonic distance and just to make sure it's working, I'm going to initialize cellular.
And he here print that print Ellen sense, so that will be the dissent in sending it to us for them.
All right.
And let's just take let's verify.
So I'm going to name it final product, step one.
So for each iteration of the project, I'm going to name step one, step two, steps three, etc, save
it.
OK, and now I'm going to upload the coup to the board.
And it's so it's wargaming, it's opening ceremony to see if we have something and yes, so this 140
is the wall behind me.
I'm going to put my hand in front of the SENSEOR.
And I can see, for example, here 13, 13 or 12 centimeters.
So the code is working as expected.
And that is the end of step one, which was something that we already did before.