WEBVTT

00:00.180 --> 00:06.900
This is the first step of the final project of the cool's, so what I have done here first is I have

00:06.900 --> 00:10.470
created a new project, a new empty project.

00:10.800 --> 00:14.360
So in this step, we're going to do something that we already did before.

00:14.550 --> 00:21.510
And actually we are going to set up the ultrasonic sensor and read the distance every 60 milliseconds

00:21.510 --> 00:23.450
and we are going to use into it.

00:23.880 --> 00:24.960
So let's get started.

00:24.960 --> 00:30.300
And I'm going to go quite quickly here because, as I said, we already did that before.

00:30.840 --> 00:36.150
So first, I am going to do some design for the things of the ultrasonic sensor.

00:36.180 --> 00:45.380
We have to echo the in, which is here on being number three, which is an interactive and define trigger

00:45.420 --> 00:45.750
pin.

00:47.060 --> 00:53.720
On PIN number four and then to be able to say, I'm going first to trigger the ultrasonic sensor and

00:53.720 --> 01:01.670
then to read the list, so to trigger the ultrasonic, since I'm going to add unsigned, long last time

01:02.510 --> 01:14.540
ultrasonic trigger that's initialized to Mitty's all the zero to the same here and unsigned long ultrasonic.

01:16.610 --> 01:23.970
Trigger delay, which is 60 milliseconds, so then in the void.

01:24.620 --> 01:26.600
Look, I am going to do.

01:28.630 --> 01:38.230
And signed long time now is released, so that time, no, we are going to use it for everything in

01:38.230 --> 01:46.370
the world and then if time now minus the last time we have triggered the ultrasonic sensor.

01:46.390 --> 01:47.940
So this is the duration.

01:48.190 --> 01:57.250
If this is greater than the delay we have said, which we do is we do last time plus equal.

01:58.390 --> 02:09.040
DeLay, and we can now so trigger the trust on each sensor, so what I'm going to do in this project

02:09.040 --> 02:10.300
is in the voice loop.

02:10.300 --> 02:14.440
I'm going to do the time functionalities and called the different actions.

02:14.650 --> 02:20.370
But every action we do, I'm going to create a function here outside of the loop.

02:20.830 --> 02:24.160
So not to trigger the ultrasonic sensor, I'm going to create.

02:25.150 --> 02:25.690
Void.

02:27.090 --> 02:38.400
Trigger Sonic Tensas, and let's so if we want to be able to use digital right on a pin, we need to

02:38.400 --> 02:39.150
set it up.

02:39.600 --> 02:41.480
So I'm going to do spin mode.

02:41.910 --> 02:51.180
So we said the two pins actually equal pin, which will be input and spin mode trigger pin.

02:52.080 --> 02:53.970
This one is output.

02:56.860 --> 03:06.760
OK, and now here I am going to do digital rights with the trigger thing, I'm going to say it to low

03:06.760 --> 03:08.670
first for two microseconds.

03:08.670 --> 03:09.460
So delay.

03:11.130 --> 03:12.270
Microseconds.

03:13.560 --> 03:17.070
Two, and then I'm going to do digital right with.

03:18.560 --> 03:29.940
Hidesign and then did a microsurgeons for 10 microseconds and finally we sit back dippin too low.

03:30.710 --> 03:36.020
OK, so I explained why we did that actually in the ultrasonic sensor section.

03:36.030 --> 03:39.960
So please go back to this section if you don't understand this code.

03:40.340 --> 03:43.910
So now I'm going to call trigger ultrasonic sensor here.

03:45.680 --> 03:52.060
And all right, that's it for the trigger, so now we are going to trigger sensor every six milliseconds

03:52.640 --> 03:58.930
and what we need to do now is to read actually that distance that we get after we trigger the sensor.

03:59.240 --> 04:03.860
But we are not going to use the pulse in function, which will block the program for too long.

04:04.040 --> 04:05.780
We are going to use an interrupt.

04:06.050 --> 04:10.490
So I'm going to create here void equal in.

04:11.630 --> 04:17.240
Interrupt, so we are going to get a value on the equipping, which is set now inputs very important.

