Let's now solve the bonus problem still with this code example.
We're going to keep the exact same functionality, OK, which is to print on the same monitor when the
buttons pressed and when the button is released.
But in this case, we want to avoid multiple detections of this of this when we just press release the
button once the what we are going to do is simply when we see that the state has changed, we are going
to ignore the following states of the button for a few milliseconds.
And as you will see, the code will be actually very similar to what we did when we wanted to avoid
using the delay for an entity or for any action you want to do in parallel.
OK, not exactly the same, but very similar.
So what I'm going to do first here, I'm going to create two global viral, unsigned, long last time
button changed and initialized to me.
So with this, we are going to keep the time and simply when we change.
So when we monitor that the button state has changed, we are going to keep a record of that.
So we know when was the last time the button changed so we can wait and see how long the bounce delay
so we can wait for the DBMS delay before reading again.
And I'm going to use fifty milliseconds, OK, and the default value, we are going to try with this
first.
So we have to violence here for the time we have our button state in which we still need to keep then
in the setup.
No need to change anything and not in the loop.
What I'm going to do, I'm going to first and long time.
I'm going to get the time.
The current stank, so every time we go in the loop, I'm going to get the quarantine, OK?
And this quarantine will, of course, increase every time I go in the look and I'm going to check if.
Time now, minus the last time the burden changed.
If this is greater than the deboned delay.
Then we can enter the if.
And then what we can do is we can read I'm going to put that in if we can read the brain state and then
check if the button I'm going to please, Jake, if the button state is different from the previous
one.
OK, we're going to do an action.
And two important things here.
We are going to update you.
The last time button changed.
I'm going to write it and then explain why.
So last.
Time, but changed is equal to time, no, and then one state is equal to New York State and here do
the action, whatever action we want to do.
So what's happening here?
The first the last time button change will be initialized to zero when we until we check the current
time.
OK, if the current time minus the last time to and change.
So the duration between the time now and when we change the button, if this duration is greater than
the duration we have set for the DBMS, delay we can enter is the look of now we are going to read the
new button state and either state is still the same as before.
So if it was low and no, it's too low, then we are not going to enter this if.
OK, so we go out and the next time we enter the loop we can still go in the if, OK, because we have
not updated the last time.
But it changed, you know, like we did for the energy previously.
We don't want to make sure that we execute this only every 50 milliseconds or every whatever time we
can rig this as fast as we want is just when the button will change, when the new button state is different
from the previous one.
In that case, we are going to update this variable with the time.
No, OK, and this is different from before.
Before what we did is we simply added the delay here.
Here what we do is we update the last time button change with time.
No.
OK, because this is the time when the button state has changed.
So we just record the exact time when the button state has changed.
And now when we go back in the loop time now minus last time button changed will not be greater and
dbms zedi.
So here we will only be able to go back in that if only if we have spent at least 50 milliseconds after
we have ADT this valuable.
So after the state of the button has changed, basically when we change the state of the button, we
don't go here and we ignore this block for fifty milliseconds, which means that any bounce that will
happen physically will still happen physically, but we are not going to read that bounce.
OK, so now here we know that we can do the action and the action is simply the same as before.
I'm going to copy this here and select and press double.
And just remove that.
All right, so now when we enter this.
We can be pretty sure that this is not the bounce, OK?
This is the real change of state.
I'm going to upload the program.
OK, and open the sale monitor, and now I'm going to press the button, I'm going to read it, but
I'm OK and well, you can try as many times as you want.
You will not get a price and release more than one time when you just press and release one thing,
OK?
And we have sold the bones, probably.
Now let's modify these DBMS dillehay.
Let's say, but zero, if I put zero, it means that just this structures is useless, OK?
Because every time we are going to enter this, if we are not going to wait any time after the state
has changed, OK, let's try with zero and let's see that we can have when talk.
I'm going to press.
OK, and you can see here we have the dance issue.
OK, we don't press the button, really.
You can see about the same time we have one moment in person, but unreleased.
Now, I go back to my program and put correct values so you can but what I'm going to put 30 milliseconds
that will work from my second.
The thing is how to actually know which value you can sit here.
So 50 or 30 milliseconds is already quite a good value.
It should work.
You can try different values.
So the tradeoff here is simply if you put a value to low, well, you might still have bonuses.
So let's say you put only three milliseconds.
You might still see Bonsey.
So you want to put a value high enough, but not too high, because if it's too high, well, this means
the delay means that you're going to ignore the state for the button for a few milliseconds, OK?
So if I just say, OK, I'm going to get 1000.
The thing is that if I press release the button in less than one second, I'm going to meet also the
real change of state when I release the button.
So you want the value to be high enough so it doesn't get any bounce in it.
But you also want it to be not too high.
So you don't miss real change of state.
All right.
I know you can see Destructo here we went.
We could have done in the program here after we read the button and we checked that.
But instead from here, we could just dump a delay, for example, delete.
Fifty milliseconds and just not use all of the structure.
This would have been the same for this program, but then, as you know, with the delay, the problem
is that we are going to stop the execution here.
So if we want to do anything else other than this, well, that would be a problem with Destructo.
We can go very, very fast.
OK, you can see it.
We read the time and then we have if.
OK, so either we enter the if or not and then the actions will be very fast to execute.
So if we have another action here, then this will not be a problem for the action to.
All right, and the thing I want to emphasize is that you are going to be the last time the burden changed
after the State of Union has changed, not just when you read it, but after you read it and that you
see that the Golden State has changed.
Only then you will have the desirable and updated with the time now.