WEBVTT

00:00.080 --> 00:06.530
This is the solution of the activity number five, where you have to send a message from the Arduino

00:06.530 --> 00:07.360
to the Raspberry Pi.

00:07.370 --> 00:13.790
When you press on the push button and then receive a color for one of the LEDs.

00:13.790 --> 00:18.950
So basically one of the color of the RGB and then change that color from the Arduino.

00:19.070 --> 00:25.850
So once again, let's start to do the Arduino code and then we're going to debug the Arduino code with

00:25.850 --> 00:30.620
the sign monitor, and then we're going to write the Python program on the Raspberry Pi.

00:30.620 --> 00:34.370
So let's initialize I'm going to remove this.

00:34.370 --> 00:36.920
Let's initialize the hardware components.

00:36.920 --> 00:43.340
So define button pin, which is pin number seven, and then define.

00:44.110 --> 00:49.420
RGB red pin, which is 11.

00:50.300 --> 00:53.390
And let's do the same for the green and the blue.

00:54.960 --> 00:55.650
Green.

00:57.470 --> 00:59.510
Blue and we have ten.

00:59.780 --> 01:01.760
And we have nine.

01:02.300 --> 01:02.780
Okay.

01:02.780 --> 01:14.750
In the void setup, what we do is spin mode with button pin input.

01:16.330 --> 01:17.650
And then pinmode.

01:18.790 --> 01:23.020
For the red pin to output.

01:23.920 --> 01:24.520
Okay.

01:24.520 --> 01:28.750
And then the same for the green.

01:28.780 --> 01:30.160
The same for.

01:31.160 --> 01:31.720
W.

01:33.320 --> 01:41.480
So I go quite fast with that because we've already done that multiple times and then I'm going to open

01:42.110 --> 01:42.950
style communication.

01:42.950 --> 01:47.510
So style dot begin with, let's keep the same ball rate.

01:49.270 --> 01:50.380
And while.

01:53.020 --> 01:55.600
So let's wait.

01:56.050 --> 01:56.650
All right.

01:57.490 --> 01:59.060
Now, what do we do?

01:59.080 --> 02:02.170
I'm going to also remove that.

02:03.260 --> 02:07.910
So we have two things to do which are, well, actually completely independent.

02:07.940 --> 02:15.770
First, we need to read the purge button, check when it is pressed and send a message to the sale.

02:15.980 --> 02:21.090
The second action is to receive something from the sale and check what command we have.

02:21.110 --> 02:26.110
If it's right green or blue and then apply the command to the Led.

02:26.150 --> 02:31.460
So while there is no particular order to do that, you can debug both independently.

02:31.460 --> 02:34.040
But let's start with let's say the push button.

02:34.070 --> 02:34.610
Okay.

02:34.850 --> 02:38.420
So I'm going to do the Debounce code for the push button once more.

02:38.630 --> 02:52.310
Unsigned long last time button changed, let's say millis and then unsigned long button.

02:53.780 --> 02:54.920
Let's say Debounce.

02:56.330 --> 03:07.790
DeLay is let's put 50 milliseconds another time and then so unsigned long time now is equal to millis.

03:07.790 --> 03:09.350
So we get the current time.

03:10.070 --> 03:16.160
We check if time now minus last time the button has changed.

03:16.610 --> 03:20.210
If this is greater or equal than the debounce delay.

03:21.530 --> 03:34.370
Then we can enter this if and what we can do is do white button state is equal to digital read from

03:34.370 --> 03:36.110
button pin.

03:36.320 --> 03:38.030
Then we will need to.

03:38.510 --> 03:42.860
So we will need to compare the button state with the previous button state.

03:42.860 --> 03:49.160
So I'm going to add here white previous button state.

03:50.000 --> 04:01.430
And just after we do the PIN modes I'm going to do previous button state is equal to digitalwrite from

04:01.430 --> 04:02.570
button pin.

04:03.020 --> 04:11.220
So we have the first state here at the beginning and then we are going to compare Button State.

04:11.240 --> 04:17.990
So if this is different than the previous one, then we need to do two things.

