WEBVTT

00:00.110 --> 00:03.440
This is step number four of the project.

00:03.440 --> 00:10.670
And in this step, we are going to get all the input commands from Serial on the Arduino side and then

00:10.670 --> 00:16.400
make sure that those commands are correctly executed on the hardware components we have connected to

00:16.400 --> 00:17.000
the Arduino.

00:17.650 --> 00:21.610
So we have already initialized all the pins.

00:21.760 --> 00:23.590
Everything we can.

00:23.590 --> 00:25.510
So we have initialized the communication.

00:25.510 --> 00:31.160
We can send also the button pressed command message when we press on the button.

00:31.180 --> 00:37.930
Now what we can do is in this void loop, well, we have this first action, which is to send something

00:37.960 --> 00:39.160
to the side.

00:39.160 --> 00:41.260
And you can see we don't have any delay here.

00:41.260 --> 00:42.310
So it's not blocking.

00:42.310 --> 00:43.960
It's going to be very fast.

00:43.990 --> 00:46.330
We can add after this.

00:47.310 --> 00:50.850
The code to check from Serial if we have received something.

00:50.850 --> 00:54.720
So if serial dot available.

00:56.560 --> 01:07.630
Greater than zero, then we can get the command with let's store as a string cmd is equal to serial

01:07.630 --> 01:15.040
dot read string until backslash n with just one single quote character.

01:15.070 --> 01:20.980
Once we have this command, well, let's see what kind of command we received.

01:20.980 --> 01:24.310
So if cmd and now.

01:24.310 --> 01:27.790
Well, I'm going to come back to the protocol we have defined here.

01:27.820 --> 01:30.640
You can see we have all those commands to handle.

01:30.940 --> 01:36.040
So open door, close door print, text, play buzzer and set led.

01:36.160 --> 01:38.890
So let's write the code for all of that.

01:39.520 --> 01:47.080
So if the command is equal to open door, okay, so if it's equal exactly to open door, then we can

01:47.080 --> 01:48.130
do something.

01:48.130 --> 01:55.210
I'm just going to write all the else if first we have close door.

01:57.000 --> 01:57.540
Okay.

01:57.540 --> 01:59.730
And we're going to close the door with the servo motor.

02:00.300 --> 02:02.700
Else if command.

02:02.730 --> 02:03.510
So.

02:05.100 --> 02:05.460
Here.

02:05.460 --> 02:06.840
This is an exact comment here.

02:06.840 --> 02:13.320
This is a comment that starts with this and then we have some text are some variables we want to extract.

02:13.320 --> 02:15.660
So for those comments, I'm going to do comment.

02:15.690 --> 02:25.920
Dot starts with print, text and colon and two parentheses to close.

02:26.820 --> 02:43.620
Then we have so else if comment starts with we have play buzzer colon and the last one comment starts

02:43.770 --> 02:48.090
with set colon.

02:49.200 --> 02:49.980
Okay.

02:49.980 --> 02:54.510
So as you can see, if you add more comments, it doesn't get more complex.

02:54.510 --> 02:57.510
You just have one more elseif and that's it.

02:57.540 --> 03:01.380
Your program is not going to be more complex and let's do else.

03:01.380 --> 03:04.900
And here I'm going to give you another best practice.

03:04.900 --> 03:06.970
Well, let's say you have received a comment.

03:06.970 --> 03:12.970
So something that you can read from Serial and it doesn't correspond to any of that, which means you

03:12.970 --> 03:15.460
have probably received some garbage comment.

03:15.460 --> 03:20.980
And in that case, what you can choose to do is to also clear the input buffer.

03:21.160 --> 03:21.300
Okay?

03:21.340 --> 03:26.890
Because if you have received something that is wrong, maybe you still have some bytes that have been

03:26.890 --> 03:30.490
received and that are in the buffer that you're going to read next.

03:30.490 --> 03:38.980
And so what we can do is do while dot available greater than zero, let's actually put some parentheses