04:18.030 --> 04:21.890
So this will be an indirect function with void and new parameter.

04:22.310 --> 04:31.780
And so so this is the interactive FOLOTYN and going to do here at attach, interact with digital, begin

04:32.060 --> 04:34.130
to interact.

04:35.070 --> 04:43.820
Equal pain and then so let's go back to a new line to make it nicer to read Equal Pean.

04:44.940 --> 04:48.220
Interact with no parent.

04:48.250 --> 04:54.600
This is just the name of the function, and then we're going to use change, OK, change because we

04:54.600 --> 04:57.390
want the arising mode and the following mode.

04:57.420 --> 05:04.320
So when it's surprising we start the timer, when it's falling, we stop the timer and we measure the

05:04.320 --> 05:04.840
duration.

05:05.460 --> 05:09.490
And in this interim function, what we are going to do is first we are going to read.

05:09.510 --> 05:10.080
So if.

05:11.610 --> 05:21.930
Digital read things the way we do that, because, well, we use the change modes, so we don't necessarily

05:21.930 --> 05:25.510
know if the signal is rising or falling here.

05:26.220 --> 05:30.140
So by reading the print again, we can know this, too.

05:30.150 --> 05:36.700
If the signal is high, it means that the signal is rising because we have a change here.

05:36.720 --> 05:39.490
And if it's heights rising, if it's low, it's falling.

05:40.320 --> 05:45.150
So if it's high, it means that the pulse has just.

05:46.520 --> 05:52.410
It and then as soon as if it's low, they'll need to precise.

05:52.440 --> 05:54.960
That means that both.

05:59.540 --> 06:05.600
So now we're trying to do is to measure to get the time here and give the time here, and then once

06:05.600 --> 06:12.020
we have the two times we can complete the duration of the polls and from that duration, measure the

06:12.020 --> 06:12.520
distance.

06:12.860 --> 06:15.620
So I'm going to create two new variables here, actually.

06:15.620 --> 06:16.070
Three.

06:17.560 --> 06:24.040
Which are so unsigned, long because in time, Megan.

06:25.770 --> 06:27.670
And let's not initialize them.

06:27.720 --> 06:36.830
We're going to initialize them and set them in the interrupt, unsigned, long spells in time and and

06:36.870 --> 06:45.680
I'm also going to do bull new distance with the label, but this one will be false by default.

06:46.350 --> 06:50.880
So we are going to measure the time between here and here.

06:50.880 --> 06:56.160
When this both stopped and when the police stopped and we get the two values, I'm going to set this

06:56.160 --> 06:56.960
flag to true.

06:57.300 --> 07:00.990
So in the loop, we can read the distance.

07:00.990 --> 07:08.100
And because we are going to modify over time, because we are going to modify those variables inside

07:08.100 --> 07:11.270
an indirect output volatile in front of them.

07:12.270 --> 07:15.420
So both in time being here will be.

07:18.660 --> 07:29.230
Microsoft use Microsoft because it's more convenient and more precise here and then pulls in and is

07:30.600 --> 07:33.150
Microsoft's will and then we need to do new.

07:34.970 --> 07:35.570
Dist..

07:36.590 --> 07:38.300
The label is.

07:40.360 --> 07:43.210
All right, so the Internet is set up.

07:43.250 --> 07:52.870
We have everything we need and now in the loop, what I can do simply is check if you distance available.

07:53.980 --> 07:58.150
If we have a new distance, I can read the distance.

07:58.150 --> 08:06.610
And I'm also going to set the flag to folks so we don't continuously come inside this, if only when

08:06.610 --> 08:09.300
we have a valid direction.

08:09.310 --> 08:11.230
OK, from the Internet.

08:11.680 --> 08:13.950
And then so this step is almost done now.

08:13.960 --> 08:17.500
We are going to create a new function, for example, here.

08:17.950 --> 08:21.580
So double because we're going to return double get.

08:22.580 --> 08:32.960
Ultra sonic distance, for example, and here we need to just compute the duration between those two

08:33.050 --> 08:33.950
times, OK?

