WEBVTT

00:00.080 --> 00:05.030
This is step number three of the final project of the intercom system.

00:05.030 --> 00:11.240
And now that we have initialized all the hardware components on the Arduino, we're going to write the

00:11.240 --> 00:18.020
code so we can send the output messages to the Raspberry Pi when we have something that happens with

00:18.020 --> 00:18.560
the hardware.

00:18.560 --> 00:23.210
So actually here we have just the button code to write.

00:23.450 --> 00:29.570
So first of all, I'm going to save this as intercom step three.

00:29.780 --> 00:30.410
Okay?

00:31.160 --> 00:31.460
Okay.

00:31.490 --> 00:32.150
Save.

00:32.660 --> 00:36.710
And let's actually open again the protocol that we have written here.

00:36.740 --> 00:39.050
So from it's right there.

00:40.340 --> 00:46.760
So we can see here from Arduino to Raspberry Pi, we just have the button pressed command to send with

00:46.760 --> 00:47.600
backslash n.

00:47.630 --> 00:49.550
So that's what we are going to do.

00:51.560 --> 00:52.100
And so.

00:52.100 --> 00:56.300
Well, this we have already done that before, but I'm still going to do it again.

00:56.630 --> 01:02.060
Right there is to read from the push button and detect when we have pressed on the push button.

01:02.240 --> 01:06.740
So I'm going to create a 2 or 3 new variables here.

01:07.070 --> 01:10.900
Let's do that right there as a global variables.

01:10.910 --> 01:20.360
So unsigned long last time button changed with equal to millis or zero, it doesn't matter.

01:20.360 --> 01:26.090
And unsigned long debounce delay.

01:26.090 --> 01:28.550
Let's put 50 milliseconds.

01:28.580 --> 01:33.350
You can adjust that a bit more, a bit less depending on how it works with your circuit.

01:33.620 --> 01:36.380
So this is going to be for the Debounce mechanism.

01:36.590 --> 01:45.230
And then I'm going to also add byte previous button state and the previous button state.

01:45.230 --> 01:56.600
I initialize it here, previous button state is equal to digital read from the button pin.

01:56.610 --> 02:04.650
And of course I do that after I have put the pin mode with button pin input now in the void loop.

02:06.240 --> 02:12.300
So in the void loop, we can read from the push button, but before we read from the push button, we're

02:12.300 --> 02:15.870
going to do the Debounce mechanism to be sure we can read from the push button.

02:15.870 --> 02:21.420
So unsigned long time now is equal to millis.

02:21.420 --> 02:27.150
So we get the time every well, every time we enter the void loop, which is going to be pretty fast.

02:27.990 --> 02:37.110
If so, if the current time time now minus the last time the button state has changed here is greater

02:37.110 --> 02:41.340
or equal then the debounce delay.

02:42.920 --> 02:49.520
So if we have spent enough time since the button has changed for the last time, then we can read the

02:49.520 --> 02:50.570
button state.

02:51.290 --> 02:58.940
So bite button state is equal to digital read button pin.

02:59.480 --> 03:02.950
And then we can compare the button state.

03:02.960 --> 03:06.110
So the current one we read from the previous one.

03:06.110 --> 03:08.570
So from previous button state.

03:08.600 --> 03:14.360
If those are different, then we have either pressed or released the button.

03:14.360 --> 03:18.920
And in that case, first we can update this.

03:19.130 --> 03:22.160
So the last time the button has changed is actually now.

03:22.160 --> 03:23.330
So we put time.

03:23.330 --> 03:29.180
Now we also say that the previous button state is the current one for the next time we enter the void

03:29.180 --> 03:29.720
loop.

03:31.010 --> 03:38.330
And then we can do an action so we can check if, for example, button State is equal to high, which

03:38.330 --> 03:44.470
means that we have pressed on the button because we have a pull down resistor.

03:44.480 --> 03:47.180
And if it's low, we have just released the button.