03:39.700 --> 03:42.520
and we do serial dot read.

03:42.520 --> 03:49.180
So the same thing we did here to clear the input buffer we do it here in the else.

03:49.570 --> 03:51.790
Okay so the else you could choose different behaviors.

03:51.790 --> 03:54.220
You could find some way to print some errors.

03:54.220 --> 04:00.370
So on the LCD screen or maybe send back a comment to the Raspberry Pi that's going to process it and

04:00.370 --> 04:03.640
print it that you have sent an error here.

04:03.640 --> 04:09.250
We just choose to clear the input buffer so that the next time we enter the while loop, we give a new

04:09.250 --> 04:12.280
chance for the Raspberry Pi to send a correct comment.

04:13.950 --> 04:14.580
All right.

04:14.880 --> 04:20.420
Let's also save this as the step number four.

04:20.430 --> 04:21.780
So don't forget.

04:23.380 --> 04:23.760
Okay.

04:23.770 --> 04:24.610
Save.

04:27.220 --> 04:28.480
Let's just verify.

04:28.510 --> 04:31.630
Let's just compile to see that we have the correct structure.

04:31.660 --> 04:32.410
Don't compile.

04:32.440 --> 04:34.840
Okay, so the structure is correct.

04:34.870 --> 04:37.240
Now let's do the commands one by one.

04:37.240 --> 04:39.280
So we have already initialized everything.

04:39.280 --> 04:41.110
That's going to be quite easy.

04:41.320 --> 04:47.590
So if we have the open door command, what we do is simply to send the servo to the position which we

04:47.590 --> 04:51.400
have defined here, the open door position.

04:51.550 --> 04:58.990
So I'm going to do simply servo, dot, write, open the position.

04:59.860 --> 05:02.380
And if we receive the close door.

05:03.710 --> 05:04.630
Command.

05:04.790 --> 05:06.050
We just do.

05:07.680 --> 05:08.180
So.

05:08.190 --> 05:10.280
All right.

05:10.290 --> 05:13.240
We've said we'll close the position.

05:13.300 --> 05:13.470
Okay.

05:13.470 --> 05:16.800
Those are actually the simplest commands to execute.

05:17.460 --> 05:20.760
And well, here what we do is we directly send the command.

05:20.790 --> 05:21.420
Okay.

05:21.630 --> 05:22.440
To the server.

05:22.470 --> 05:27.660
What you could do as an improvement and not gonna do that here, but you can do that as an improvement

05:27.690 --> 05:33.660
is to make the server go a little bit slower with a non-blocking delay mechanism in the void loop.

05:33.660 --> 05:40.050
So you could have another part of the code here that's going to compare the current angle of the server

05:40.200 --> 05:44.220
with the target angle and check if it needs to move a bit.

05:44.220 --> 05:48.810
And every time wait for example, ten milliseconds before it moves for one degree.

05:48.840 --> 05:49.470
Okay.

05:49.740 --> 05:55.020
But here we directly send the command, go to open position, go to closed position.

05:55.560 --> 05:56.040
All right.

05:56.070 --> 05:59.100
Now, when we receive print text, what we do.

05:59.100 --> 06:02.040
So the first thing we do is comment dot remove.

06:02.640 --> 06:04.650
Let's remove the print text.

06:04.650 --> 06:13.810
So we have zero, one, two, three, four, five, six, seven, eight, nine, ten and 11.

06:13.810 --> 06:24.760
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

06:25.060 --> 06:29.200
to remove anything LCD dot set cursor.

06:30.120 --> 06:36.750
Zero and zero and then LCD dot print cmd.

06:37.350 --> 06:42.720
I'm going to add an improvement here because, well, you're going to just print on the first line and

06:42.720 --> 06:48.330
you have well, you have 16 character on the first line, 16 character on the second line.

06:48.330 --> 06:50.250
And here we just write on the first line.

06:50.250 --> 06:54.350
So everything after 16 character is going to be truncated.