08:35.710 --> 08:42.910
So those two are unsigned long, so it will be long, but I'm going directly to put it as double, so

08:42.910 --> 08:46.160
they're not going to make computation which doubles it would be more convenient.

08:46.630 --> 08:55.510
So duration micros is equal to the first in time and minus two percent in time between.

08:56.980 --> 09:09.910
And then double distance is equal to I'm going to duration micros divided by 58 or zero, which is the

09:09.910 --> 09:12.230
number to convert to centimeters.

09:12.520 --> 09:17.950
So in this project, we're going to toggle between centimeters and inches and you could see and the

09:18.160 --> 09:18.700
video.

09:19.120 --> 09:24.870
But actually when we toggle between centimeters and inches, it's only for the display for the user.

09:24.940 --> 09:30.250
OK, for inside the code, I'm going to keep using centimeters, OK?

09:30.580 --> 09:35.470
Using just one unit inside the program will make the program much simpler to handle.

09:35.500 --> 09:42.820
OK, so just one unit and then when we display on the LCD, we can print as many units and just convert

09:42.820 --> 09:44.440
to as many units as we want.

09:44.470 --> 09:48.220
So here I'm sticking to some images and we get the distance.

09:48.870 --> 09:49.930
I can do retune.

09:51.300 --> 09:58.530
Distance, but I'm also going to add some filtering, OK, in case we have a sense of that gives value

09:58.530 --> 10:00.220
that can be quite erotic.

10:00.630 --> 10:01.680
So cute.

10:01.710 --> 10:09.090
I'm going to check if the distance is greater than four hundred, which should not happen, but can

10:09.090 --> 10:14.640
happen, then return the previous distance that we actually need to set.

10:15.480 --> 10:16.740
Yes, I'm going to create.

10:18.410 --> 10:18.950
Double.

10:20.830 --> 10:22.660
Previous dist..

10:23.700 --> 10:28.620
That's initialized to 400 people just to give a number.

10:31.240 --> 10:37.060
So if the distance is greater than the maximum we reach on the previous distance, if not, we can just

10:37.060 --> 10:37.960
see previous.

10:39.090 --> 10:44.850
Distance is equal to the distance we have measured right now, and then before that, I'm going to add

10:44.850 --> 10:47.090
also a complementary filter.

10:47.430 --> 10:48.870
So let's say previous.

10:50.400 --> 10:53.520
Distance with 60 percent.

10:55.600 --> 11:04.600
And the cheering distance with 40 percent, OK, and we read at 60 minutes ago, that should update

11:04.600 --> 11:05.360
quite quickly.

11:05.710 --> 11:06.040
All right.

11:06.050 --> 11:12.310
So again, if you haven't dealt with this, please go back to the ultrasonic section and know that I

11:12.310 --> 11:12.880
have this.

11:13.280 --> 11:16.990
I'm going to be able to call it here double.

11:19.570 --> 11:28.600
Distance is get ultrasonic distance and just to make sure it's working, I'm going to initialize cellular.

11:31.770 --> 11:39.910
And he here print that print Ellen sense, so that will be the dissent in sending it to us for them.

11:40.350 --> 11:40.800
All right.

11:40.800 --> 11:43.550
And let's just take let's verify.

11:44.010 --> 11:50.670
So I'm going to name it final product, step one.

11:52.500 --> 11:59.870
So for each iteration of the project, I'm going to name step one, step two, steps three, etc, save

11:59.880 --> 12:00.120
it.

12:02.660 --> 12:06.350
OK, and now I'm going to upload the coup to the board.

12:08.330 --> 12:16.460
And it's so it's wargaming, it's opening ceremony to see if we have something and yes, so this 140

12:16.460 --> 12:17.540
is the wall behind me.

12:17.540 --> 12:19.640
I'm going to put my hand in front of the SENSEOR.

12:20.030 --> 12:24.910
And I can see, for example, here 13, 13 or 12 centimeters.

12:25.340 --> 12:28.330
So the code is working as expected.

12:28.910 --> 12:34.100
And that is the end of step one, which was something that we already did before.