04:17.990 --> 04:23.690
We need to update the last time the button has changed to time now.

04:24.000 --> 04:24.380
Okay.

04:24.380 --> 04:27.560
So then the debounce is going to happen another time.

04:27.560 --> 04:35.630
We're going to do the previous button state is equal to button state.

04:36.640 --> 04:46.030
And then we can check if Button State is equal to hi here because we have a pull down resistor.

04:46.270 --> 04:51.400
Then the button button is pressed.

04:52.130 --> 04:52.580
Okay.

04:52.580 --> 04:56.600
And let's do self dot print ln.

04:57.960 --> 04:58.440
And.

04:58.440 --> 05:00.430
Well, what do we send here?

05:00.450 --> 05:01.440
I'm not going to do.

05:01.440 --> 05:02.530
Button is pressed.

05:02.550 --> 05:02.940
Okay.

05:02.940 --> 05:03.990
This is not a log.

05:03.990 --> 05:08.280
This is a communication from the Arduino to the Raspberry Pi.

05:08.430 --> 05:18.030
So let's say we are going to send button pressed like this and here we're going to receive button pressed.

05:18.060 --> 05:18.630
Okay.

05:19.380 --> 05:23.520
So very important to agree on what you send and what you receive.

05:23.640 --> 05:29.270
And well, I'm first going to just check if that code is working.

05:29.280 --> 05:32.760
So activity five.

05:33.890 --> 05:34.670
Compiling.

05:34.700 --> 05:35.690
Uploading.

05:36.680 --> 05:40.960
Okay, let's open the serial monitor and let's press on the push button.

05:40.970 --> 05:42.920
Button pressed another time.

05:42.920 --> 05:45.200
Button pressed button pressed.

05:45.230 --> 05:47.060
It is working.

05:47.060 --> 05:47.660
Great.

05:47.660 --> 05:51.260
So now let's do the code for the RGB led.

05:51.650 --> 05:54.140
So we have this already.

05:54.260 --> 05:57.890
Well, we have the initialization, which is okay.

05:57.920 --> 06:00.890
Now let's do so here in the void loop.

06:00.890 --> 06:03.650
Make sure you don't make a mistake, okay?

06:03.650 --> 06:07.070
And that you don't put the code in the wrong block of code.

06:07.070 --> 06:09.110
So that's going to be here.

06:09.980 --> 06:11.180
So if.

06:13.860 --> 06:15.700
Dot available.

06:16.800 --> 06:18.540
Greater than zero.

06:18.540 --> 06:20.850
So that doesn't change really.

06:22.410 --> 06:22.980
Then.

06:22.980 --> 06:25.110
So if we have received something.

06:26.200 --> 06:27.400
We're going to do string.

06:30.120 --> 06:33.700
Simply command is equal to self dot.

06:33.720 --> 06:34.680
And guess what?

06:34.710 --> 06:41.820
Read string until backslash n with one single quote.

06:43.200 --> 06:47.790
And then we do if and that's where we are going to check what command we have received.

06:47.790 --> 06:51.110
Because here we can receive three different command.

06:51.120 --> 07:01.020
So if command dot starts with so the command can start with, for example, read like this, then we're

07:01.020 --> 07:11.580
going to process the read command and then else if cmd dot starts with we have green.

07:13.750 --> 07:14.350
Okay.

07:15.340 --> 07:29.530
Else if cmd dot start with blue and then let's do another else like I did before.

07:30.310 --> 07:31.930
Nothing for now.

07:32.560 --> 07:37.840
Or maybe what we could do if we receive an error is just to, for example, power off completely the

07:37.870 --> 07:38.640
RGB entity.

07:38.710 --> 07:39.880
Okay, It's up to you.

07:40.920 --> 07:50.370
So if we receive the red color, what we can do with the command is first to do cmd dot remove and then

07:50.370 --> 07:52.050
zero for.

07:53.310 --> 07:54.420
Okay, we'll remove.

07:54.540 --> 07:56.950
So zero, one, two, three, four.

