WEBVTT

00:00.150 --> 00:07.800
This is day two of the final project in this that we are going to make the LSD to the yellow LSD bank,

00:07.830 --> 00:14.790
which we're going to call the warning L.A. and we are going to update the blink rate of this ad with

00:14.790 --> 00:17.520
the distance from the ultrasonic sensor.

00:17.550 --> 00:20.670
So we already get the distance here.

00:20.910 --> 00:22.590
So now let's handle this.

00:23.580 --> 00:25.740
So I'm going to create a new design for the bean.

00:26.070 --> 00:32.670
And instead of naming here little one, two or three am going to meet thing.

00:33.810 --> 00:34.260
Ali.

00:35.130 --> 00:42.990
OK, which is on PIN the 11th for me, this is the yellow alley, but I have my pin, I can initialize

00:42.990 --> 00:45.000
the pin he pin mode.

00:46.960 --> 00:51.070
So this is one thing.

00:52.510 --> 00:54.500
Initialized to about.

00:56.620 --> 01:00.700
Then what I'm going to do anything to create also new variables here.

01:00.910 --> 01:09.900
So let's put some comment, ultrasonic it and let's do here warning.

01:11.020 --> 01:17.800
So I'm also going to use the time functionality to be able to make the edit blink every X amount of

01:17.800 --> 01:18.160
time.

01:19.160 --> 01:28.400
So unsigned, long last time wanting release, linked, for example.

01:30.600 --> 01:31.190
Moelis.

01:32.680 --> 01:39.790
And then unsigned long, let's say, warning added delay.

01:41.580 --> 01:48.120
So previously, I have used time interval as the name of the for the day here.

01:48.230 --> 01:54.450
Here I was dealing with this is not very important as long as you are consistent in a program.

01:54.480 --> 01:55.790
So I use delay.

01:56.040 --> 01:57.380
I use delay.

01:57.600 --> 02:01.730
And if it's reliable and if it makes sense, then that's not appropriate.

02:02.400 --> 02:04.770
And so I need to give a delay.

02:04.770 --> 02:05.180
The delay.

02:05.190 --> 02:07.660
We are going to say it from the distance where we.

02:07.740 --> 02:09.220
But I need to give a first value.

02:09.240 --> 02:11.070
OK, so let's put 500.

02:12.240 --> 02:21.120
OK, I this will be replaced, but just let's put any value as a full body, and then I need also bite

02:22.290 --> 02:27.030
one state and let's say it's low first.

02:27.940 --> 02:37.020
So I can toggle the OK directly, so we have our three vials here now in the loop.

02:38.600 --> 02:43.840
But we never look for nor we read the time we do the first action, which is to trigger the Petrossian

02:43.850 --> 02:46.130
extensively, and then we have check here.

02:46.400 --> 02:49.190
We check for the flight to see if we have a distance.

02:49.200 --> 02:52.690
So I'm going to add a new action here if timed.

02:53.090 --> 02:58.970
So we use the time, not the same one as here minus the last time.

02:59.840 --> 03:05.120
Hear the last time that one blinked.

03:08.920 --> 03:13.210
If this is greater than wanting any delay.

03:14.460 --> 03:19.740
What we do is take the same thing, we add the delay to the last time.

03:21.810 --> 03:31.170
And then we do blink, and it may be totally so I'm going to create a function here and I'm going to

03:31.170 --> 03:39.030
do it here, I'm going to create a function that can be here, void toggle running, anything.

03:41.870 --> 03:46.730
OK, so first I'm going to make it blink and then I'm going to change the brain, right?

03:46.780 --> 03:48.690
OK, one step at a time.

03:49.220 --> 04:01.310
So here I have so I can check if one in any state is equal to hi, I can set it to low.

04:06.510 --> 04:08.880
I can set it to high.

04:10.590 --> 04:16.200
And after setting the stage, after touring the state, I can do digital.

04:17.470 --> 04:28.100
Right, with the warning handed in and warning state, all right, quite done that here.

04:28.420 --> 04:34.780
And now I'm going to show you a small tip here to write this instead of one, two, three, four, five,

04:34.780 --> 04:37.300
six, nine, just use one line, OK?

04:37.600 --> 04:43.900
Because no, I guess you correctly understand what this does so we can try to optimize this with this

04:43.900 --> 04:48.640
code, because six lines just for that is actually quite a lot.

04:48.970 --> 04:55.720
So instead of this, I'm going to first comment this comment lines to toggle the state.

04:55.720 --> 05:04.420
I'm going to do one thing every state is equal to, and then I'm going to open parentheses and check.

05:05.630 --> 05:06.590
This condition.

05:08.460 --> 05:14.130
But interrogation, Mark, so basically, if this is true.

05:15.220 --> 05:23.290
Interrogation, like we are going to set the value low to develop, if this is false, then I put a

05:23.290 --> 05:25.690
column and we said, do you hike?

05:26.080 --> 05:28.140
And then a semicolon and that's it.

05:28.270 --> 05:31.860
And this is doing the exact same thing as these.

05:32.260 --> 05:37.300
So I have not shown you that before because, well, this is clearly an optimization.

05:37.300 --> 05:41.200
And it's best if you first understand how to do it in a simple way.

05:41.200 --> 05:49.780
And then with this week so quick recap about that, when you want to just assign one value or another

05:49.780 --> 05:54.420
value just for two values in one viable, you can test a condition.

05:54.440 --> 06:01.690
So you put the condition here inside parentheses and then interrogation match the value to put in the

06:01.690 --> 06:02.220
variable.

06:02.230 --> 06:07.780
If this is true, Calon the value to put in the variable if this is false.

06:08.830 --> 06:14.000
So this is what we may call a one line if statement.

