This is now step number four of the final project in the state, we are going to unlock the applications.
So far what we have done is read the distance from the ultrasonic sensor, both on the warning and the
faster is the obstacle is closer and then lock the application.
If the distance is lower than the certain threshold here, 10 centimeters in that case, we of the right
in the default behavior and we make the IDs.
So the one and the error and it is blinking and not with the things that once the application is locked,
we can't do anything else.
So we know what we need to do is create a mechanism to unlock the application when a user, for example,
a human user, presses on a push button.
So in real life, that could be that you're about to stoke somewhere.
The user would make sure the robot is not stacked anymore.
And we press on a button to say that now everything is OK.
You can resume your normal behavior.
So let's handle the push button, because this is when we press on the button that we are going to unlock
the application.
So I need for Dippin.
The fine button in which he's been number two, and he will for the president and we are going to need
to debate the button, which we're going to do in the code, and we can choose between using polling
or interrupts.
And actually, I'm not going to interrupt in that final project for a button.
I'm going to interrupt for the address and make OK.
This is already perfect for this budget, for the budget.
And I'm going to use Pawling and check the burden states only when the application is blocked.
So the thing is that in that case, using polling will not be a problem because we will never be stuck
somewhere in the void loop.
So we are not going to miss the data from the button.
So I have my button pinned here.
I'm going to say I have made available for ultrasonics one error and I'm going to add button here as
button.
And the unsigned long last time.
But then changed, so this will not be used to trigger the button and do something every X amount of
time.
This is just for the debates.
OK, we check when the last time the button has changed so we can make sure that when the button changes,
we ignore the following few changes for a few milliseconds so we don't count all the physical bounces
that happen on the physical button.
Unsigned, long, but demands delay, which is.
Let's put 50 milliseconds and bytes by the state we need to monitor the states.
Actually, I'm not going to initiate it here.
Now, I am going to go in the void setup and I'm going to do clean mode.
Button being input and just after that, so button state.
Is equal to Digital three Budden.
So instead of initializing the state to an arbitrary state, let's just read the real state for the
bottom.
OK, and then so Lebanon is set up.
We have all the variables we need in the void loop.
What I'm going to do is so if is locked, we make the edit is being and I'm going to do so at the same
time.
I'm going to check for the push button here.
So when the application is not locked, well, we don't check for the pushbutton because the pushbutton
is only here to unlock the application.
So no need to check for it any time.
We're just going to check the state when the application is looked.
So to read the state, which is digital, read for the button.
But I'm also going to write the DBMS mechanism.
So time now if time minus last time button changed, if this is greater than the button demands delay,
then what we're going to do is do so.
New button state.
Is equal to digital.
We with the.
So if the state doesn't change, we continue to read it at full speed and now if the Golden State is
different.
Like the new Golden State is different from the bottom state, we have stalled what we are going to
do is last time.
Change is equal to time now and button that is equal to a mute button state and then do the action we
want.
So this again, things you don't understand this structure.
Please go back to the demands section.
And now we are going to unlock the application so the button we can press on the button and we can release
the button.
So I actually am going to check if Button State is low.
So here I decide to do the action when we release the book.
OK, and because I have created a lock function, I'm going to create analog function and let's so we
have largely to avoid a ADNOC.
And let's right the mechanism to unlock the application, so here, when I looked a first check, if
it was not locked.
OK, so I'm going to do the same here.
I'm going to do but the opposite, actually, if it is blocked, then I can unlock it.
OK, so I just had this small condition to make sure that we don't unlock the application when it's
already unlocked.
And then I'm going to do is locked is false.
And note that I have set the flag to false.
The thing is that the volume will continue to grow.
OK.
And now instead of going inside this, if he's locked, we are going to go with the X, so the red energy
will not blink anymore.
We are not going to take the button state anymore.
Instead, we are going to go back to normal behavior and make the yellow and blink with the blink rate
depending on the distance.
And we also continue to read the distance.
So now that the application is unlocked, we read the distance and again we check if the distance is
lower than the long distance, then we lock the application begin.
So now we would be able to lock the application and lock the application, lock the application, etc..
And one thing here is that in the end, look, what I'm going to do is also do hero.
Editing state to low and digital rights are a 15 year old data states and also apply the state to the
area at Dippin simply because here, if we stop targeting the right any means that maybe we are going
to stop when the boiled off, but maybe we are going to stop when the ADHD bobbled on.
So what may happen when you unlock the application is that the right ending will be put on and you don't
want that.
You want it to be pulled off.
So we make sure it's of when we unlock the application.
Actually, here there is a saying we to comment because we don't need that anymore.
Now, if you want to test, I'm going to test with the real secret and we are going to see if it's working
simply with the behavior of the it is.
But what you could also do is here I'm just going to say a little print and I'm looking, for example,
and do.
You and.
Looking OK, so you can also when you get to that function here, when you get to that function here
with the cell monitor, so you make sure you don't enter this function when you don't need to.
So that's an additional week to bring your program if you need to.
All right.
To recap quickly here and bring us to their final call.
OK, so final project, step number four.
Let's see if it complies, yes.
So to recap quickly on the code I have, so initialize the button.
Thien initialized some variable for the deboned functionality of the button and then so we keep those
functions.
I have created the unlooked function, which shak so if it's already locked, we unlock the application
and we pull out of the editing by setting the building in the setup and in the loop, I check the button
state.
And basically when the button is worthy's here, let's seek release when the button is released and
also when the state is locked, then we call the analog function.
Now we have a mechanism to lock and unlock the application and now let's blow the code to the original.
So I still have my yellow calendar, which is blinking, and if I go closer here, the lady will blink
faster and here I have reached the point where so the 10 centimeters where the application will be looked,
you can see now the two entities are blinking together.
And now I have a way to unlock the application.
I'm going to press on the button and you can see it's going back to not not if I look again.
OK.
And I can unlock it and is going back to normal so I can alternate between the two states.
All right, so now you have already completed the main functionality of this project, which is to lock
and unlock the application depending on the measured distance.
Now, what we are going to do in the next few steps is to improve all the other functionality around
that main functionality to make the project nicer with more information that we can get and with more
interactiveness.