07:56.970 --> 08:00.900
We'll remove all of that and we are left with the number.

08:00.900 --> 08:02.490
But the number is actually a string.

08:02.490 --> 08:12.840
So we do for example, int red is equal to C and D dot to int like this.

08:12.840 --> 08:26.010
And then analog, right with RGB red pin and with the red number that we got from the command and what

08:26.010 --> 08:26.370
we can do.

08:26.370 --> 08:30.300
The same with every other command.

08:30.300 --> 08:32.640
So cmd dot remove.

08:32.670 --> 08:38.070
But here we don't do zero four because if we do zero four we're going to remove one, two, three,

08:38.070 --> 08:43.560
four and we are going to be left with n colon and the number.

08:43.560 --> 08:47.940
So we do zero one, two, three, four, five, six.

08:47.970 --> 08:48.480
Okay.

08:48.480 --> 08:54.880
So as you can see each command, you will need to process it independently and make sure that it's working.

08:55.330 --> 08:59.590
INT green is equal to cmd dot to INT.

08:59.620 --> 09:04.840
This doesn't change in analog, so let's use analog write the same.

09:04.840 --> 09:05.470
But.

09:06.890 --> 09:07.820
We've.

09:09.580 --> 09:11.440
Green and.

09:12.970 --> 09:13.510
Green.

09:15.520 --> 09:17.020
Okay.

09:17.170 --> 09:18.640
And then for the blue.

09:20.300 --> 09:24.860
We do cmd dot remove and how many do we need to remove?

09:24.890 --> 09:27.050
One, 2345.

09:27.050 --> 09:35.330
So zero five and then int blue is equal to cmd dot to int.

09:35.330 --> 09:42.740
And let's do analog right with the blue pin and the blue color.

09:43.950 --> 09:44.250
Okay.

09:44.250 --> 09:49.590
And now we can try this code actually on the Arduino to check if it's correctly working and well, if

09:49.590 --> 09:55.680
you want an additional debug step because maybe well, let's say you want to send the right or the green

09:55.680 --> 09:57.270
or the blue command and it doesn't work.

09:57.300 --> 10:00.300
You don't necessarily know why it doesn't work.

10:00.300 --> 10:08.220
So what you can do, for example, let's say here Serial.println, for example, red, so we can just

10:08.220 --> 10:11.490
use this when we debug the Arduino with the serial monitor.

10:11.490 --> 10:13.230
And then of course that's very important.

10:13.260 --> 10:18.450
Don't forget to remove any log that you have put first for the Arduino side.

10:18.450 --> 10:21.210
So let's upload the code to the Arduino.

10:23.360 --> 10:27.950
Let's open the serial monitor so we still have the push button.

10:28.190 --> 10:30.710
That will show button pressed.

10:30.710 --> 10:34.490
And then let's do red colon 200.

10:34.820 --> 10:38.060
You can see it is correctly working and we have 200.

10:38.090 --> 10:40.010
Okay, let's do green.

10:41.060 --> 10:42.170
100.

10:42.590 --> 10:42.920
Okay.

10:42.920 --> 10:46.850
We have a different color in blue and 50.

10:47.510 --> 10:48.610
Let's do blue.

10:48.620 --> 10:49.970
Actually, 200.

10:50.810 --> 10:51.260
Okay.

10:51.260 --> 10:52.550
And maybe red.

10:52.730 --> 10:53.690
Zero.

10:54.020 --> 10:54.490
Right.

10:54.560 --> 10:56.510
And green.

10:57.350 --> 10:58.160
Zero.

10:59.240 --> 11:01.070
Blue, 200.

11:01.100 --> 11:03.260
Well, there is something wrong with blue.

11:05.170 --> 11:11.410
And well, actually, I have checked that I have used I don't know why, but 19 for the blueprint.

11:11.410 --> 11:13.000
So of course that doesn't work.

11:13.000 --> 11:14.830
So you can see here that didn't work.

11:14.830 --> 11:16.540
We didn't see the command blue change.

11:16.540 --> 11:20.650
And then it helped us to actually see that we have an error in our code.

