This is the solution of the previous activity where you need to make three, it is being in a certain
way and then change the current state.
And if the president is pressed, you just stop the sequence and then you resume when the push button
is not pressed anymore.
So I'm going to first initialize all the things here.
We've defined the one in which is number 12.
And the two in number 11.
And being which is number 10 and then we have we are going to use the push button to button.
Dean, which is on digital being number two.
OK, so we have our full.
Things right here, so not in the set of what we can do is initialise we've been moved, so I'm going
to initialize the Berlanti first.
We have an input Motoki, because we are going to read from that thing because this is essential and
then the buying mode for the one in which is output and I'm going to do the same.
For eighty two, end the.
Treat it as well as initialise.
State property one below.
And then.
Two and three.
But we have our city.
OK, so nothing really new here and now in the ocean.
I'm going to remove all that.
I'm going to make the first state of the blink.
OK, so digital rights, any one in will be high, OK?
The.
So the energy to will be low and then the.
Digital right, Eighty-three?
Will be high, so we have high, low and high.
And then so this is a typical link, we use a delay of three hundred milliseconds and then we can simply
say, I'm going to just.
This isn't just in this state low.
I.
And and then, of course, we need to wait another three hundred.
Milliseconds, OK, because the program will simply go from there to the beginning of the loop.
OK, but if we don't use the delay this we can CDC to directly go back to the state.
All right.
So the brink is finished.
That is the first step of this mini project.
But now how are we going to stop that from the president?
Well, we would need, of course, to use the digital read function we've done.
So that is for sure.
So we need to use that to get the state of the button.
And now, because we want to check if the push button is pressed, then we are going to use and if.
If digital read button thing is equal with two equal signs, they're important, too.
I think if the president is priced.
Then what we are going to do?
Is, well, simply nothing.
OK, and it's.
Let's put that here in the end.
So if you copy and paste in a block, the indentation may be wrong, so you just fix it.
So, for example, I select all of that and I press dub once and it automatically adds to notation,
which is much nicer to read.
But what we do here in the program is we check everything into the loop.
We check if the button is pressed, if we do nothing.
And if the button is not pressed, we execute the.
And the first thing you may have noticed here is that instead of doing if this condition is true, we
do nothing else.
We do something we can directly check.
If the growth is low and then we simply do the blink.
And well, the reason is because we don't do anything else.
So here we are only going to enter the look of code and we are only going to make the editors blink
when the push button is not pressed because so whenever the push button is priced, we simply don't
enter this book of code.
And then you might see another problem is that so if the pushbutton is not pressed, which we are going
to do is to we enter that.
If so, we make the error.
It is so high, low, high, and then the delete 300 segments and then it is low, high, low delay
300 seconds.
And then and only then we check the button again.
So you will not be able to pull the program here between those two states.
You will only be able to push the program after this date.
So after we go back here, but not at this point, what are we can do here?
Well, first, the maybe most obvious solution is we can split this into two.
If so, I'm just going to finish the F here and add another, which is the exact same.
It right here.
So first we check.
And actually, let's put a delay here.
But we have two blocks of code that are saying, OK, but just with a different state, OK, for the
bridge, the first we check, if the bottom is not priced, if the button is not pressed, then we just
execute this eight of the.
We wait for three hundred milliseconds and then we check again the button.
So if let's say we executed this, but then we press the button, then this will not be executed.
And then we come back and we continue to check it.
But this is not a good solution because, well, you need to write the same structure twice.
And then depending on when you press the push button, you may execute the same structure twice.
So let's say you execute that one, OK?
And you in this three hundred milliseconds, you press the button.
So this will be false.
And so we don't enter this book of code and then you continue to check at full speed.
And then when you release the button, if this is the first line that gets executed, you are just going
to execute that a second time instead of just going to the next one.
So what I'm going to do is simply to use just one if but I'm going to check the state of the brain inside.
If so, we are going to use a variable here that I'm going to make it global so we can create it here.
We can use it in the loop.
And because it is in a more global scope, the value will stay in the book.
It just creates a viable in the loop.
Every time we go out of the loop, the variable will be gone and then we can't keep the volume.
So let's name it any linked states and just going to initialize it to let's say one.
OK, let's say we have the state one and the state to.
Let's just focus on that first issue, which will become the only if if the button is not pressed so
we don't change that, then we are going to check if every state is equal to one.
So that was, let's say the current state was one, what we do is we do think states is equal to two.
So here we check.
The value with to equal, so this will simply compared to value and return true, if this is correct,
this is equal and here we assign a new value with just one equal to the state.
So let's say that we are going to execute that first year.
OK.
So, Ellie, dimpling state one, we execute this and then we change the variable.
To do so the next time we enter the look and the if, then the eligible state will not be equal to one,
but to do so, we can use else and he could write as a state is equal to two.
But because there is only two states we can just use as.
And going to right now.
This year.
So the second state is the first state to second state and now I'm going to put any link, eight is
equal to one.
OK, and just remove all that and what I'm going to keep is the delay that I'm going to write the delay
only once.
And actually, we are missing a three bracket.
OK, so make sure you have all the brackets.
This is also a common error, is that you are missing a bracket and then you may have some errors that
we have.
The brackets here, you can see with the look and then the fist if they get them.
This if from here to there and then else and then let's end the delay here is not inside this.
This blog is inside the digital void.
But look.
So this court is, as you can see, much better because you will be able to pull the program between
each state, OK, if you want, you can add other states, you can add more states easily.
You just need to write delete one time.
And this well, this is your first state mission in hardware for women.
In Hunwick project, you would often see state machines.
So basically you have available, which contains a state, OK, and then you check if the state is that
particular state, you do one action and then you change the state to another one.
The the next time you can tell if the state is different and you execute another action and every time
you executive action, you change the state.
So you change the action for the next time.
OK, so to recap in the loop function, first we check for the state of the button.
If the button is priced well, this is false or we don't do anything.
If the button is not priced, we are going to execute the blink with either one of the two states,
OK?
And once we execute the state, then the next time it would be the other state.
And after we execute the blink, we simply use the delay 300 to wait before we check the next time.
And let's actually make this program combined here.
I'm going to name it as.
Activity.
So let's check if we have any compañero and down company, so that seems to be good and now we're going
to upload to connect your make sure it's connected, et cetera, upload code.
And you can see the editors are blinking.
So first one and three and then edit it to every three hundred milliseconds.
So now is the text I'm going to press on the button.
And as you can see here, the blink has stopped.
I realize the president and the brain continues like.
As the president, you can see now, it's already one and three it.
Let's try to get you to win three again.
And we had two so we can pull between each state of the black.
All right, that's the end of this activity.