This is day two of the final project in this that we are going to make the LSD to the yellow LSD bank,
which we're going to call the warning L.A. and we are going to update the blink rate of this ad with
the distance from the ultrasonic sensor.
So we already get the distance here.
So now let's handle this.
So I'm going to create a new design for the bean.
And instead of naming here little one, two or three am going to meet thing.
Ali.
OK, which is on PIN the 11th for me, this is the yellow alley, but I have my pin, I can initialize
the pin he pin mode.
So this is one thing.
Initialized to about.
Then what I'm going to do anything to create also new variables here.
So let's put some comment, ultrasonic it and let's do here warning.
So I'm also going to use the time functionality to be able to make the edit blink every X amount of
time.
So unsigned, long last time wanting release, linked, for example.
Moelis.
And then unsigned long, let's say, warning added delay.
So previously, I have used time interval as the name of the for the day here.
Here I was dealing with this is not very important as long as you are consistent in a program.
So I use delay.
I use delay.
And if it's reliable and if it makes sense, then that's not appropriate.
And so I need to give a delay.
The delay.
We are going to say it from the distance where we.
But I need to give a first value.
OK, so let's put 500.
OK, I this will be replaced, but just let's put any value as a full body, and then I need also bite
one state and let's say it's low first.
So I can toggle the OK directly, so we have our three vials here now in the loop.
But we never look for nor we read the time we do the first action, which is to trigger the Petrossian
extensively, and then we have check here.
We check for the flight to see if we have a distance.
So I'm going to add a new action here if timed.
So we use the time, not the same one as here minus the last time.
Hear the last time that one blinked.
If this is greater than wanting any delay.
What we do is take the same thing, we add the delay to the last time.
And then we do blink, and it may be totally so I'm going to create a function here and I'm going to
do it here, I'm going to create a function that can be here, void toggle running, anything.
OK, so first I'm going to make it blink and then I'm going to change the brain, right?
OK, one step at a time.
So here I have so I can check if one in any state is equal to hi, I can set it to low.
I can set it to high.
And after setting the stage, after touring the state, I can do digital.
Right, with the warning handed in and warning state, all right, quite done that here.
And now I'm going to show you a small tip here to write this instead of one, two, three, four, five,
six, nine, just use one line, OK?
Because no, I guess you correctly understand what this does so we can try to optimize this with this
code, because six lines just for that is actually quite a lot.
So instead of this, I'm going to first comment this comment lines to toggle the state.
I'm going to do one thing every state is equal to, and then I'm going to open parentheses and check.
This condition.
But interrogation, Mark, so basically, if this is true.
Interrogation, like we are going to set the value low to develop, if this is false, then I put a
column and we said, do you hike?
And then a semicolon and that's it.
And this is doing the exact same thing as these.
So I have not shown you that before because, well, this is clearly an optimization.
And it's best if you first understand how to do it in a simple way.
And then with this week so quick recap about that, when you want to just assign one value or another
value just for two values in one viable, you can test a condition.
So you put the condition here inside parentheses and then interrogation match the value to put in the
variable.
If this is true, Calon the value to put in the variable if this is false.
So this is what we may call a one line if statement.
So as a warning, don't overuse this.
OK, use this only when you have a case like this, when you just want to put one value or another value
inside a variable, depending on one condition, that's it.
So like this now the code is a little bit cleaner and we have a trigger warning entity that I am going
to call.
Right.
All right, I know the LCD will blink at so every five hundred milliseconds, which is what is different
here.
So we have the blinking, but now we need to update the blink rate.
So I'm going to create a new function just below that one void.
Said one ed link right from.
So quite explicit name for the function, a little bit known, but explicit, so we're going to receive
the distance in some images.
So, again, I'm going to use sentimentalizing all the program in inches only when we display for the
user.
So we receive a distance that is between zero and four hundred centimeters.
OK, that's the unit.
And that's the value that we were receiving.
Not what we want to do is to set a blink rate between zero.
And so in the spec, I have given one thousand six hundred milliseconds where you want to do something
that is proportional.
So if the distance is zero, the blink rate is zero.
If the distance is four hundred, the blink rate will be one thousand six hundred.
So basically, as you can see, if you want to go from disarranged to that range, you need to multiply
the value by four, OK, four by four, 60.
So we are simply going to do one thing, any delay.
So this is the delay we have set here.
So we are going to modify this and simply do distance multiplied by four.
So the distance here is a double.
We multiply it by four warning any delay is unsigned long, so it will be cast as unsigned long.
So no problem with that.
And well, that's the only line we need in that function.
So now we need to call that function, of course.
And where are we going to call it?
We're going to call it right now.
So when we get the new distance.
Here we just call the function and we pass the distance as a parameter, I'm going to also commit this
and the code is finished.
So to recap, first, I have initialized and added the one here with the pin.
Even I have created some variables for the time, functionality and to toggle the OK, then I have one
function to toggle the lead.
And you have seen a new way to do the one line statement.
I told you I have a function to set the blink rate from the distance and then so intercepted by initialize
the pin to output and in the loop I just add one action to my pool of actions here, which is to basically
toggle the one entity and I change also to bring.
Right.
So I call the function when I get a new distance.
So as you can see this and this and this.
So the three things are going to be completely independent but are going to work together really nicely.
And what we can do, for example, if we want to check that we have a correct delay, we can do say
that print l.n. with the delay.
So what I encourage you to do as you progress in your project is to use the cell to debug your application
as you progress.
So here I have.
But first, the sale you can see with the distance.
No, I comment the line.
I don't remove it.
OK, I just commented in case I have a problem, I can just increment it and check again.
No, I'm going to do the same thing here.
Check if I have the correct delay and once it's done, I would just comment that line and no, let's
upload the code and let's so let's name it.
Final project step two.
And I'm going to to send money to all sorts of debris at the same time to see what's happening, and
you can see we have a blink rate here.
The blink delay is five hundred forty something, OK?
And if I put my hand closer here, you can see it really going down and the closer I get to the address
to an extent.
So the fastest the will blink.
OK, I go back to it, you can see we blink.
Let's fast I go close, we being fast, I remove my hand and we go back to the previous blink rate.
All right, so that is the end of Stepto.