11:20.650 --> 11:23.350
So now let's run that again.

11:25.780 --> 11:28.420
I'm going to open the side monitor again and just.

11:29.520 --> 11:36.360
Test blue with two 100 and well, the blue is working great.

11:36.630 --> 11:39.030
So now I can just close that.

11:39.030 --> 11:41.630
I could also do a print with the blue.

11:41.640 --> 11:41.900
Okay.

11:41.940 --> 11:45.930
To make sure that we receive from sale and I'm going to remove this is very important.

11:45.930 --> 11:51.930
Any log that I have put which have nothing to do with the cell communication with the Raspberry Pi.

11:52.290 --> 11:52.650
Okay.

11:52.650 --> 11:56.040
So now the Arduino code is working.

11:56.040 --> 12:00.900
I'm going to upload also the new code, of course, without the dot print.

12:00.950 --> 12:04.830
Ellen Okay, The Arduino side is working.

12:04.920 --> 12:07.890
Let's go to the Raspberry Pi side.

12:08.460 --> 12:15.420
So first of all, we need to initialize serial and everything, but I'm going to use the code from the

12:15.420 --> 12:16.770
previous activity.

12:17.190 --> 12:21.150
So select everything so you can do this by yourself.

12:21.150 --> 12:25.320
But I'm going to do this so it goes a bit faster.

12:25.320 --> 12:30.370
And anyway, we have already done that multiple times.

12:30.370 --> 12:31.270
So what?

12:31.270 --> 12:33.310
I'm going to remove that also.

12:34.830 --> 12:44.190
You adjust the input initialization and then the infinite loop with the time.sleep very important.

12:44.280 --> 12:51.900
And I'm going to so I'm going to import random here and let's remove that.

12:52.830 --> 12:56.840
Okay, so what do we do on the Raspberry Pi?

12:56.850 --> 13:03.420
Well, we don't send anything without receiving anything, so we first need to check that we have received

13:03.420 --> 13:04.080
something.

13:04.080 --> 13:14.460
So in the while true if say, dot in waiting is greater than zero again without parentheses, then we

13:14.460 --> 13:16.440
do, let's say message.

13:18.040 --> 13:19.120
Is equal to.

13:19.540 --> 13:22.660
And from this age we could also do MSG, for example.

13:22.900 --> 13:25.330
That's the same as using CMD for command.

13:25.360 --> 13:29.290
Okay, that's pretty explicit and that's something you're going to use very often.

13:29.290 --> 13:31.630
So that's not a big deal.

13:31.870 --> 13:35.680
And so set dot readline dot.

13:37.530 --> 13:38.430
Decode.

13:39.560 --> 13:41.360
UTF eight.

13:42.760 --> 13:44.310
Dot s trick.

13:46.390 --> 13:46.660
Okay.

13:46.660 --> 13:55.360
And then what we do is we check if message is equal to is equal to what is equal to button pressed.

13:55.720 --> 13:56.260
Okay.

13:56.590 --> 13:57.940
We send button pressed.

13:57.940 --> 14:00.130
We check if we receive button pressed.

14:00.160 --> 14:05.620
If we have received button pressed, then we are going to process the action.

14:05.620 --> 14:08.890
And what is the action is to choose a random color.

14:09.160 --> 14:13.180
So right green and blue and then the random number for that color.

14:13.690 --> 14:25.780
Okay, so let's do D is equal to random dot rand int with zero and to.

14:27.780 --> 14:39.630
And then the number is equal to random dot rand, int from 0 to 255.

14:40.500 --> 14:40.770
Okay.

14:40.770 --> 14:44.100
And then we're going to construct the string that we're going to send.

14:44.130 --> 14:44.640
Okay.

14:44.940 --> 14:50.040
So the command and so let's start with CMD is equal to an empty string.

14:50.040 --> 15:02.400
And then if ID is equal to one, we do cmd plus red.

15:03.440 --> 15:04.720
And then let's do L.

15:04.790 --> 15:09.110
If Z is equal to two, we do cmd.

15:10.880 --> 15:13.130
Less green.