06:14.470 --> 06:16.690
So as a warning, don't overuse this.

06:16.690 --> 06:22.540
OK, use this only when you have a case like this, when you just want to put one value or another value

06:22.540 --> 06:25.420
inside a variable, depending on one condition, that's it.

06:26.410 --> 06:33.550
So like this now the code is a little bit cleaner and we have a trigger warning entity that I am going

06:33.550 --> 06:34.090
to call.

06:35.510 --> 06:35.790
Right.

06:38.240 --> 06:45.140
All right, I know the LCD will blink at so every five hundred milliseconds, which is what is different

06:45.140 --> 06:45.420
here.

06:46.430 --> 06:50.380
So we have the blinking, but now we need to update the blink rate.

06:50.390 --> 06:54.200
So I'm going to create a new function just below that one void.

06:55.560 --> 07:01.500
Said one ed link right from.

07:08.100 --> 07:15.180
So quite explicit name for the function, a little bit known, but explicit, so we're going to receive

07:15.180 --> 07:17.920
the distance in some images.

07:18.120 --> 07:23.190
So, again, I'm going to use sentimentalizing all the program in inches only when we display for the

07:23.190 --> 07:23.560
user.

07:23.820 --> 07:31.590
So we receive a distance that is between zero and four hundred centimeters.

07:31.620 --> 07:32.730
OK, that's the unit.

07:32.730 --> 07:35.080
And that's the value that we were receiving.

07:35.460 --> 07:40.620
Not what we want to do is to set a blink rate between zero.

07:40.830 --> 07:49.050
And so in the spec, I have given one thousand six hundred milliseconds where you want to do something

07:49.050 --> 07:50.040
that is proportional.

07:50.070 --> 07:54.270
So if the distance is zero, the blink rate is zero.

07:54.270 --> 07:59.890
If the distance is four hundred, the blink rate will be one thousand six hundred.

08:00.690 --> 08:06.480
So basically, as you can see, if you want to go from disarranged to that range, you need to multiply

08:06.480 --> 08:11.510
the value by four, OK, four by four, 60.

08:11.970 --> 08:17.110
So we are simply going to do one thing, any delay.

08:17.580 --> 08:20.930
So this is the delay we have set here.

08:21.270 --> 08:28.490
So we are going to modify this and simply do distance multiplied by four.

08:29.010 --> 08:30.690
So the distance here is a double.

08:30.720 --> 08:37.810
We multiply it by four warning any delay is unsigned long, so it will be cast as unsigned long.

08:38.250 --> 08:40.100
So no problem with that.

08:41.040 --> 08:44.070
And well, that's the only line we need in that function.

08:44.460 --> 08:46.620
So now we need to call that function, of course.

08:46.630 --> 08:48.450
And where are we going to call it?

08:48.460 --> 08:49.740
We're going to call it right now.

08:49.770 --> 08:51.630
So when we get the new distance.

08:52.830 --> 09:00.300
Here we just call the function and we pass the distance as a parameter, I'm going to also commit this

09:00.900 --> 09:02.590
and the code is finished.

09:02.610 --> 09:09.150
So to recap, first, I have initialized and added the one here with the pin.

09:09.790 --> 09:20.460
Even I have created some variables for the time, functionality and to toggle the OK, then I have one

09:20.460 --> 09:22.260
function to toggle the lead.

09:22.260 --> 09:26.480
And you have seen a new way to do the one line statement.

09:26.900 --> 09:33.810
I told you I have a function to set the blink rate from the distance and then so intercepted by initialize

09:33.810 --> 09:43.230
the pin to output and in the loop I just add one action to my pool of actions here, which is to basically

09:43.230 --> 09:47.040
toggle the one entity and I change also to bring.

09:47.040 --> 09:47.410
Right.

09:47.430 --> 09:50.100
So I call the function when I get a new distance.

09:50.670 --> 09:52.800
So as you can see this and this and this.

09:52.800 --> 09:59.690
So the three things are going to be completely independent but are going to work together really nicely.

10:00.000 --> 10:05.220
And what we can do, for example, if we want to check that we have a correct delay, we can do say

10:05.360 --> 10:08.290
that print l.n. with the delay.

10:08.520 --> 10:17.730
So what I encourage you to do as you progress in your project is to use the cell to debug your application

10:18.030 --> 10:18.920
as you progress.

10:19.350 --> 10:20.150
So here I have.

10:20.160 --> 10:22.730
But first, the sale you can see with the distance.

10:22.740 --> 10:23.970
No, I comment the line.

10:23.970 --> 10:24.860
I don't remove it.

10:24.870 --> 10:30.270
OK, I just commented in case I have a problem, I can just increment it and check again.

10:30.540 --> 10:32.240
No, I'm going to do the same thing here.

10:32.250 --> 10:38.940
Check if I have the correct delay and once it's done, I would just comment that line and no, let's

10:38.950 --> 10:43.080
upload the code and let's so let's name it.

10:43.830 --> 10:45.990
Final project step two.

10:51.000 --> 10:55.500
And I'm going to to send money to all sorts of debris at the same time to see what's happening, and

10:55.500 --> 10:58.800
you can see we have a blink rate here.

10:59.100 --> 11:02.650
The blink delay is five hundred forty something, OK?

11:02.800 --> 11:11.310
And if I put my hand closer here, you can see it really going down and the closer I get to the address

11:11.310 --> 11:11.760
to an extent.

11:11.790 --> 11:15.450
So the fastest the will blink.

11:15.480 --> 11:19.860
OK, I go back to it, you can see we blink.

11:21.060 --> 11:30.120
Let's fast I go close, we being fast, I remove my hand and we go back to the previous blink rate.

11:33.110 --> 11:35.490
All right, so that is the end of Stepto.
