WEBVTT

00:00.080 --> 00:04.460
This is the two of the final project of the intercom system.

00:04.610 --> 00:08.840
And in this step, we are going to start to write the Arduino code.

00:08.870 --> 00:14.690
I'm going to write the Arduino code in three steps, first to initialize all the components and then

00:14.690 --> 00:16.250
to focus on the output.

00:16.250 --> 00:20.780
So what we send from the Arduino to the Raspberry Pi.

00:20.810 --> 00:24.020
And the third step would be to focus on the input command.

00:24.020 --> 00:29.600
So basically what we receive from the Raspberry Pi and apply those commands to the hardware we have

00:29.600 --> 00:31.260
previously initialized.

00:31.280 --> 00:32.960
I'm doing it this way.

00:32.990 --> 00:33.350
Why?

00:33.380 --> 00:37.790
Because well, we already have all the hardware components plugged to the Arduino on the circuit.

00:37.820 --> 00:40.940
We know it's working because we already have tested them.

00:41.120 --> 00:48.650
And also in this project, well, there is no relation between all the components in the Arduino program.

00:48.680 --> 00:51.820
Only the Raspberry Pi will know how to combine them.

00:51.830 --> 00:52.080
Okay.

00:52.190 --> 00:59.910
When to power on the when to give some text to the LCD screen, when to ask to open the door, etcetera.

00:59.910 --> 01:02.220
But the Arduino doesn't know any of that.

01:02.220 --> 01:09.540
The Arduino just sends output messages and receives input commands and execute them independently for

01:09.540 --> 01:10.350
each component.

01:10.350 --> 01:12.930
So that's why I'm going with this order.

01:12.930 --> 01:18.210
What you could also do and which is also a valid option, is to do the steps.

01:18.210 --> 01:20.580
So one step for each component.

01:20.610 --> 01:26.040
For example, if you were to start the circuit from scratch, you could add a component to the circuit

01:26.070 --> 01:32.580
on the Arduino, test this component and add this component into the Arduino program until you have

01:32.580 --> 01:36.030
tested and added all the components one by one.

01:36.360 --> 01:37.980
All right, so now let's start.

01:37.980 --> 01:44.670
And here in this step, I'm going to just initialize all the components and print something on the LCD

01:44.670 --> 01:49.950
screen, like a starting message to just know that we have started the application.

01:50.130 --> 01:56.280
Also, for each step, I'm going to save the project as a different file with the name of the step so

01:56.280 --> 02:01.100
you can just download each program for each step independently.

02:01.100 --> 02:02.570
All right, let's start.

02:03.080 --> 02:06.140
So first I'm going to include the libraries we need.

02:06.560 --> 02:13.160
And so for RGB, the buzzer and the push button, we don't need anything, but we need something for

02:13.160 --> 02:14.390
the liquid crystal.

02:14.720 --> 02:25.790
So liquid crystal dot h and include several dot h.

02:26.450 --> 02:26.870
All right.

02:26.870 --> 02:28.730
We have all the libraries we need.

02:28.730 --> 02:33.140
And then I'm going to do a define for each of the pin.

02:33.410 --> 02:45.350
So RGB, let's start with RGB red pin, which is PIN number 11 and then RGB green pin, which is number

02:45.350 --> 02:46.070
ten.

02:46.070 --> 02:46.310
Okay.

02:46.310 --> 02:51.230
In this project here in the solution of this project, I'm going to write all the code from scratch.

02:51.740 --> 02:57.200
Define RGB blue pin, which is nine.

02:57.230 --> 02:59.420
Okay, We have the RGB.

02:59.420 --> 03:01.070
Let's do the buzzer.

03:01.100 --> 03:12.510
Define on buzzer pin, which is pin eight, and then define button pin, which is pin seven.

03:12.510 --> 03:12.720
Okay.

03:12.720 --> 03:13.980
There is no order for this.

03:13.980 --> 03:16.950
We just do all the defined then.

03:16.950 --> 03:18.570
Well, let's add the servo here.

03:18.600 --> 03:23.040
Define servo pin 12.

03:23.430 --> 03:27.360
We have the servo RGB buzzer button and then we need the LCD.

03:27.360 --> 03:45.210
So LCD is PIN A4 and then define LCD E pin, which is A5 and we have four more with LCD for PIN, which

03:45.210 --> 03:46.620
is number two.

03:47.220 --> 03:50.640
Let's just copy this three more times.

03:51.990 --> 03:56.280
The five D, six, D seven.

03:57.850 --> 04:01.480
And we have two, three, four and five.

04:01.510 --> 04:01.690
Okay.

04:01.690 --> 04:04.450
Make sure you have the correct defines.

04:04.630 --> 04:08.470
That's going to save you a lot of debugging time in the future.

04:08.620 --> 04:14.530
Now let's create the global objects we need for the liquid crystal and the server.

04:14.710 --> 04:19.390
So let's start liquid crystal LCD.

04:20.320 --> 04:29.320
We need to give all those pins in the order so as default.

04:29.740 --> 04:40.780
And then let's align here with the parentheses, the five, the six and the seven.

04:42.760 --> 04:45.550
And let's do also servo.

04:47.160 --> 04:47.760
Servo.

04:48.840 --> 04:49.740
Let's name it Servo.

04:49.770 --> 04:50.100
Okay.

04:50.140 --> 04:51.150
LCD and several.

04:51.150 --> 04:54.480
We just have one, so let's name it LCD and Servo.

04:54.480 --> 04:58.530
If we had multiple ones, you would give of course, more meaningful names.

04:58.560 --> 04:59.070
Okay.

04:59.190 --> 05:04.410
For example, right door servo and left door servo here we just have one.