06:54.360 --> 06:59.700
What we could do is that if you receive a string that is more than 16 characters, you bring the following

06:59.730 --> 07:00.690
on the second line.

07:00.690 --> 07:03.180
Okay, so you can use all the 32 characters.

07:03.180 --> 07:12.720
And so what I'm going to do is I'm going to do string line one is equal to comment dot substring.

07:13.200 --> 07:25.920
I'm going to take from 0 to 16 and then string line two is equal to comment dot substring from 16 to

07:25.950 --> 07:27.570
the end of the string.

07:27.600 --> 07:30.060
Okay, so I still clear the screen.

07:30.060 --> 07:35.220
I set the cursor to zero zero and then I print not the comment but print line one.

07:36.780 --> 07:47.010
And then do LCD dot set cursor zero one for the second line LCD dot print line two.

07:47.850 --> 07:48.360
Great.

07:48.360 --> 07:50.640
So that's a bit improved here.

07:50.790 --> 07:52.050
Still has an improvement.

07:52.050 --> 07:58.170
What you can do is, for example, make the text rotate if you have more than 32 characters, because

07:58.170 --> 08:00.900
here after 32 characters, it's going to be truncated.

08:01.320 --> 08:04.110
But let's keep things simple for now.

08:04.410 --> 08:06.300
Then we have the play buzzer.

08:06.300 --> 08:13.260
So with the play buzzer, we're going to do first comment dot remove and let's count zero one, two,

08:13.260 --> 08:18.120
three, four, five, six, seven, eight, nine, ten, 11, 12.

08:18.540 --> 08:29.460
So let's remove 12 And then what we are going to get is so frequency, comma, duration, but we don't

08:29.460 --> 08:35.010
know exactly how many characters this is going to take and how many characters this is going to take.

08:35.010 --> 08:42.160
So what we can do is we can focus on this comma, we can find the index of that comma and then take

08:42.160 --> 08:45.520
everything before the comma and everything after the comma.

08:45.640 --> 08:55.210
And so I'm going to do int comma index is equal to and you have a function that is so on the string

08:55.630 --> 09:05.050
index off like this and you give a character with single quotes like that, it's going to search for

09:05.050 --> 09:08.110
the comma and return the index of the comma.

09:08.440 --> 09:11.560
So of course you need to have a comma in the string.

09:11.710 --> 09:25.720
And once we have gotten this comma index, now we can do int frequency is equal to comment dot substring

09:25.720 --> 09:29.620
from zero to the comma index.

09:31.720 --> 09:40.020
And we are going to also do dot to int to convert this string to an integer and then int duration is

09:40.020 --> 09:47.490
equal to comment dot substring comma index plus one.

09:48.300 --> 09:48.530
Okay.

09:48.540 --> 09:52.590
Because we don't want to actually get the comma in the number.

09:52.620 --> 09:53.160
Okay.

09:53.370 --> 09:59.760
So here we're going to go from zero to the comma index, but this is not going to go until the comma

09:59.790 --> 10:01.710
is going to go just one before.

10:01.950 --> 10:09.600
And when we start again, we start just after the comma to int and now we have the frequency and the

10:09.600 --> 10:10.080
duration.

10:10.080 --> 10:21.690
We can do tone buzzer pin with frequency and duration and that's going to play the tone on the buzzer.

10:21.960 --> 10:22.350
All right.

10:22.350 --> 10:26.640
The last command is settled and so we've already done that before.

10:26.640 --> 10:38.080
So first of all, the command dot remove zero to let's count zero one, two, three, four, 5678 0

10:38.080 --> 10:38.980
to 8.

10:39.160 --> 10:46.270
And then we have something like this and we need to get red, green and blue.

10:46.270 --> 11:03.970
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

11:03.970 --> 11:12.400
equal to so we have 01234567.

11:12.790 --> 11:20.410
So 4 to 7 command dot substring 4 to 7 to int.

11:22.010 --> 11:32.450
And then int blue is equal to comment dot sub string and we're going to start at 01234, five, six,