03:47.180 --> 03:55.060
But here we want to know when we press on the button and once we have this, we can send to sales.

03:55.060 --> 03:59.240
So send the message to sale and to send the message to sale.

03:59.270 --> 04:01.490
I'm going to initialize sale here.

04:02.210 --> 04:04.250
Serial dot.

04:04.250 --> 04:07.400
Begin with this baud rate.

04:07.400 --> 04:09.530
We're going to use the same in the Raspberry Pi.

04:09.770 --> 04:18.260
Let's do while not serial okay for compatibility with some Arduino boards and here I'm going to add

04:18.260 --> 04:23.810
something else that I didn't do before and we probably won't need it in that project, but that can

04:23.810 --> 04:29.930
be a good best practice to add it to the while serial dot available.

04:31.240 --> 04:33.400
Is greater than zero.

04:34.030 --> 04:38.260
You do serial dot read.

04:38.590 --> 04:41.640
So read is going to read only one byte.

04:41.650 --> 04:43.930
So here we just read it and we don't store it.

04:43.960 --> 04:45.370
We don't do anything with it.

04:45.370 --> 04:49.390
But basically what we do here is we just clear the input buffer.

04:49.630 --> 04:55.900
If you remember with Serial in Python, you have the reset input buffer function.

04:55.900 --> 05:03.280
We don't have one in Arduino, but we can easily implement it by just checking that well, while we

05:03.280 --> 05:08.140
still have bytes on the input buffer, we read them so we clear them from the buffer.

05:08.230 --> 05:13.480
So we are sure that when we start here we don't have anything in the input buffer.

05:13.690 --> 05:17.950
And so I said for this project is not going to be useful because why is that?

05:17.980 --> 05:24.100
Because when we start the Python program and we initialize the communication with the Arduino, well,

05:24.100 --> 05:26.320
the Arduino is going to be ready first.

05:26.320 --> 05:33.770
So by the time that we enter this block of code and that we enter the void loop, the Python program

05:33.770 --> 05:36.980
is still not going to be sending data.

05:37.010 --> 05:37.430
Okay?

05:37.430 --> 05:44.060
So this is should not be called, but that can be a best practice for other projects of your own.

05:44.120 --> 05:44.600
Okay.

05:44.600 --> 05:50.420
If you want to make different boards communicate between each other and now that the cell is initialized,

05:50.420 --> 05:55.520
let's actually do here serial dot print.

05:55.570 --> 06:02.600
Ln okay, because we need the backslash n at the end button pressed exactly like that.

06:02.810 --> 06:04.580
This should be it.

06:04.580 --> 06:05.600
Let's check.

06:06.260 --> 06:09.680
So we have those three variables here.

06:10.100 --> 06:14.210
We correctly initialize the state for the button.

06:14.210 --> 06:15.920
We read the previous state.

06:16.130 --> 06:22.520
We have the serial initialization and then we read from the push button with the debounce mechanism.

06:22.520 --> 06:26.000
And when the button is pressed, we send button pressed.

06:26.010 --> 06:29.600
Let's connect the Arduino.

06:31.340 --> 06:34.690
Okay, let's now so it's good.

06:34.690 --> 06:44.380
Let's now upload the code and well, actually I have forgotten so just made a typo here.

06:47.980 --> 06:50.440
Uploading down, uploading.

06:50.440 --> 06:54.790
Let's open the serial monitor and I'm going to press on the push button.

06:54.790 --> 06:57.310
So starting pressing the push button.

06:57.310 --> 06:59.530
Button pressed button pressed button pressed.

06:59.530 --> 07:05.290
So every time I press, I receive a button pressed message on the serial monitor.

07:05.410 --> 07:07.120
Everything is correct.

07:07.120 --> 07:08.530
And that's the end of this.

07:08.530 --> 07:15.040
Step number three, we can now focus on the input comments that we receive from the Raspberry Pi.
