In this new activity, you are going to do a bi directional communication between the Arduino and the
Raspberry Pi.
And your challenge is that when you press on the push button on the Arduino side, so you're going to
read the push button.
When you detect that the push button is pressed, you send a message to the Raspberry Pi.
The Raspberry Pi will receive that message, will check that it corresponds to a button pressed message
and then it will choose a random led.
So the red, the green or the blue, and it will choose a random number between 0 and 255, and it will
send that back to the Arduino.
And then the Arduino is going to process this command and power on accordingly.
One of the color of the RGB led with the number that we have received with Analogwrite.
Okay.
So on the Arduino side, you're going to have two actions.
The first is to read the push button.
The second is to read the sale.
So you read the push button, you send the message to the RPI.
So the Raspberry Pi and here, when I'm not going to tell you what exact message you need to send,
okay, I'll leave that up to you so you can practice on defining a good protocol for sending and receiving
data and then, well, for the lead.
I have given you an example here because that's maybe a little bit more complex.
So I give you an example.
First, what you can do, for example, is if you send a command to power on so to change the color
on the red led, then you can send something that starts with red colon and then the color, for example,
56 like this.
Okay, If you send something with the green, then you're going to do green colon color, okay?
And blue color, etcetera.
And so you can send that from the Raspberry Pi.
And then on the Arduino, what you can do instead of comparing.
So the equality with string, which you can't do because you don't actually know what number you're
going to get, this is valuable.
You can do comments, starts with and then the name of the command colon.
Okay.
If this starts with this, then you're going to process this and how to extract the number that is here.
Well, you can first do command dot remove.
So this is going to remove the first here, the first part of the string.
So you have index zero, one, two, three and four.
So this is going to remove completely this part and you're just left with the number, but the number
is still a string.
Okay?
Because you receive a string.
So the number is still a string.
So then you do command dot to INT and the result of that is going to go inside an integer, right?
That you can use then with analogwrite.
Okay.
And you can do the same with green and the same with blue.
Now on the Raspberry Pi side.
So you're going to initialize a serial and then you're going to do a while.
True like we did before.
And you're going to check if you receive some comment from the Arduino.
If you receive something, you check if the button has been pressed.
If yes, then you're going to choose a random number.
So between 0 and 2.
Okay.
So that you can have zero 1 or 2, which means red, green or blue, and then you're going to choose
another number with zero and 255, you are going to construct a string.
Okay?
That corresponds to the command you're going to send to the Arduino.
So to choose a random number, you can well just import random and then do random dot rand int like
this with the minimum and the maximum.
Okay.
And this activity is actually a good example of how to use the combination of Raspberry Pi and Arduino
for a complete application where the Arduino is basically the muscle of the application.
Okay.
It's going to read from sensor, it's going to actuate some sensor, but it's not going to decide what
to do.
And then the Raspberry Pi here decides on what to do.
So the Raspberry Pi is actually the brain of the application.
So you have the brain and the muscle.
And basically that's how things work in many different projects with those two balls.
Okay, so now I'm going to see you in the next video for The Solution.