11:32.450 --> 11:34.460
seven, eight to the end.

11:36.180 --> 11:41.010
To eat now that we have the red, green and blue.

11:41.010 --> 11:42.120
So let's remove that comment.

11:42.150 --> 11:46.920
Now we just do analog, right?

11:47.400 --> 11:49.140
RGB red.

11:50.290 --> 11:52.660
PIN we've read.

11:52.660 --> 11:56.680
And then the same for green.

11:56.680 --> 11:58.090
And the same for blue.

11:59.320 --> 12:01.030
Green and blue.

12:03.380 --> 12:09.810
You can see now our code is about 100 lines, but it's still quite clear.

12:09.830 --> 12:10.130
Okay.

12:10.130 --> 12:14.360
We have first initialization, so some defines and some objects.

12:14.360 --> 12:17.860
We need to create some global variables in the setup.

12:17.870 --> 12:24.020
We start communication, we initialize all the pins, all the components.

12:24.050 --> 12:26.090
Okay, We do some setup actions.

12:26.090 --> 12:32.840
For example, put the servo in a certain position, print some text on an LCD, and then in the void

12:32.840 --> 12:39.080
loop we have first action, which is to check the push button and send the message to Syria.

12:39.200 --> 12:43.580
And second action is to process the data we receive from Serial.

12:43.580 --> 12:47.870
And for each command we have a different block of code.

12:48.440 --> 12:54.470
And well, if you have followed all the previous lessons and activities, you can see that the structure

12:54.470 --> 12:56.720
of the Arduino program is almost the same.

12:56.750 --> 12:56.990
Okay?

12:56.990 --> 13:01.160
That structure that we have here is going to be almost the same in the void loop.

13:01.160 --> 13:05.090
You have all the outputs, all the inputs, and well, that's pretty much it.

13:05.090 --> 13:09.050
If you know how to do this, you know how to do another project.

13:09.050 --> 13:13.310
And you can see here there is no relation between any of the components.

13:13.340 --> 13:15.440
Okay, this is an independent action.

13:15.440 --> 13:17.000
This is an independent action.

13:17.000 --> 13:20.300
Also this one, that one, that one, everything is independent.

13:20.330 --> 13:25.760
The glue or the logic of the application will be from the Raspberry Pi.

13:25.790 --> 13:31.250
So now what we can do is we can test this code actually, because how can you know that it's working?

13:31.250 --> 13:32.630
Well, you have to test it.

13:32.660 --> 13:37.520
Let's upload it to the Arduino.

13:42.880 --> 13:43.150
Okay.

13:43.150 --> 13:47.890
So we have starting on the LCD screen and now let's open the serial monitor.

13:48.250 --> 13:50.500
So let's check that we still have.

13:50.770 --> 13:51.790
Okay, starting again.

13:51.790 --> 13:52.510
Let's check that.

13:52.510 --> 13:54.040
We still have the button pressed.

13:54.040 --> 13:54.670
Okay.

13:55.390 --> 13:59.110
And then I'm going to do print text.

13:59.470 --> 14:00.880
Let's say hello.

14:01.240 --> 14:02.380
It's working.

14:02.380 --> 14:10.060
Let's print some plus some more characters.

14:11.470 --> 14:12.070
Okay.

14:12.100 --> 14:12.430
Hello.

14:12.430 --> 14:16.750
Plus some more characters on the LCD screen and anything that I'm going to write.

14:16.750 --> 14:19.720
So here I use actually 31 characters.

14:20.260 --> 14:24.400
Everything I'm going to write after 32 characters is going to be truncated.

14:24.400 --> 14:26.140
But now you can see that it's working.

14:26.350 --> 14:28.900
So print text is working.

14:29.500 --> 14:31.270
What else do we have?

14:32.260 --> 14:32.970
Open door.

14:32.980 --> 14:34.180
Let's try open door.

14:34.660 --> 14:36.340
So actually the door is closed.

