WEBVTT

00:00.080 --> 00:06.590
In this new activity, you are going to do a bi directional communication between the Arduino and the

00:06.590 --> 00:07.520
Raspberry Pi.

00:07.550 --> 00:13.070
And your challenge is that when you press on the push button on the Arduino side, so you're going to

00:13.070 --> 00:14.930
read the push button.

00:14.930 --> 00:19.550
When you detect that the push button is pressed, you send a message to the Raspberry Pi.

00:19.820 --> 00:27.170
The Raspberry Pi will receive that message, will check that it corresponds to a button pressed message

00:27.170 --> 00:29.960
and then it will choose a random led.

00:30.080 --> 00:36.710
So the red, the green or the blue, and it will choose a random number between 0 and 255, and it will

00:36.710 --> 00:38.750
send that back to the Arduino.

00:38.750 --> 00:44.180
And then the Arduino is going to process this command and power on accordingly.

00:44.210 --> 00:50.510
One of the color of the RGB led with the number that we have received with Analogwrite.

00:50.540 --> 00:50.840
Okay.

00:50.840 --> 00:54.350
So on the Arduino side, you're going to have two actions.

00:54.350 --> 00:57.140
The first is to read the push button.

00:57.170 --> 00:59.720
The second is to read the sale.

00:59.720 --> 01:02.850
So you read the push button, you send the message to the RPI.

01:02.880 --> 01:09.180
So the Raspberry Pi and here, when I'm not going to tell you what exact message you need to send,

01:09.180 --> 01:16.380
okay, I'll leave that up to you so you can practice on defining a good protocol for sending and receiving

01:16.380 --> 01:19.040
data and then, well, for the lead.

01:19.050 --> 01:22.770
I have given you an example here because that's maybe a little bit more complex.

01:22.770 --> 01:24.180
So I give you an example.

01:24.180 --> 01:30.270
First, what you can do, for example, is if you send a command to power on so to change the color

01:30.270 --> 01:36.900
on the red led, then you can send something that starts with red colon and then the color, for example,

01:37.380 --> 01:38.460
56 like this.

01:38.460 --> 01:44.220
Okay, If you send something with the green, then you're going to do green colon color, okay?

01:44.220 --> 01:46.020
And blue color, etcetera.

01:46.020 --> 01:47.820
And so you can send that from the Raspberry Pi.

01:47.820 --> 01:51.540
And then on the Arduino, what you can do instead of comparing.

01:51.540 --> 01:57.180
So the equality with string, which you can't do because you don't actually know what number you're

01:57.180 --> 01:58.920
going to get, this is valuable.

01:58.920 --> 02:05.010
You can do comments, starts with and then the name of the command colon.

02:05.250 --> 02:05.520
Okay.

02:05.520 --> 02:11.700
If this starts with this, then you're going to process this and how to extract the number that is here.

02:11.730 --> 02:14.160
Well, you can first do command dot remove.

02:14.160 --> 02:19.050
So this is going to remove the first here, the first part of the string.

02:19.050 --> 02:22.680
So you have index zero, one, two, three and four.

02:22.680 --> 02:28.350
So this is going to remove completely this part and you're just left with the number, but the number

02:28.350 --> 02:29.430
is still a string.

02:29.430 --> 02:29.820
Okay?

02:29.820 --> 02:30.840
Because you receive a string.

02:30.840 --> 02:32.250
So the number is still a string.

02:32.250 --> 02:37.890
So then you do command dot to INT and the result of that is going to go inside an integer, right?

02:37.890 --> 02:40.410
That you can use then with analogwrite.

02:40.440 --> 02:40.710
Okay.

02:40.710 --> 02:43.290
And you can do the same with green and the same with blue.

02:43.290 --> 02:45.420
Now on the Raspberry Pi side.

02:45.420 --> 02:49.520
So you're going to initialize a serial and then you're going to do a while.

02:49.560 --> 02:51.390
True like we did before.

02:51.510 --> 02:55.380
And you're going to check if you receive some comment from the Arduino.

02:55.380 --> 02:58.410
If you receive something, you check if the button has been pressed.

02:58.410 --> 03:01.800
If yes, then you're going to choose a random number.

03:01.800 --> 03:04.080
So between 0 and 2.

03:04.470 --> 03:04.830
Okay.

03:04.830 --> 03:10.440
So that you can have zero 1 or 2, which means red, green or blue, and then you're going to choose

03:10.440 --> 03:17.370
another number with zero and 255, you are going to construct a string.

03:17.400 --> 03:17.670
Okay?

03:17.670 --> 03:20.700
That corresponds to the command you're going to send to the Arduino.

03:20.700 --> 03:27.300
So to choose a random number, you can well just import random and then do random dot rand int like

03:27.300 --> 03:29.640
this with the minimum and the maximum.

03:29.640 --> 03:29.940
Okay.

03:29.940 --> 03:36.240
And this activity is actually a good example of how to use the combination of Raspberry Pi and Arduino

03:36.600 --> 03:41.130
for a complete application where the Arduino is basically the muscle of the application.

03:41.130 --> 03:41.550
Okay.

03:41.550 --> 03:46.920
It's going to read from sensor, it's going to actuate some sensor, but it's not going to decide what

03:46.920 --> 03:47.340
to do.

03:47.340 --> 03:50.940
And then the Raspberry Pi here decides on what to do.

03:50.940 --> 03:54.240
So the Raspberry Pi is actually the brain of the application.

03:54.240 --> 03:56.280
So you have the brain and the muscle.

03:56.280 --> 04:02.100
And basically that's how things work in many different projects with those two balls.

04:02.310 --> 04:06.660
Okay, so now I'm going to see you in the next video for The Solution.
