WEBVTT

00:00.180 --> 00:05.860
Let's now solve the bonus problem still with this code example.

00:06.180 --> 00:10.860
We're going to keep the exact same functionality, OK, which is to print on the same monitor when the

00:10.860 --> 00:12.830
buttons pressed and when the button is released.

00:13.230 --> 00:20.790
But in this case, we want to avoid multiple detections of this of this when we just press release the

00:20.790 --> 00:26.460
button once the what we are going to do is simply when we see that the state has changed, we are going

00:26.460 --> 00:31.500
to ignore the following states of the button for a few milliseconds.

00:31.740 --> 00:38.520
And as you will see, the code will be actually very similar to what we did when we wanted to avoid

00:38.520 --> 00:43.020
using the delay for an entity or for any action you want to do in parallel.

00:43.080 --> 00:45.940
OK, not exactly the same, but very similar.

00:46.980 --> 00:56.430
So what I'm going to do first here, I'm going to create two global viral, unsigned, long last time

00:57.300 --> 01:01.540
button changed and initialized to me.

01:01.720 --> 01:05.810
So with this, we are going to keep the time and simply when we change.

01:06.180 --> 01:12.720
So when we monitor that the button state has changed, we are going to keep a record of that.

01:13.430 --> 01:22.070
So we know when was the last time the button changed so we can wait and see how long the bounce delay

01:22.090 --> 01:25.530
so we can wait for the DBMS delay before reading again.

01:26.100 --> 01:32.580
And I'm going to use fifty milliseconds, OK, and the default value, we are going to try with this

01:32.580 --> 01:33.030
first.

01:33.480 --> 01:41.340
So we have to violence here for the time we have our button state in which we still need to keep then

01:41.340 --> 01:42.150
in the setup.

01:42.150 --> 01:44.900
No need to change anything and not in the loop.

01:45.600 --> 01:52.350
What I'm going to do, I'm going to first and long time.

01:52.830 --> 01:54.300
I'm going to get the time.

01:56.060 --> 02:00.870
The current stank, so every time we go in the loop, I'm going to get the quarantine, OK?

02:01.440 --> 02:07.560
And this quarantine will, of course, increase every time I go in the look and I'm going to check if.

02:08.960 --> 02:14.390
Time now, minus the last time the burden changed.

02:15.200 --> 02:17.750
If this is greater than the deboned delay.

02:19.290 --> 02:22.350
Then we can enter the if.

02:23.360 --> 02:31.130
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

02:31.130 --> 02:38.060
check if the button I'm going to please, Jake, if the button state is different from the previous

02:38.060 --> 02:38.450
one.

02:39.170 --> 02:40.940
OK, we're going to do an action.

02:41.180 --> 02:43.630
And two important things here.

02:44.190 --> 02:46.060
We are going to update you.

02:46.190 --> 02:48.260
The last time button changed.

02:48.860 --> 02:51.230
I'm going to write it and then explain why.

02:51.560 --> 02:52.280
So last.

02:53.510 --> 03:06.950
Time, but changed is equal to time, no, and then one state is equal to New York State and here do

03:06.980 --> 03:09.400
the action, whatever action we want to do.

03:09.800 --> 03:10.970
So what's happening here?

03:11.570 --> 03:18.710
The first the last time button change will be initialized to zero when we until we check the current

03:18.710 --> 03:19.190
time.

03:19.230 --> 03:22.790
OK, if the current time minus the last time to and change.

03:22.790 --> 03:29.780
So the duration between the time now and when we change the button, if this duration is greater than

03:29.780 --> 03:35.780
the duration we have set for the DBMS, delay we can enter is the look of now we are going to read the

03:35.780 --> 03:39.290
new button state and either state is still the same as before.

03:39.410 --> 03:44.280
So if it was low and no, it's too low, then we are not going to enter this if.

03:44.540 --> 03:51.590
OK, so we go out and the next time we enter the loop we can still go in the if, OK, because we have

03:51.590 --> 03:53.180
not updated the last time.

03:53.180 --> 03:56.210
But it changed, you know, like we did for the energy previously.

03:56.210 --> 04:02.690
We don't want to make sure that we execute this only every 50 milliseconds or every whatever time we

04:02.690 --> 04:10.550
can rig this as fast as we want is just when the button will change, when the new button state is different

04:10.550 --> 04:11.690
from the previous one.

04:12.110 --> 04:17.000
In that case, we are going to update this variable with the time.

04:17.000 --> 04:19.130
No, OK, and this is different from before.

04:19.130 --> 04:22.470
Before what we did is we simply added the delay here.

04:22.800 --> 04:26.570
Here what we do is we update the last time button change with time.

04:26.570 --> 04:26.930
No.

04:27.600 --> 04:32.080
OK, because this is the time when the button state has changed.

04:32.390 --> 04:36.680
So we just record the exact time when the button state has changed.

04:36.920 --> 04:44.240
And now when we go back in the loop time now minus last time button changed will not be greater and

04:44.240 --> 04:44.960
dbms zedi.

