WEBVTT

00:00.080 --> 00:05.900
Let's now see how to read data from the push button and more specifically, how to know when we have

00:05.900 --> 00:07.130
pressed on the button.

00:07.130 --> 00:10.160
So the exact moment when we have pressed on the button.

00:10.190 --> 00:14.960
This is something you should already know how to do as per the prerequisites of the course, but I'm

00:14.960 --> 00:16.520
still going to do it quickly.

00:16.520 --> 00:17.480
As a recap.

00:17.480 --> 00:19.880
Also, we will debounce the button.

00:19.880 --> 00:26.600
So first of all, what you're going to do is to create a define here button.

00:27.680 --> 00:30.020
So define a constant int.

00:30.020 --> 00:32.990
And so the pin is number seven.

00:33.650 --> 00:42.170
And then if you want to initialize the push button, you do pin mode with button pin and then input.

00:42.470 --> 00:44.600
So the push button is initialized.

00:44.630 --> 00:53.360
Now if you want to read from the push button, you are going to do digital read button pin and this

00:53.360 --> 00:55.850
is going to return you a byte.

00:55.880 --> 00:58.370
Okay, that is high or that is low.

00:58.370 --> 01:03.710
So for example, bytes state is equal to digital read button pin.

01:04.070 --> 01:09.860
Now if we want to check if we have pressed on the push button, what we need to do is we need to save

01:09.860 --> 01:10.700
the previous state.

01:10.700 --> 01:15.920
So we need to read the state, save the previous state, and then read again and check if we have something

01:15.920 --> 01:16.460
different.

01:16.490 --> 01:16.940
Okay.

01:16.940 --> 01:26.420
So I'm going to add here, byte previous button state, and I'm simply going to initialize that here

01:26.600 --> 01:31.130
to the current state of the button just after we initialize it.

01:31.130 --> 01:33.680
So that will be the first state of the push button.

01:33.680 --> 01:39.110
And so now what we need to do in the for loop is to read the push button again with digital read and

01:39.110 --> 01:43.360
then compare to the previous button state and see if we have something different.

01:43.370 --> 01:49.610
But what I'm also going to do is I'm going to debounce the push button because if we just compare the

01:49.610 --> 01:54.890
previous and the current state, what's going to happen is we're going to have the bounce mechanism.

01:54.890 --> 02:00.260
So the physical bounce of the button and this will give us some wrong measurements.

02:00.350 --> 02:00.750
Okay.

02:00.750 --> 02:07.650
So we need to debounce the button to make sure that when we actually press, we get just one press on

02:07.650 --> 02:11.640
the code and not multiple prices because the button is bouncing.

02:11.640 --> 02:16.440
So to do that, I'm going to create two new variables here.

02:16.740 --> 02:23.430
Unsigned long last time button pressed is equal to.

02:23.460 --> 02:25.500
So let's initialize to millis or to zero.

02:25.500 --> 02:26.460
It doesn't matter.

02:26.460 --> 02:36.060
And then unsigned long let's say button debounce is equal to and let's put 50 milliseconds.

02:36.060 --> 02:41.730
So what we're going to do here, basically we're going to first in the void loop, get the time.

02:41.730 --> 02:47.130
So unsigned long time now is equal to millis.

02:47.430 --> 02:56.430
And then we are going to do if time now minus last time the button has been pressed if this is greater

02:56.430 --> 03:02.280
or equal than the button debounce duration here, then we can enter this.

03:02.310 --> 03:07.770
If so, we will see that when the state of the button changes, we're going to update the last time

03:07.770 --> 03:13.740
the button was pressed to the current time and so that the next time we enter the void loop, we're

03:13.740 --> 03:14.910
going to check the condition.

03:14.910 --> 03:18.090
So time now minus last time the button has been pressed.

03:18.090 --> 03:20.970
And if this is lower than the delay.