14:36.340 --> 14:38.740
Let's do open door and what?

14:40.990 --> 14:41.680
It's working.

14:41.680 --> 14:42.550
As you can see here.

14:42.550 --> 14:49.240
I had to move a little bit the power, just make sure the power is correctly plugged here.

14:49.240 --> 14:55.600
And this is why this is a limitation of a circuit that you do with a breadboard and some wires like

14:55.600 --> 15:01.690
this is that, of course, the connections may be not 100% stable.

15:01.870 --> 15:02.440
Okay.

15:02.440 --> 15:08.050
So of course, what I recommend is if you try to do this project on a real door is that, well, first

15:08.050 --> 15:14.530
you use a different power supply, use an external power supply for the servomotor, and then you also,

15:14.650 --> 15:16.960
of course, you will need to solder the component.

15:16.960 --> 15:17.200
Okay.

15:17.200 --> 15:18.760
But that's another step.

15:18.760 --> 15:23.980
And just here, that was just here to show you that, well, we have some limitations actually with

15:23.980 --> 15:29.740
a breadboard because all those wires will at the end you will have something that is not working because

15:29.740 --> 15:31.510
something is not correctly plugged.

15:31.870 --> 15:32.560
But here.

15:32.560 --> 15:34.630
So we have open door that's working.

15:34.630 --> 15:36.880
Let's do close door.

15:38.290 --> 15:41.380
And okay, it's working.

15:41.380 --> 15:43.450
So we have closed the open door.

15:43.480 --> 15:45.160
Currently working.

15:45.970 --> 15:46.540
Open door.

15:46.540 --> 15:47.800
Close door print Text.

15:47.800 --> 15:48.670
Play buzzer.

15:50.110 --> 15:52.330
Let's actually do something with that.

15:52.600 --> 15:53.710
Play Buzzer.

15:53.890 --> 16:01.300
Let's send, let's say 400 frequency and 500 milliseconds.

16:02.140 --> 16:03.100
It's working.

16:03.100 --> 16:08.920
Let's send something else like 301,000.

16:10.090 --> 16:11.040
Okay.

16:11.050 --> 16:13.060
It's correctly working and set.

16:14.650 --> 16:25.330
Let's put the right full on 000 and 000 and well, actually, we have an error and it's more like white

16:25.330 --> 16:25.960
and.

16:25.960 --> 16:34.060
Well, that's the of course if I put the red, which was 255 to each one to the right, the green and

16:34.060 --> 16:35.980
the blue, that's going to be white, of course.

16:35.980 --> 16:42.850
So I need to change that to use the green here and to use the blue here.

16:42.850 --> 16:44.950
And let's upload again.

16:50.450 --> 16:50.900
Okay.

16:50.930 --> 16:56.960
Set led to 55 000000.

16:56.990 --> 16:58.040
We have red.

16:58.070 --> 17:00.020
Let's do now.

17:00.200 --> 17:05.390
0002 55.

17:05.420 --> 17:10.520
It should be green and then 000 and 255 at the end.

17:10.550 --> 17:14.540
It should be blue and yes, it is correctly working.

17:15.350 --> 17:15.920
All right.

17:15.920 --> 17:20.180
So now the Arduino code should be done for the application.

17:20.180 --> 17:23.030
So it doesn't mean you don't have to come back to it later on.

17:23.030 --> 17:28.460
Maybe you will need to change or to tweak something because when you program on a Raspberry Pi, you're

17:28.460 --> 17:33.290
going to maybe realize that some stuff on the Arduino should be handled differently.

17:33.410 --> 17:37.970
But still, we should be pretty safe because we already have defined the protocol.

17:38.210 --> 17:38.630
Okay.

17:38.630 --> 17:45.110
We have made sure that all the components are working separately and all the comments are also working

17:45.110 --> 17:46.250
as expected.

17:46.250 --> 17:47.990
And this is the end of this.

17:47.990 --> 17:50.820
Step number four for the final project.