04:45.380 --> 04:53.180
So here we will only be able to go back in that if only if we have spent at least 50 milliseconds after

04:53.180 --> 04:55.320
we have ADT this valuable.

04:55.370 --> 05:02.300
So after the state of the button has changed, basically when we change the state of the button, we

05:02.300 --> 05:10.520
don't go here and we ignore this block for fifty milliseconds, which means that any bounce that will

05:10.520 --> 05:15.680
happen physically will still happen physically, but we are not going to read that bounce.

05:16.040 --> 05:21.350
OK, so now here we know that we can do the action and the action is simply the same as before.

05:24.210 --> 05:29.790
I'm going to copy this here and select and press double.

05:31.040 --> 05:32.270
And just remove that.

05:33.840 --> 05:35.790
All right, so now when we enter this.

05:37.240 --> 05:40.600
We can be pretty sure that this is not the bounce, OK?

05:40.620 --> 05:42.540
This is the real change of state.

05:43.140 --> 05:46.420
I'm going to upload the program.

05:48.680 --> 05:54.170
OK, and open the sale monitor, and now I'm going to press the button, I'm going to read it, but

05:54.240 --> 05:58.750
I'm OK and well, you can try as many times as you want.

05:59.440 --> 06:07.030
You will not get a price and release more than one time when you just press and release one thing,

06:07.730 --> 06:08.000
OK?

06:08.000 --> 06:10.690
And we have sold the bones, probably.

06:11.030 --> 06:13.490
Now let's modify these DBMS dillehay.

06:14.730 --> 06:21.440
Let's say, but zero, if I put zero, it means that just this structures is useless, OK?

06:21.450 --> 06:27.780
Because every time we are going to enter this, if we are not going to wait any time after the state

06:27.780 --> 06:34.680
has changed, OK, let's try with zero and let's see that we can have when talk.

06:35.590 --> 06:36.690
I'm going to press.

06:46.210 --> 06:49.310
OK, and you can see here we have the dance issue.

06:49.330 --> 06:51.440
OK, we don't press the button, really.

06:51.460 --> 06:57.130
You can see about the same time we have one moment in person, but unreleased.

06:58.950 --> 07:06.200
Now, I go back to my program and put correct values so you can but what I'm going to put 30 milliseconds

07:06.540 --> 07:08.060
that will work from my second.

07:08.700 --> 07:11.620
The thing is how to actually know which value you can sit here.

07:11.640 --> 07:15.690
So 50 or 30 milliseconds is already quite a good value.

07:15.720 --> 07:16.500
It should work.

07:17.050 --> 07:18.710
You can try different values.

07:19.050 --> 07:25.420
So the tradeoff here is simply if you put a value to low, well, you might still have bonuses.

07:25.440 --> 07:27.400
So let's say you put only three milliseconds.

07:27.750 --> 07:29.620
You might still see Bonsey.

07:29.640 --> 07:36.330
So you want to put a value high enough, but not too high, because if it's too high, well, this means

07:36.790 --> 07:42.070
the delay means that you're going to ignore the state for the button for a few milliseconds, OK?

07:42.300 --> 07:45.250
So if I just say, OK, I'm going to get 1000.

07:46.050 --> 07:52.740
The thing is that if I press release the button in less than one second, I'm going to meet also the

07:52.740 --> 07:55.590
real change of state when I release the button.

07:55.770 --> 08:00.030
So you want the value to be high enough so it doesn't get any bounce in it.

08:00.300 --> 08:03.010
But you also want it to be not too high.

08:03.300 --> 08:05.650
So you don't miss real change of state.

08:06.180 --> 08:06.560
All right.

08:06.570 --> 08:09.480
I know you can see Destructo here we went.

08:09.480 --> 08:14.680
We could have done in the program here after we read the button and we checked that.

08:14.790 --> 08:20.070
But instead from here, we could just dump a delay, for example, delete.

08:21.230 --> 08:25.650
Fifty milliseconds and just not use all of the structure.

08:25.880 --> 08:30.230
This would have been the same for this program, but then, as you know, with the delay, the problem

08:30.230 --> 08:33.320
is that we are going to stop the execution here.

08:33.590 --> 08:40.130
So if we want to do anything else other than this, well, that would be a problem with Destructo.

08:40.310 --> 08:42.000
We can go very, very fast.

08:42.020 --> 08:43.630
OK, you can see it.

08:43.640 --> 08:45.500
We read the time and then we have if.

08:45.800 --> 08:52.080
OK, so either we enter the if or not and then the actions will be very fast to execute.

08:52.560 --> 08:58.670
So if we have another action here, then this will not be a problem for the action to.

09:00.050 --> 09:06.380
All right, and the thing I want to emphasize is that you are going to be the last time the burden changed

09:06.680 --> 09:13.520
after the State of Union has changed, not just when you read it, but after you read it and that you

09:14.150 --> 09:16.570
see that the Golden State has changed.

09:16.760 --> 09:21.050
Only then you will have the desirable and updated with the time now.
