This is the solution of the activity number five, where you have to send a message from the Arduino
to the Raspberry Pi.
When you press on the push button and then receive a color for one of the LEDs.
So basically one of the color of the RGB and then change that color from the Arduino.
So once again, let's start to do the Arduino code and then we're going to debug the Arduino code with
the sign monitor, and then we're going to write the Python program on the Raspberry Pi.
So let's initialize I'm going to remove this.
Let's initialize the hardware components.
So define button pin, which is pin number seven, and then define.
RGB red pin, which is 11.
And let's do the same for the green and the blue.
Green.
Blue and we have ten.
And we have nine.
Okay.
In the void setup, what we do is spin mode with button pin input.
And then pinmode.
For the red pin to output.
Okay.
And then the same for the green.
The same for.
W.
So I go quite fast with that because we've already done that multiple times and then I'm going to open
style communication.
So style dot begin with, let's keep the same ball rate.
And while.
So let's wait.
All right.
Now, what do we do?
I'm going to also remove that.
So we have two things to do which are, well, actually completely independent.
First, we need to read the purge button, check when it is pressed and send a message to the sale.
The second action is to receive something from the sale and check what command we have.
If it's right green or blue and then apply the command to the Led.
So while there is no particular order to do that, you can debug both independently.
But let's start with let's say the push button.
Okay.
So I'm going to do the Debounce code for the push button once more.
Unsigned long last time button changed, let's say millis and then unsigned long button.
Let's say Debounce.
DeLay is let's put 50 milliseconds another time and then so unsigned long time now is equal to millis.
So we get the current time.
We check if time now minus last time the button has changed.
If this is greater or equal than the debounce delay.
Then we can enter this if and what we can do is do white button state is equal to digital read from
button pin.
Then we will need to.
So we will need to compare the button state with the previous button state.
So I'm going to add here white previous button state.
And just after we do the PIN modes I'm going to do previous button state is equal to digitalwrite from
button pin.
So we have the first state here at the beginning and then we are going to compare Button State.
So if this is different than the previous one, then we need to do two things.
We need to update the last time the button has changed to time now.
Okay.
So then the debounce is going to happen another time.
We're going to do the previous button state is equal to button state.
And then we can check if Button State is equal to hi here because we have a pull down resistor.
Then the button button is pressed.
Okay.
And let's do self dot print ln.
And.
Well, what do we send here?
I'm not going to do.
Button is pressed.
Okay.
This is not a log.
This is a communication from the Arduino to the Raspberry Pi.
So let's say we are going to send button pressed like this and here we're going to receive button pressed.
Okay.
So very important to agree on what you send and what you receive.
And well, I'm first going to just check if that code is working.
So activity five.
Compiling.
Uploading.
Okay, let's open the serial monitor and let's press on the push button.
Button pressed another time.
Button pressed button pressed.
It is working.
Great.
So now let's do the code for the RGB led.
So we have this already.
Well, we have the initialization, which is okay.
Now let's do so here in the void loop.
Make sure you don't make a mistake, okay?
And that you don't put the code in the wrong block of code.
So that's going to be here.
So if.
Dot available.
Greater than zero.
So that doesn't change really.
Then.
So if we have received something.
We're going to do string.
Simply command is equal to self dot.
And guess what?
Read string until backslash n with one single quote.
And then we do if and that's where we are going to check what command we have received.
Because here we can receive three different command.
So if command dot starts with so the command can start with, for example, read like this, then we're
going to process the read command and then else if cmd dot starts with we have green.
Okay.
Else if cmd dot start with blue and then let's do another else like I did before.
Nothing for now.
Or maybe what we could do if we receive an error is just to, for example, power off completely the
RGB entity.
Okay, It's up to you.
So if we receive the red color, what we can do with the command is first to do cmd dot remove and then
zero for.
Okay, we'll remove.
So zero, one, two, three, four.
We'll remove all of that and we are left with the number.
But the number is actually a string.
So we do for example, int red is equal to C and D dot to int like this.
And then analog, right with RGB red pin and with the red number that we got from the command and what
we can do.
The same with every other command.
So cmd dot remove.
But here we don't do zero four because if we do zero four we're going to remove one, two, three,
four and we are going to be left with n colon and the number.
So we do zero one, two, three, four, five, six.
Okay.
So as you can see each command, you will need to process it independently and make sure that it's working.
INT green is equal to cmd dot to INT.
This doesn't change in analog, so let's use analog write the same.
But.
We've.
Green and.
Green.
Okay.
And then for the blue.
We do cmd dot remove and how many do we need to remove?
One, 2345.
So zero five and then int blue is equal to cmd dot to int.
And let's do analog right with the blue pin and the blue color.
Okay.
And now we can try this code actually on the Arduino to check if it's correctly working and well, if
you want an additional debug step because maybe well, let's say you want to send the right or the green
or the blue command and it doesn't work.
You don't necessarily know why it doesn't work.
So what you can do, for example, let's say here Serial.println, for example, red, so we can just
use this when we debug the Arduino with the serial monitor.
And then of course that's very important.
Don't forget to remove any log that you have put first for the Arduino side.
So let's upload the code to the Arduino.
Let's open the serial monitor so we still have the push button.
That will show button pressed.
And then let's do red colon 200.
You can see it is correctly working and we have 200.
Okay, let's do green.
100.
Okay.
We have a different color in blue and 50.
Let's do blue.
Actually, 200.
Okay.
And maybe red.
Zero.
Right.
And green.
Zero.
Blue, 200.
Well, there is something wrong with blue.
And well, actually, I have checked that I have used I don't know why, but 19 for the blueprint.
So of course that doesn't work.
So you can see here that didn't work.
We didn't see the command blue change.
And then it helped us to actually see that we have an error in our code.
So now let's run that again.
I'm going to open the side monitor again and just.
Test blue with two 100 and well, the blue is working great.
So now I can just close that.
I could also do a print with the blue.
Okay.
To make sure that we receive from sale and I'm going to remove this is very important.
Any log that I have put which have nothing to do with the cell communication with the Raspberry Pi.
Okay.
So now the Arduino code is working.
I'm going to upload also the new code, of course, without the dot print.
Ellen Okay, The Arduino side is working.
Let's go to the Raspberry Pi side.
So first of all, we need to initialize serial and everything, but I'm going to use the code from the
previous activity.
So select everything so you can do this by yourself.
But I'm going to do this so it goes a bit faster.
And anyway, we have already done that multiple times.
So what?
I'm going to remove that also.
You adjust the input initialization and then the infinite loop with the time.sleep very important.
And I'm going to so I'm going to import random here and let's remove that.
Okay, so what do we do on the Raspberry Pi?
Well, we don't send anything without receiving anything, so we first need to check that we have received
something.
So in the while true if say, dot in waiting is greater than zero again without parentheses, then we
do, let's say message.
Is equal to.
And from this age we could also do MSG, for example.
That's the same as using CMD for command.
Okay, that's pretty explicit and that's something you're going to use very often.
So that's not a big deal.
And so set dot readline dot.
Decode.
UTF eight.
Dot s trick.
Okay.
And then what we do is we check if message is equal to is equal to what is equal to button pressed.
Okay.
We send button pressed.
We check if we receive button pressed.
If we have received button pressed, then we are going to process the action.
And what is the action is to choose a random color.
So right green and blue and then the random number for that color.
Okay, so let's do D is equal to random dot rand int with zero and to.
And then the number is equal to random dot rand, int from 0 to 255.
Okay.
And then we're going to construct the string that we're going to send.
Okay.
So the command and so let's start with CMD is equal to an empty string.
And then if ID is equal to one, we do cmd plus red.
And then let's do L.
If Z is equal to two, we do cmd.
Less green.
And then let's do ls cmd plus blue.
So we have the first part of the command and then we add So cmd plus.
Number, but actually as a string.
So we cast the number as a string and then cmd plus.
Backslash.
N.
Okay, so we start from an empty string, then we construct it with first the command name red, green
and blue, and then the number we want to read that we're going to get here and then backslash n for
the read string until with the backslash.
N Okay.
And then we can do set dot write.
CMD dot encode UTF.
Eight like this.
And let's see if that works.
Maybe we can add some prints here because it's easier to debug on Python.
We can add some prints.
So let's say here print.
Button has been pressed.
Okay.
And then maybe print, send, command plus command.
So we're gonna make sure that we send the correct command.
Okay.
Because we know that the Arduino side is working.
So if we send something from the Raspberry Pi to the Arduino, and if it doesn't work, well, we probably
know that this is because we sent the wrong thing.
So if we just print the thing, it's easier to debug.
Let's save this as activity five.
And so the Arduino is already uploaded.
Let's run it on the Raspberry Pi.
Okay.
You can see cereal.
Okay.
And now?
Well, what I need to do is I need to press on the push button and.
Let's see.
Okay, we have an error and well, actually okay.
The module random has no attribute rand int simply because.
Well I have to use a lowercase here.
Okay.
I have used uppercase but it is a lowercase.
I made a mistake.
So now let's run again.
Sale.
Okay, let's press on the push button and you can see button has been pressed.
Send command red 125.
I press again.
Send comments or write again here.
Write again with 30.
And here you can see Blue 237.
Green.
36.
Green 200.
Okay, so every time we change one color, 174 for blue.
ET cetera.
So now I press control C okay.
And you can see adding some.
A print here on the Python side is very, very useful to see what you send and then you can check that
it's correctly doing what you want to do on the Arduino side and that's the end of this activity.