This is the two of the final project of the intercom system.
And in this step, we are going to start to write the Arduino code.
I'm going to write the Arduino code in three steps, first to initialize all the components and then
to focus on the output.
So what we send from the Arduino to the Raspberry Pi.
And the third step would be to focus on the input command.
So basically what we receive from the Raspberry Pi and apply those commands to the hardware we have
previously initialized.
I'm doing it this way.
Why?
Because well, we already have all the hardware components plugged to the Arduino on the circuit.
We know it's working because we already have tested them.
And also in this project, well, there is no relation between all the components in the Arduino program.
Only the Raspberry Pi will know how to combine them.
Okay.
When to power on the when to give some text to the LCD screen, when to ask to open the door, etcetera.
But the Arduino doesn't know any of that.
The Arduino just sends output messages and receives input commands and execute them independently for
each component.
So that's why I'm going with this order.
What you could also do and which is also a valid option, is to do the steps.
So one step for each component.
For example, if you were to start the circuit from scratch, you could add a component to the circuit
on the Arduino, test this component and add this component into the Arduino program until you have
tested and added all the components one by one.
All right, so now let's start.
And here in this step, I'm going to just initialize all the components and print something on the LCD
screen, like a starting message to just know that we have started the application.
Also, for each step, I'm going to save the project as a different file with the name of the step so
you can just download each program for each step independently.
All right, let's start.
So first I'm going to include the libraries we need.
And so for RGB, the buzzer and the push button, we don't need anything, but we need something for
the liquid crystal.
So liquid crystal dot h and include several dot h.
All right.
We have all the libraries we need.
And then I'm going to do a define for each of the pin.
So RGB, let's start with RGB red pin, which is PIN number 11 and then RGB green pin, which is number
ten.
Okay.
In this project here in the solution of this project, I'm going to write all the code from scratch.
Define RGB blue pin, which is nine.
Okay, We have the RGB.
Let's do the buzzer.
Define on buzzer pin, which is pin eight, and then define button pin, which is pin seven.
Okay.
There is no order for this.
We just do all the defined then.
Well, let's add the servo here.
Define servo pin 12.
We have the servo RGB buzzer button and then we need the LCD.
So LCD is PIN A4 and then define LCD E pin, which is A5 and we have four more with LCD for PIN, which
is number two.
Let's just copy this three more times.
The five D, six, D seven.
And we have two, three, four and five.
Okay.
Make sure you have the correct defines.
That's going to save you a lot of debugging time in the future.
Now let's create the global objects we need for the liquid crystal and the server.
So let's start liquid crystal LCD.
We need to give all those pins in the order so as default.
And then let's align here with the parentheses, the five, the six and the seven.
And let's do also servo.
Servo.
Let's name it Servo.
Okay.
LCD and several.
We just have one, so let's name it LCD and Servo.
If we had multiple ones, you would give of course, more meaningful names.
Okay.
For example, right door servo and left door servo here we just have one.
Now in the void setup.
So let's handle the void setup.
I'm going to initialize in the next step.
Okay?
Because here we just initialize hardware components.
So let's start with the servo servo dot attach.
We've several pin.
What we can also do for the several immediately is to put it in the close position.
So basically to close the door, to lock the door.
So we're going to do servo dot, right?
And here you can play with the servo.
You can just give different angles and put a plastic part on it to see which positions are going to
suit your project.
And what you can do is you can create let's create another two defines here.
Define several open door position and let's define several close door position so that if we have this
right now, then when we receive the command, we can just ask the server to go to this position.
And for me, I'm going to use 50 and 140 degrees.
Okay?
We are not going to go from 0 to 180 here.
I have just put two values in the middle and you can see the difference here is 90 degrees.
And so this here is the position where the plastic parts that I've put on my server is going to be straight.
So the door is not locked.
And here we rotate it by 90 degrees to close the door.
Okay.
A quick tip here I recommend is if you use a hobby servo motor, don't go to the extreme.
Okay?
Don't go close to zero and don't go close to 180.
Try to stay somewhere in the middle.
Okay.
But you can make your own experiment.
You can use those values to continue with the project.
But if you install the servo on a real door, then you might just experiment and choose different values.
All right.
So I'm going to first write the servo close door position.
Okay.
We don't want the door to be open by default.
We want the door to be closed by default.
So we have the servo initialized.
Now let's initialize the pushbutton, for example, with pin mode button pin input and then pin mode
buzzer pin output.
Let's do the RGB pin mode with RGB red pin output.
And let's do also the two other legs.
So we have green and we have blue and then we are left with the LCD screen.
So LCD dot begin 16 by two and we are going to print a welcome message or just starting LCD dot print
starting like this.
And what I'm going to do is I'm going to add just a delay here with 1000 milliseconds.
So just one second and then LCD dot clear.
So this I usually do in my Arduino applications when I have an LCD is for something like starting for
one second, two seconds, something like that, and then clear the screen so that we know when the
problem is starting.
And we know basically when we enter the void loop.
And that's always a nice message to print for the user.
All right.
Now that we have all of that, let's try.
Let's upload the code.
Okay.
So we should have the servo that goes to the closed position.
We should have also starting printed on the LCD screen.
So I'm going to upload.
Let's make sure the ball is here and upload.
Let's name it Intercom.
Step two create a step.
One was to define the protocol.
Compiling.
And then uploading.
Starting.
You can see we had starting and well, the server is in the closed position.
It was already here so it didn't move.
But you can see here the closed position is the, uh, the, the lock mechanism will be like this and
the open position, the lock mechanism will be up.
Okay.
Like this.
Close and open.
Okay.
And we have just finished the step two of the project, so not the most interesting one of course,
but a necessary one.
So then we can focus on the application because we have everything correctly initialized.