03:20.970 --> 03:27.090
So basically, if this is lower than 50 milliseconds, then we are not going to read the state again.

03:27.090 --> 03:32.940
So basically we are going to ignore the button for 50 milliseconds every time the button has been pressed

03:32.940 --> 03:35.910
so we don't read all the bounces of the button.

03:35.910 --> 03:41.370
So now if enough time has passed, what we can do is do bite button state.

03:41.370 --> 03:47.220
So the current one is equal to digital read with button pin.

03:49.200 --> 03:49.980
Like this.

03:50.790 --> 03:59.640
We read the button state and then we do if Button State is different from the previous button state.

03:59.910 --> 04:03.930
In that case, we know that the state has changed.

04:04.080 --> 04:10.110
And what we can do is to now update the last time the button pressed.

04:10.110 --> 04:11.970
So actually let's name it different.

04:11.970 --> 04:14.940
Last time button changed.

04:14.970 --> 04:15.480
Okay.

04:15.990 --> 04:23.100
Because it's not pressed here, it's just changed because if we are here, it could be pressed or released.

04:23.100 --> 04:25.980
So I'm just going to use changed, which is for both.

04:25.980 --> 04:26.190
Okay.

04:26.190 --> 04:29.700
So last time the button has changed is actually time now.

04:29.850 --> 04:30.240
Okay.

04:30.240 --> 04:32.550
Because that's what just happened.

04:32.550 --> 04:39.030
And also I'm going to update the previous button state, which is now the current button state.

04:39.180 --> 04:45.090
And so the next time we enter the void loop, we're going to first check if enough time has passed since

04:45.090 --> 04:46.800
the last time button changed.

04:46.800 --> 04:55.390
And we only going to enter this if again after 50 milliseconds and then well, when we are here in the

04:55.390 --> 04:58.600
code, we know that the button state has changed.

04:58.600 --> 05:03.340
So to know if the button has actually been pressed or released, we do.

05:03.370 --> 05:12.040
If button State is equal to high in our case because we have a pull down resistor, this means that

05:12.040 --> 05:17.500
the button has been pressed and if it's low, it means the button has been released.

05:17.500 --> 05:18.940
So here we could do so.

05:18.940 --> 05:22.120
Let's actually do sale dot begin.

05:22.690 --> 05:30.480
So here we use sale to debug and serial dot print.

05:30.580 --> 05:35.410
Ln let's say button has been pressed.

05:35.830 --> 05:42.280
So when we enter this, if we know that the button has been pressed, so just pressed and we don't have

05:42.280 --> 05:48.460
any bounds because the next time we are going to read the button is 50 milliseconds later.

05:48.460 --> 05:50.920
So let's upload the code.

05:52.270 --> 05:54.140
And actually, we have an error.

05:54.160 --> 05:55.420
Why do I have this error?

05:55.450 --> 06:01.690
You can see can't open device because I have simply not plugged my Arduino to the Raspberry Pi or to

06:01.690 --> 06:02.260
the computer.

06:02.260 --> 06:04.780
If you are using your normal computer.

06:04.780 --> 06:06.580
And now check tools.

06:06.940 --> 06:07.630
Okay.

06:07.630 --> 06:08.560
Upload.

06:10.490 --> 06:11.750
And done uploading.

06:11.750 --> 06:13.580
I'm going to open the serial monitor.

06:13.610 --> 06:17.000
Make sure the border is correct and I'm going to press on.

06:17.000 --> 06:19.400
The push button button has been pressed.

06:19.520 --> 06:19.760
Okay.

06:19.880 --> 06:20.720
Press again.

06:20.720 --> 06:22.820
And you can see every time I price.

06:22.820 --> 06:30.260
So price again button has been pressed one more time and well, the code is working.

06:30.680 --> 06:37.250
So this is the structure that you are going to use often to know when you have pressed or released on

06:37.250 --> 06:40.490
a push button and with the debounce mechanism.
