This is the number six of the final project, so now that we have initialized the liquid crystal, also
the LCD screen, we are going to use it to print the correct distance measured by the ultrasonic sensor.
So we already have some visual information with the yellow OLED, which is blinking faster if the obstacle
is closer.
And then we have the locked mode.
Whenever the obstacle is getting to close with the LCD, we are going to be able to get more information
and also to be able to give the user some instructions.
For example, when the application is not to say that you have to press the button to unlock the application.
So let's do this.
So I'm going to create a new function that's created here.
Void print distance on NCG.
And we get the distance in centimeters.
OK, again, I'm using centimeters here.
We are going to convert to inches later in the project.
And so here what I want to do, I want to check first if he's locked.
OK, and then it's so I'm going to use a different behavior if the application is locked out, the application
is not locked, OK, if it's locked, I'm going to bring some error message until the application is
unlocked and with some instruction.
And if it's not locked, I'm going to put simply the distance and maybe with a warning we can other
one, OK, we can add an intermediate threshold, for example, 50 centimeters to say that if the obstacle
is at a distance lower and 50 centimeters, we print a warning, OK, before we get to the error state.
So one thing I'm going to do is call that function whenever we get the distance here.
Look here.
So this we continue to read it, OK?
We continue to rule whatever happens, we continue to rate the distance and then they print the distance
the and depending on the is locked, which is already set.
OK, with the lock and unlock function, we already have the mechanism which is working now.
We just need to use that, like to know if the application is locked out.
So let's start with the normal behavior if the application is not locked.
So normal behavior.
What I'm going to do is print on the first line distance and print on the second line.
Some information research.
OK, so acidy set.
Just so I'm going to use the Kyozo with zero and zero to be on the first column, first line.
And in the end, Saudi brains, I'm going to do this, not distance, because we have distance, we
will not have the place to print the distance and the unit.
So just this and the intensity that Prince.
Distance or the distance that we get here in the parameter and function and then acidy, that brains
are going to bring to the unit, which is fono centimeters, we're going to come back to that later
and I'm going to adjust a few spaces, OK, just in case, because the distance here, if we have three
digits or for example, the distance is one hundred forty point zero zero, then we have one, two,
three, four, five, six characters.
But now let's say it's 14 centimeters.
We have.
Only five characters, OK, if it's eight centimeters, we have only four characters.
So with the additional spaces, we make sure that if the new text we print is smaller than the previous
one, we don't keep the previous characters at the end of the line.
OK, so the first line, we bring the distance.
All right.
And then I'm going to LCD set.
Just so we've zero and one to start in the segment and then check, if so, what are you going to do
is I'm going to add a new here we have long distance.
I'm going to say define.
Running distance, which is 50 centimeters, and again, those values, you can just modify them as
you want.
You just need to modify your view here and it will automatically update all your program with this new
value.
So that's very convenient, as you can see, to use to define you.
So one in distance.
I'm going to put that here if the distance so we get the distance in the distance is greater than the
warning distance.
Let's say got print.
No obstacle, OK?
Nothing to report and let's have a few spaces here to reach 16 or more characters and then else, which
means that the distance is lower than the one in distance or the distance here will be lower than 50
centimeters.
And we do Elsley print.
We let's say two exclamation mark in one.
And it's quite a few species here.
And then we don't need to check for the case when the distance is lower than the log threshold, because
if this is the case, we are going to look we're going to log the application here.
And because we love the application, we are going to enter the if is logged on the print distance and
kidney function.
So with the logic, we have said here, we are never going to enter this block of code if the application
is blocked.
So we don't need to check for the log threshold here, just the warning threshold.
OK, so we have the normal behavior first distance in the distance in centimeters and then the message,
no, a warning message.
If we start to get close to the obstacle and then if the application is blocked, what are you going
to do?
So LCD set just saw zero zero to start the first line, first column.
So now every time you call the brain first, you need to set the Kyosuke because you don't necessarily
know where the Kyozo was set the last time you printed something.
OK, it can be anywhere.
And then I'm going to acidy prince.
With three exclamation mark obstacle, and it's the other three and some species.
And LCD does print, so said.
Kyozo, actually.
We zero one and see print, that's the price to unlock a few spaces.
So here we print a big error message, ending price to unlock, to give an indication to the user of
what to do to download the application.
And the thing is that the lock mechanism, we don't need to do anything else.
We just need to add action if it's locked at in action, if it's not locked.
OK, because the lock mechanism is already handled here in the vault.
Look like it's already handled with the lock here and with the lock you.
So now, depending on each state of the application, we just need to say, OK, if this is that state
we do this, if it is that other state, we do that.
And that's quite simple.
So once you have a solid foundation for your program, you can easily add new blocks that add nice functionalities
without having to rewrite the entire mechanism, just using what you have built previously.
OK, I think this is complete.
So let's say let's verify and let's save some final project.
Step number six, save it.
Let's upload the code to the original.
So you will see initializing and then you see first the distance on the first line here, one hundred
forty centimeters and then no obstacle.
So what I'm going to do is first I'm going to.
So I'm need to add something in front of the sensor.
But let's see here at about 30 centimeters, 25.
And you can see one.
So we start to have a warning.
So the here and the yellow and it is blinking faster.
Now we have the warning and the closer I get, I can see now we have the distance.
And when I reach 10 centimeters, now you can see.
So the application is locked and you can see no obstacle press to unlock and can see whatever I do.
If I remove any obstacle, it's still not.
So nothing will change.
Now I need to press on the push button and it goes back to normal.
OK, once again I go closer, closer and the application is locked.
So as you can see, adding an LCD screen to your project is really nice because you can print anything,
you can bring some data you get from the program, you can print some instructions, some warning,
etc. This will give really useful instructions to the user of your application.
And because you use the LCD screen, as you can see, we don't need to use the same monitor anymore.
So from now you could power on your Arduino from any USB power source.
Just put it on the program and just follow along with the instructions and values you see on the LCD
screen.
And that is the end of this step.