Let's first understand what the problem is for that, they will simply start with an example where you
will clearly see the button bouncing.
So what we're going to do is to try to monitor the push-Button, but not just when it's high or when
it's low equity.
Previously, we are going to monitor the push-Button so we can detect a change of state.
This is the most common use case for anybody when you detect a change of state, which means that,
for example, the user presses the button so the state will go from low to high in the circuit.
We have all that.
You simply release the button and the state will go from high to low.
And this is this exact change of state that we want to monitor, not just all the time.
The button is high all the time.
The button is low.
Well, let's make a simple example to monitor the change of the state of the button and print it on
the side of the monitor.
So first, I'm going to initialize the define button in two.
OK, so as usual.
Because we are going to use Cyril, I'm going to say begin.
It with these moderate and then.
In mode, written in.
Input.
So nothing new here.
And then in the loop function, what I'm going to do is I'm going to read digital read button thing.
I'm going to read the stage from the button.
Well, and if we want to be able to monitor the difference, we need to stop because the previous one.
OK, how can you know that the previous volume was high and now it's low?
Well, because you stored the prejudice that you had.
So we are going to store it as a global valuable byte.
But in states, it's initially too low that we use bait here for the state of the button because it's
just low and high.
So this is maybe a little bit better, but you could also use it without any problem.
OK, that's not a game changer thing.
If we want the same, whether you use it or just like we did previously in some previous activities
and as a quick recap byte is of value from zero to two hundred fifty five and an integer contains two
bytes and with two bytes you can go from minus thirty two thousand to about thirty two thousand.
So now we have our reading of the button.
I'm going to save it in a new variable kinetic creating the void loop.
So let's say new button state.
OK, so we have the new Golden State and we have the, let's say, the Buuren state, which is also
the producer.
OK, so we're in the neighboring state.
And then what we are going to do, we are simply going to check if the new Budden date is different
than the one state.
And if that is the case, we are going to see that we are going to action.
And angry also just before so the other head of the mirror that I'm going to do Button St..
Is equal to New Bernstine.
So every time we enter the loop, we check the balance.
It's low first, then so it's low and then we press the button and the time that we press the button
at this exact moment, this will give high.
OK, we check the new Golden State is different from the previous button state because now it's high
and this is low.
So we enter this executive action and we store, of course, the brain state as the new one.
OK, so the next time the value is high, we don't come back in this if again and we only come back
in the if if the person is no low.
So when we release the button again, what is the action we want to do?
We want to print when the button was pressed aren't released.
So here in this, if we know that the state of the button has just changed, so we know it's either
pressed or either released now to make the difference, to go further, we can just check.
So no button state is equal to new warranty, so we can just check any viable we can just check if the
current state is high, which means that the bottom was priced.
Cellulose print, Ellen Burton, Riced, and then we can use El's, so basically the burns steak is
not high, which means that it is low because there are only two states in the label, Taylor Green
Capellan.
Release.
Can we do this, we are going to have the exact moment when we press the button and when we release
the button, so quick recap, we stole the previous state, our current state of the button here in
a global Bible, then in the loop.
Every time we read the new state, we compare the state with the previous one.
If this is different, we know that something happened, that we, of course, stole the previous date
or the next time we run the loop and then we do the action so we know something has changed.
We just need to check the brain state here.
If it's high, it means that we went from low to high.
So we press the button.
If it's low, it means we went from high to low.
So the bottom was released.
And now I'm going to upload that code.
I'm going to save it as a button.
Bounce.
OK, Don, imploding and in to open the sale monitor there, and I'm going to click here on show time
stamp.
OK, so whenever we have something printed, we say in MONITEAU, we will also get the time stamp to
help us get the exact time of the change of date.
So I'm going to press the button several times.
So I just press under and you can see we already have a first example of the buttons.
So what did I do?
I just pressed once and realized once you can see the time is really almost the same and I don't have
just button pressed and button released once I have been embraced and button released twice.
OK, let's make another example.
So here's the mouse.
I press the button.
I realized I press the button.
I really do it.
Now let's go.
Let's do a press and release quite quickly.
OK, it works.
We have two another one.
We have to another one.
Another one.
Let's clear the output and let's try to get those.
OK, and you can see here after several tries, so the usual behavior is that we press the button,
we have been pressed, we release the button, we have button that case.
You can see I press the button and then when so at this time here and then when they release the button,
you can see at this exact time we also have pressed and released, which means that the Audi, you know,
has detected that the button was pressed and released one more time can sometimes can be one more time,
two more time, three more times, etc..
So this can be quite inconvenient because let's say you want to toggle and edit, which is something
we are going to do later.
Also discuss.
Let's say you want to toggle the algae only when you release the push-Button.
So we this normal behavior, OK, you press and you release.
So you are going to toggle the energy.
For example, if it was put on, you pull it off.
If you are going to now put it on OK with this one, you are going to pull right off and then you press
again and what happens.
So the end is pulled off, you press and when you release here, the energy will be pulled on.
But with these two additional dates that are monitored, the energy will return to the state both.
Which means that, well, that's not what you wanted to do with your program.
So this is actually the bond problem that you can clearly see with this example.
And what's happening here actually will in the next lesson, you will get the complete explanation.