05:04.710 --> 05:06.480
Now in the void setup.

05:06.480 --> 05:08.670
So let's handle the void setup.

05:09.480 --> 05:11.820
I'm going to initialize in the next step.

05:11.820 --> 05:12.030
Okay?

05:12.030 --> 05:14.810
Because here we just initialize hardware components.

05:14.820 --> 05:19.440
So let's start with the servo servo dot attach.

05:20.170 --> 05:22.870
We've several pin.

05:23.800 --> 05:30.220
What we can also do for the several immediately is to put it in the close position.

05:30.220 --> 05:32.980
So basically to close the door, to lock the door.

05:32.980 --> 05:36.280
So we're going to do servo dot, right?

05:36.280 --> 05:38.860
And here you can play with the servo.

05:38.890 --> 05:45.640
You can just give different angles and put a plastic part on it to see which positions are going to

05:45.640 --> 05:46.900
suit your project.

05:47.380 --> 05:53.560
And what you can do is you can create let's create another two defines here.

05:54.410 --> 06:11.240
Define several open door position and let's define several close door position so that if we have this

06:11.240 --> 06:17.810
right now, then when we receive the command, we can just ask the server to go to this position.

06:17.810 --> 06:25.850
And for me, I'm going to use 50 and 140 degrees.

06:25.880 --> 06:26.120
Okay?

06:26.120 --> 06:28.940
We are not going to go from 0 to 180 here.

06:29.120 --> 06:34.910
I have just put two values in the middle and you can see the difference here is 90 degrees.

06:34.910 --> 06:41.960
And so this here is the position where the plastic parts that I've put on my server is going to be straight.

06:41.960 --> 06:43.880
So the door is not locked.

06:43.880 --> 06:49.430
And here we rotate it by 90 degrees to close the door.

06:49.580 --> 06:50.180
Okay.

06:50.300 --> 06:54.870
A quick tip here I recommend is if you use a hobby servo motor, don't go to the extreme.

06:54.900 --> 06:55.110
Okay?

06:55.140 --> 06:59.700
Don't go close to zero and don't go close to 180.

06:59.790 --> 07:01.800
Try to stay somewhere in the middle.

07:01.830 --> 07:02.250
Okay.

07:02.250 --> 07:03.690
But you can make your own experiment.

07:03.690 --> 07:06.270
You can use those values to continue with the project.

07:06.270 --> 07:11.610
But if you install the servo on a real door, then you might just experiment and choose different values.

07:11.640 --> 07:12.090
All right.

07:12.090 --> 07:17.250
So I'm going to first write the servo close door position.

07:17.550 --> 07:17.820
Okay.

07:17.820 --> 07:20.310
We don't want the door to be open by default.

07:20.310 --> 07:23.040
We want the door to be closed by default.

07:23.700 --> 07:26.550
So we have the servo initialized.

07:26.700 --> 07:37.110
Now let's initialize the pushbutton, for example, with pin mode button pin input and then pin mode

07:37.110 --> 07:41.670
buzzer pin output.

07:42.210 --> 07:50.670
Let's do the RGB pin mode with RGB red pin output.

07:51.510 --> 07:54.690
And let's do also the two other legs.

07:56.710 --> 08:07.210
So we have green and we have blue and then we are left with the LCD screen.

08:07.210 --> 08:17.740
So LCD dot begin 16 by two and we are going to print a welcome message or just starting LCD dot print

08:18.970 --> 08:21.340
starting like this.

08:22.220 --> 08:28.220
And what I'm going to do is I'm going to add just a delay here with 1000 milliseconds.

08:28.220 --> 08:31.610
So just one second and then LCD dot clear.

08:32.090 --> 08:37.940
So this I usually do in my Arduino applications when I have an LCD is for something like starting for

08:37.970 --> 08:43.670
one second, two seconds, something like that, and then clear the screen so that we know when the

08:43.670 --> 08:44.420
problem is starting.

08:44.420 --> 08:47.210
And we know basically when we enter the void loop.

08:47.210 --> 08:49.970
And that's always a nice message to print for the user.

08:50.090 --> 08:50.540
All right.

08:50.570 --> 08:54.240
Now that we have all of that, let's try.

08:54.260 --> 08:55.700
Let's upload the code.

08:55.890 --> 08:56.020
Okay.

08:56.030 --> 08:58.940
So we should have the servo that goes to the closed position.

08:58.940 --> 09:02.480
We should have also starting printed on the LCD screen.

09:02.480 --> 09:03.710
So I'm going to upload.

09:03.710 --> 09:07.690
Let's make sure the ball is here and upload.

09:07.700 --> 09:11.270
Let's name it Intercom.

09:12.140 --> 09:14.810
Step two create a step.

09:14.810 --> 09:16.640
One was to define the protocol.

09:17.540 --> 09:18.710
Compiling.

09:19.810 --> 09:21.550
And then uploading.

09:23.360 --> 09:24.290
Starting.

09:24.710 --> 09:29.300
You can see we had starting and well, the server is in the closed position.

09:29.300 --> 09:31.490
It was already here so it didn't move.

09:31.820 --> 09:38.690
But you can see here the closed position is the, uh, the, the lock mechanism will be like this and

09:38.690 --> 09:41.420
the open position, the lock mechanism will be up.

09:41.840 --> 09:42.240
Okay.

09:42.260 --> 09:43.190
Like this.

09:44.210 --> 09:46.310
Close and open.

09:47.000 --> 09:47.300
Okay.

09:47.300 --> 09:52.250
And we have just finished the step two of the project, so not the most interesting one of course,

09:52.250 --> 09:53.930
but a necessary one.

09:53.930 --> 09:59.600
So then we can focus on the application because we have everything correctly initialized.