15:14.640 --> 15:21.210
And then let's do ls cmd plus blue.

15:22.110 --> 15:29.460
So we have the first part of the command and then we add So cmd plus.

15:30.550 --> 15:33.280
Number, but actually as a string.

15:33.280 --> 15:39.370
So we cast the number as a string and then cmd plus.

15:40.260 --> 15:41.460
Backslash.

15:42.060 --> 15:42.600
N.

15:43.800 --> 15:50.580
Okay, so we start from an empty string, then we construct it with first the command name red, green

15:50.580 --> 15:58.050
and blue, and then the number we want to read that we're going to get here and then backslash n for

15:58.050 --> 16:00.120
the read string until with the backslash.

16:00.120 --> 16:01.380
N Okay.

16:01.380 --> 16:05.190
And then we can do set dot write.

16:06.460 --> 16:09.670
CMD dot encode UTF.

16:10.800 --> 16:13.410
Eight like this.

16:13.980 --> 16:17.310
And let's see if that works.

16:17.340 --> 16:21.760
Maybe we can add some prints here because it's easier to debug on Python.

16:21.780 --> 16:22.950
We can add some prints.

16:22.950 --> 16:25.350
So let's say here print.

16:27.030 --> 16:28.740
Button has been pressed.

16:31.010 --> 16:31.310
Okay.

16:31.310 --> 16:40.490
And then maybe print, send, command plus command.

16:41.240 --> 16:44.450
So we're gonna make sure that we send the correct command.

16:44.480 --> 16:44.930
Okay.

16:44.930 --> 16:47.170
Because we know that the Arduino side is working.

16:47.180 --> 16:53.540
So if we send something from the Raspberry Pi to the Arduino, and if it doesn't work, well, we probably

16:53.540 --> 16:56.630
know that this is because we sent the wrong thing.

16:56.630 --> 17:00.380
So if we just print the thing, it's easier to debug.

17:00.530 --> 17:06.170
Let's save this as activity five.

17:07.790 --> 17:12.020
And so the Arduino is already uploaded.

17:12.020 --> 17:16.220
Let's run it on the Raspberry Pi.

17:18.270 --> 17:18.600
Okay.

17:18.600 --> 17:19.770
You can see cereal.

17:19.800 --> 17:20.130
Okay.

17:20.280 --> 17:20.850
And now?

17:20.850 --> 17:23.220
Well, what I need to do is I need to press on the push button and.

17:23.220 --> 17:24.000
Let's see.

17:25.330 --> 17:30.520
Okay, we have an error and well, actually okay.

17:30.550 --> 17:34.890
The module random has no attribute rand int simply because.

17:34.900 --> 17:38.770
Well I have to use a lowercase here.

17:39.580 --> 17:39.900
Okay.

17:39.910 --> 17:43.540
I have used uppercase but it is a lowercase.

17:43.540 --> 17:44.820
I made a mistake.

17:44.830 --> 17:46.630
So now let's run again.

17:50.330 --> 17:50.910
Sale.

17:50.990 --> 17:56.810
Okay, let's press on the push button and you can see button has been pressed.

17:56.810 --> 17:59.960
Send command red 125.

17:59.990 --> 18:00.980
I press again.

18:01.920 --> 18:05.690
Send comments or write again here.

18:05.700 --> 18:07.100
Write again with 30.

18:07.110 --> 18:10.470
And here you can see Blue 237.

18:11.370 --> 18:11.880
Green.

18:11.880 --> 18:12.840
36.

18:13.170 --> 18:14.460
Green 200.

18:14.550 --> 18:19.440
Okay, so every time we change one color, 174 for blue.

18:19.470 --> 18:20.280
ET cetera.

18:20.310 --> 18:21.110
ET cetera.

18:21.120 --> 18:23.850
So now I press control C okay.

18:23.850 --> 18:26.190
And you can see adding some.

18:27.890 --> 18:34.700
A print here on the Python side is very, very useful to see what you send and then you can check that

18:34.700 --> 18:41.030
it's correctly doing what you want to do on the Arduino side and that's the end of this activity.
