This is step number three of the final project in this step, we are going to look at the application
when the measured distance is lower than a certain threshold.
So basically what this means is if you put this ultrasonic sensor in a real robot and mobile robot,
for example, if there is an obstacle that is too close, you are going to make the robot stop and you
are going to override the default behavior, but some warnings, et cetera, until the user removes
the obstacle and unlock the application.
OK, so in this step, only the lock functionality and in the next step we are going to unlock the application.
So how to lock the application?
I'm going to create a new boolean here, which is locked and set it to false by default.
So this is a flag we are going to check and to set.
And as you will see, it's quite easy.
If we want to look at the application, we just set it to true.
If we want to unlock, we set it to false and then we can just add in our code at some different places,
Jake.
If it's locked, if it's not locked and do some actions.
I'm going to add also here.
Define.
Distance, OK, and let's say 10 centimeters, the ten point zero, so this is something you can change
on your own application if you want to.
OK, but for my application here, I'm going to use 10 centimeters.
So below 10 centimeters, we lock the application.
So I have my here, here.
And what I can do now is actually the distance.
When I get a new distance, I said so, I said some to break, etc., and then what I can do is check
if distance is lower than not.
Distance, if the distance is lower than the long distance we have defined, then I do is locked.
And actually, what I'm going to do, I'm not going to just lock it here, I'm going to create a function
called lock, so voided lock and in that function and who is locked is true.
And actually, I'm going to do something else here.
I'm going to check first if the application is not locked.
So if exclamation mark is locked and only if it's not locked, I'm going to say it is locked to true
and then call the lock function.
So why use a function here, because, well, later on, you will be able to do more things, OK, and
you can call the log if you want, from different parts of your application.
And then in that function, you can see that first a check if is locked is false first.
So this will enable us to only look at the application when it is not locked at because every time you
but look to true and then you do some actions where you are going to do that many times here.
By doing this, we ensure that we enter this book of code only when we go from the state, not logged
to the state locked.
All right.
So we have a flag that is false, that is first and false.
And then when the distance is lower than long distance, we set it to true.
So the lock mechanism is already here, but now it's not doing anything.
OK, so we need to do some action depending on if it's locked or not locked.
What I'm going to do now is I'm going to say that when the state is locked, we're going to make the
red and yellow and blink together at the same time.
And this will of the right, the default behavior, for example, here, the one energy that is blinking
at different rates will not happen.
It will simply blink along with the right.
So I'm going first to define here.
Defined, so let's call it zero lead in 12.
Let's add here some liabilities for.
OP ed, that's just.
Use the same thing here, so last time your energy blinked and then error added, delay willing to put
that at three hundred.
And in Iraq.
And states, so we have our three variables to use the time, functionality and to be able to toggle
the editing now and going also to add a function here.
So we have toggle one managing to avoid toggle zero and can we create a separate function for that and
the error state.
So this function will toggle the state.
So I'm going to use the one line if we are going to check.
So there are any states, if the state is high interrogation match, we set it to low as we set it to
high and then digital rights with that.
There are.
Naledi.
And there are already state.
And we have the Torgau, there are.
So we were down in the city.
I'm going to do spin mode zero in with about.
It took initialise the pain and now what I'm going to do so now we are going to decide what we do when
the application is locked, what we do when the application is not locked.
Let's check each action we have here.
So first, we trigger that ultrasonic sensor here and we measure the distance.
This webbing to keep it OK.
That is very important.
We're going to keep that.
And now this is the behavior of the one which is going to blink faster if the obstacle is closer.
But we want to override this.
What I'm going to do is if is locked, which we are going to do is make SilverLink.
Errol and.
One reads at the same time, so this would only happen if the flag is locked is true, and then else
as I put this in the.
And let's fix the incantation here.
And actually, let's put this at the top of the loop.
He.
So now in the look, we first check if the flag is locked is true, then we do whatever we need to do
when the application is logged, which would override some of the default behaviours that we put in
the.
So if the application is not logged, we do this.
If it's locked, we do this.
So what I'm going to do here is thank no minus last time you're all linked.
If this is greater than zero editing delay.
Then I'm going to do last time, plus delay.
And I'm going to do toggled.
Hero Eddie and I'm also going to do toggled one Eddie.
So only if it's locked, I took a lead and a warning at the same time.
Another thing is that you still set the warning, bring rates from the distance here.
But this is actually used in that block of code.
A warning allegedly here.
We're going to talk about the warning energy depending on the eral and not the warning, evidently.
So we don't really care about that line.
We can just leave it like this.
So now at 300, so this is three hundred defined here at three hundred milliseconds.
Every three hundred milliseconds, we toggle the error and the warning reads.
You can see the code here for the two functions will do the same thing.
And just what I'm going to do is when we lock the application, so we set the flag to true, I'm going
to do warning state, so I'm going to set the state to low and error state.
So which means that I set the state for the two entities at the same so the same state.
So then when I toggle it with toggle high and high and then low and low at the same time.
OK, because what could happen here is that let's say so you first toggle the energy, so it's high,
low, high, etc. and then you lock the application.
When you lock the application, for example, the right LG is out and the yellow LCD is pulled on,
which means that it's going to blink, but it's going to alternate between the yellow and red, which
is maybe what you want to do.
But in my case, it's not what I want to do.
I want to make the league exactly at the same time.
So I initialize also the state to the same state when I look at the application.
So I think this is good, let's verify you could name it final project step three.
OK, we don't have any compilation, Eros.
I am going to upload.
So you can see here we still have our yellow LCD, which is blinking, and if I put my hand closer to
the end, it is blinking faster and faster and faster until I reach the point where you can see now
the red and yellow are blinking at the same time.
OK, I can do whatever I want.
Now the application is locked.
OK, there is nothing I can do.
So the behavior for the yellow lady here has been replaced.
So of course now we don't have the unlock mechanism.
So if you want to just unlock, you have to restart the application.
I'm going to press the reset button, which will stop the application from start from scratch.
And now I can you can see I can move my hand and if I get closer, I will look at the application begin.
So we have a nice warning, which means that, for example, your robot is going to close from a wall
or an obstacle so you can, for example, stop the robot and then make the two entities blink.
And as you can see, even if you remove the obstacle, even if the measure, the distance is no longer
than before, the application is still locked.
This is a mechanism that is quite useful.
When there is some danger, you lock the application and then you ask for a human manual interaction
with the circuit or the robot to unlock the application.
So the user will need to first check and make sure everything is OK and then press on some button so
the application can continue working as before.