This is step number four of the project.
And in this step, we are going to get all the input commands from Serial on the Arduino side and then
make sure that those commands are correctly executed on the hardware components we have connected to
the Arduino.
So we have already initialized all the pins.
Everything we can.
So we have initialized the communication.
We can send also the button pressed command message when we press on the button.
Now what we can do is in this void loop, well, we have this first action, which is to send something
to the side.
And you can see we don't have any delay here.
So it's not blocking.
It's going to be very fast.
We can add after this.
The code to check from Serial if we have received something.
So if serial dot available.
Greater than zero, then we can get the command with let's store as a string cmd is equal to serial
dot read string until backslash n with just one single quote character.
Once we have this command, well, let's see what kind of command we received.
So if cmd and now.
Well, I'm going to come back to the protocol we have defined here.
You can see we have all those commands to handle.
So open door, close door print, text, play buzzer and set led.
So let's write the code for all of that.
So if the command is equal to open door, okay, so if it's equal exactly to open door, then we can
do something.
I'm just going to write all the else if first we have close door.
Okay.
And we're going to close the door with the servo motor.
Else if command.
So.
Here.
This is an exact comment here.
This is a comment that starts with this and then we have some text are some variables we want to extract.
So for those comments, I'm going to do comment.
Dot starts with print, text and colon and two parentheses to close.
Then we have so else if comment starts with we have play buzzer colon and the last one comment starts
with set colon.
Okay.
So as you can see, if you add more comments, it doesn't get more complex.
You just have one more elseif and that's it.
Your program is not going to be more complex and let's do else.
And here I'm going to give you another best practice.
Well, let's say you have received a comment.
So something that you can read from Serial and it doesn't correspond to any of that, which means you
have probably received some garbage comment.
And in that case, what you can choose to do is to also clear the input buffer.
Okay?
Because if you have received something that is wrong, maybe you still have some bytes that have been
received and that are in the buffer that you're going to read next.
And so what we can do is do while dot available greater than zero, let's actually put some parentheses
and we do serial dot read.
So the same thing we did here to clear the input buffer we do it here in the else.
Okay so the else you could choose different behaviors.
You could find some way to print some errors.
So on the LCD screen or maybe send back a comment to the Raspberry Pi that's going to process it and
print it that you have sent an error here.
We just choose to clear the input buffer so that the next time we enter the while loop, we give a new
chance for the Raspberry Pi to send a correct comment.
All right.
Let's also save this as the step number four.
So don't forget.
Okay.
Save.
Let's just verify.
Let's just compile to see that we have the correct structure.
Don't compile.
Okay, so the structure is correct.
Now let's do the commands one by one.
So we have already initialized everything.
That's going to be quite easy.
So if we have the open door command, what we do is simply to send the servo to the position which we
have defined here, the open door position.
So I'm going to do simply servo, dot, write, open the position.
And if we receive the close door.
Command.
We just do.
So.
All right.
We've said we'll close the position.
Okay.
Those are actually the simplest commands to execute.
And well, here what we do is we directly send the command.
Okay.
To the server.
What you could do as an improvement and not gonna do that here, but you can do that as an improvement
is to make the server go a little bit slower with a non-blocking delay mechanism in the void loop.
So you could have another part of the code here that's going to compare the current angle of the server
with the target angle and check if it needs to move a bit.
And every time wait for example, ten milliseconds before it moves for one degree.
Okay.
But here we directly send the command, go to open position, go to closed position.
All right.
Now, when we receive print text, what we do.
So the first thing we do is comment dot remove.
Let's remove the print text.
So we have zero, one, two, three, four, five, six, seven, eight, nine, ten and 11.
So we're going to remove from 0 to 11 and we are left with the text to print so we can do LCD dot clear
to remove anything LCD dot set cursor.
Zero and zero and then LCD dot print cmd.
I'm going to add an improvement here because, well, you're going to just print on the first line and
you have well, you have 16 character on the first line, 16 character on the second line.
And here we just write on the first line.
So everything after 16 character is going to be truncated.
What we could do is that if you receive a string that is more than 16 characters, you bring the following
on the second line.
Okay, so you can use all the 32 characters.
And so what I'm going to do is I'm going to do string line one is equal to comment dot substring.
I'm going to take from 0 to 16 and then string line two is equal to comment dot substring from 16 to
the end of the string.
Okay, so I still clear the screen.
I set the cursor to zero zero and then I print not the comment but print line one.
And then do LCD dot set cursor zero one for the second line LCD dot print line two.
Great.
So that's a bit improved here.
Still has an improvement.
What you can do is, for example, make the text rotate if you have more than 32 characters, because
here after 32 characters, it's going to be truncated.
But let's keep things simple for now.
Then we have the play buzzer.
So with the play buzzer, we're going to do first comment dot remove and let's count zero one, two,
three, four, five, six, seven, eight, nine, ten, 11, 12.
So let's remove 12 And then what we are going to get is so frequency, comma, duration, but we don't
know exactly how many characters this is going to take and how many characters this is going to take.
So what we can do is we can focus on this comma, we can find the index of that comma and then take
everything before the comma and everything after the comma.
And so I'm going to do int comma index is equal to and you have a function that is so on the string
index off like this and you give a character with single quotes like that, it's going to search for
the comma and return the index of the comma.
So of course you need to have a comma in the string.
And once we have gotten this comma index, now we can do int frequency is equal to comment dot substring
from zero to the comma index.
And we are going to also do dot to int to convert this string to an integer and then int duration is
equal to comment dot substring comma index plus one.
Okay.
Because we don't want to actually get the comma in the number.
Okay.
So here we're going to go from zero to the comma index, but this is not going to go until the comma
is going to go just one before.
And when we start again, we start just after the comma to int and now we have the frequency and the
duration.
We can do tone buzzer pin with frequency and duration and that's going to play the tone on the buzzer.
All right.
The last command is settled and so we've already done that before.
So first of all, the command dot remove zero to let's count zero one, two, three, four, 5678 0
to 8.
And then we have something like this and we need to get red, green and blue.
So we do int red is equal to command dot sub string from zero to so 012 and 3.2 int then int green is
equal to so we have 01234567.
So 4 to 7 command dot substring 4 to 7 to int.
And then int blue is equal to comment dot sub string and we're going to start at 01234, five, six,
seven, eight to the end.
To eat now that we have the red, green and blue.
So let's remove that comment.
Now we just do analog, right?
RGB red.
PIN we've read.
And then the same for green.
And the same for blue.
Green and blue.
You can see now our code is about 100 lines, but it's still quite clear.
Okay.
We have first initialization, so some defines and some objects.
We need to create some global variables in the setup.
We start communication, we initialize all the pins, all the components.
Okay, We do some setup actions.
For example, put the servo in a certain position, print some text on an LCD, and then in the void
loop we have first action, which is to check the push button and send the message to Syria.
And second action is to process the data we receive from Serial.
And for each command we have a different block of code.
And well, if you have followed all the previous lessons and activities, you can see that the structure
of the Arduino program is almost the same.
Okay?
That structure that we have here is going to be almost the same in the void loop.
You have all the outputs, all the inputs, and well, that's pretty much it.
If you know how to do this, you know how to do another project.
And you can see here there is no relation between any of the components.
Okay, this is an independent action.
This is an independent action.
Also this one, that one, that one, everything is independent.
The glue or the logic of the application will be from the Raspberry Pi.
So now what we can do is we can test this code actually, because how can you know that it's working?
Well, you have to test it.
Let's upload it to the Arduino.
Okay.
So we have starting on the LCD screen and now let's open the serial monitor.
So let's check that we still have.
Okay, starting again.
Let's check that.
We still have the button pressed.
Okay.
And then I'm going to do print text.
Let's say hello.
It's working.
Let's print some plus some more characters.
Okay.
Hello.
Plus some more characters on the LCD screen and anything that I'm going to write.
So here I use actually 31 characters.
Everything I'm going to write after 32 characters is going to be truncated.
But now you can see that it's working.
So print text is working.
What else do we have?
Open door.
Let's try open door.
So actually the door is closed.
Let's do open door and what?
It's working.
As you can see here.
I had to move a little bit the power, just make sure the power is correctly plugged here.
And this is why this is a limitation of a circuit that you do with a breadboard and some wires like
this is that, of course, the connections may be not 100% stable.
Okay.
So of course, what I recommend is if you try to do this project on a real door is that, well, first
you use a different power supply, use an external power supply for the servomotor, and then you also,
of course, you will need to solder the component.
Okay.
But that's another step.
And just here, that was just here to show you that, well, we have some limitations actually with
a breadboard because all those wires will at the end you will have something that is not working because
something is not correctly plugged.
But here.
So we have open door that's working.
Let's do close door.
And okay, it's working.
So we have closed the open door.
Currently working.
Open door.
Close door print Text.
Play buzzer.
Let's actually do something with that.
Play Buzzer.
Let's send, let's say 400 frequency and 500 milliseconds.
It's working.
Let's send something else like 301,000.
Okay.
It's correctly working and set.
Let's put the right full on 000 and 000 and well, actually, we have an error and it's more like white
and.
Well, that's the of course if I put the red, which was 255 to each one to the right, the green and
the blue, that's going to be white, of course.
So I need to change that to use the green here and to use the blue here.
And let's upload again.
Okay.
Set led to 55 000000.
We have red.
Let's do now.
0002 55.
It should be green and then 000 and 255 at the end.
It should be blue and yes, it is correctly working.
All right.
So now the Arduino code should be done for the application.
So it doesn't mean you don't have to come back to it later on.
Maybe you will need to change or to tweak something because when you program on a Raspberry Pi, you're
going to maybe realize that some stuff on the Arduino should be handled differently.
But still, we should be pretty safe because we already have defined the protocol.
Okay.
We have made sure that all the components are working separately and all the comments are also working
as expected.
And this is the end of this.
Step number four for the final project.