All right.
This is the solution of the previous activity where you need to make the servo sweep from the Raspberry
Pi and then you're going to change the speed when you press on the push button on the Arduino.
And so.
Well, once again, let's start with the Arduino side.
So I'm first going to do the button press code and actually let's open the previous activity, which
was five.
So I am not going to write the complete code for the push button because we just did it in the activity
and in one of the previous lessons.
So.
Well, if you want more explanation, please go back.
In a previous video to get the explanations of that.
I'm just going to hit copy.
The code that we already wrote before.
Okay, so that doesn't come from nowhere.
That comes from something we have already done.
Okay.
And I'm going to also initialize the serial, as always.
And then well, you see, that's the part of the code I want to copy here because.
Well, we're not going to write that again.
Okay.
And that's it.
I'm going to close that.
So in our code for now, we have so the define for the button, some variables we initialize the button
and the previous state.
And then in the void loop.
So make sure you have the correct blocks of code here.
So well in the void loop, we read the current time, we check the duration.
Since last time the button has changed.
If this is greater than the debounce delay, then we read the push button, we compare it, we update
the variables and if it's high, the button has been pressed and we send button pressed.
So just to be sure, let's plot that to the Arduino activity.
Six.
Down uploading.
Let's open the monitor.
Press on the push button.
Button pressed.
If I press multiple times.
Okay, great.
Now let's do the code for the servo.
So I'm going to initialize the servo.
So define servo pin 12.
Let's include
the servo dot h library.
And let's create so we can create it before that or after.
It doesn't matter.
Servo.
Semicolon.
And here let's do several dot attach servo pin.
Okay.
The servo is initialized and now in the void loop.
After we have read or before, it doesn't matter again.
So after we have read from the push button, let's do if serial dot available greater than zero and
then let's check.
So what command we're going to receive, let's do string cmd is equal to read string until backslash
n and then if command dot.
So what do we want to receive?
We want to receive the servo command with a number.
So let's say that we're going to receive something that starts with.
Several colon like this.
And then the angle we want.
And so we can do command dot remove.
Okay.
We start at zero one, two, three, four, five, six.
So zero six we are left with the number.
So int angle is equal to cmd dot two int.
What we could do also.
Now we're not going to do it.
But what we could do as an improvement is to check that the angle is actually between 0 and 180.
Okay.
So that we don't receive values that are negative, for example, or values that are greater than 180.
And then we do servo dot right angle and that's pretty much it.
We have the code to send the button pressed message and we have the code to receive and execute the
servo command.
Okay, let's check.
Yes, I'm going to upload the code and test this code.
And well, actually it's better if I do serial dot read string until because this is part of the serial
library, not just a random function like this.
Let's upload.
Uploading, download, uploading.
Let's open the serial monitor and let's see what we have on the circuit.
So first, well, if you have like like me, if you have disconnected the servo pin here on pin 12,
put it back.
Okay?
Because if not, that's not going to work.
So if I press on the push button, it's still working, button pressed.
And then I'm going to do several, for example, zero and let's press enter.
Okay, it goes to zero and then servo with 153.
For example, it goes to 153.
That's correctly working.
So I'm going to close that and where I think we are done with the Arduino code.
Let's now switch to the Raspberry Pi code so you could see that on the Arduino.
Well, we didn't have many things to do.
Okay.
We just send something here, we just receive something here.
There is no link between the two actions.
Okay?
The link between the two actions is going to be on the python side.
So let's remove.
Well, let's remove all of that.
Okay.
We know what to do.
Let's start from a blank page.
Well, a blank page.
No, because I'm going to use, of course, the structure.
So let's go to any of the previous activity and let's just paste the structure, remove what we don't
need.
Like this.
Okay, That's the structure to import stuff.
Initialize communication and then our own kind of void loop.
I'm going to put here 0.01.
Okay.
So we can run this loop at 1000Hz.
And now, well, let's do the servo sweep first.
So we are going to start here.
I'm going to put last time servo move, for example, is equal to time dot time and we have the time
already imported.
And then let's say several delay is equal to.
So we're going to put the different delays after I'm going to start with 005 or let's do actually this,
which is ten milliseconds now in our while true after the time dot sleep.
So I usually put this at the beginning, okay.
Because if you put this at the end of your program and then you have something that is quite big, then
you might miss it or you might put a wrong indentation.
So that's why I usually put that first, okay, as a best practice.
So I don't remove it by mistake or I don't put indentation by mistake, I don't do any mistake.
So any code I write after this time dot sleep.
And so now I do time now is equal to time dot time, which is the current time in second if time dot
now minus the last time servo move if this is greater than the servo delay.
So you can see this is really similar to what we did here for example with the push button.
Okay, time now minus last time is greater than the delay.
That's the exact same code but with Python.
So we do last time servo move is equal to time now and then we can do the action.
So what is the action?
We're going to start from somewhere.
Okay.
So let's say servo angle is equal to 90.
Let's start at 90, okay, which is at the middle.
And we're going to do servo going up is equal to true.
So we start at 90 and we go until 181st.
And so what we do is so if servo going up, then we do servo angle plus one and then else.
So if the servo is not going up, which means it's going down several angle minus one.
And so here the angle was 90 we put true here.
So we go here, the servo angle is going to be 91.
Now we also need to check, of course, when we reach the max.
So if several angle is equal to 180 in that case we are going to do several.
Going up is equal to false so that the next time what we do the next time is we don't go here, the
next time we go here.
And then so the servo angle will be 180, but we do minus one, so 179 and then 178 et cetera.
And then we also need to check here.
If servo angle is equal to zero, then we don't go lower and we do several.
Going up is equal to true.
So the next time we don't go inside this block of code, we go inside this one and we start from zero,
one, two, three, four, five, etcetera, until we reach 180 and then we go back here.
Until we reach zero, we go back here.
ET cetera.
Okay.
And now that we have the servo angle, so let's skip the indentation we are in that if I'm going to
do command is equal to servo.
Like this plus several angle, but as a string plus backslash n.
Okay, so we make sure that we have the same command as here.
Several.
Colon and then a number k in backslash n for the read string until.
Backslash n.
Okay.
It seems good.
So we just need now to send the command.
So say dot write.
Comment dot encode UTF eight.
Okay.
So we have the delay mechanism with that if.
Okay.
And the time now and we have the sweep mechanism where we change the angle.
Here and then we send the command.
So here we're going to send the command every.
Ten millisecond.
Okay, let's save this as activity six dot p y.
Let's first try this actually.
Okay.
So I am going to run the script.
Okay.
The server is connected and you can see the server is sweeping.
Great.
It's working.
Okay, so here, I'm just going to disconnect that for now so it doesn't make noise and well, let's
continue with the following part of the code.
So Ctrl C here.
So you can see here we already have a sweep command from the Raspberry Pi to the Arduino and we do the
sweep here instead of in the Arduino.
Now I'm going to add a list, so let's add a list here.
Let's do several delays is equal to and so we have 0.005 and 0.01.
So that's all the delays in milliseconds.
So 15 and.
20.
Let's go with that.
And let's say that the several well, actually, let's put that before several delay is several delays.
Zero.
So the first one.
Okay.
So we start here.
We don't start at ten milliseconds.
We start at five.
Okay.
And now well, in order to change that, I'm going to read from the soil.
So we have this first block of code.
So let's put a comment here.
Sweep so we can put some spaces to make it more readable.
And then let's do change.
Several speed.
So the order here doesn't matter.
You could do that before or do that before because we're going to go very fast in this loop.
And so what we do if, say, dot in waiting greater than zero then we do messages is equal to say dot
readline dot decode UTF.
Eight dots airstrip.
We check if message is equal to button pressed.
So let's use that directly.
Then what we simply need to do is to do a change here on the server delay.
And so we need to keep an index actually.
So let's do several delay index is equal to zero.
Let's put that here.
Okay.
And we're going to increment this.
So we're going to do several.
DeLay index plus one.
And of course, we need to check if servo delay index is greater or equal.
So just equal should be enough.
But let's just use that as a more general condition to the length of the servo delays.
So basically, here we are at zero.
We do plus one.
We are at one plus one, we are at two plus one.
We are at three and plus one we are at index four.
That doesn't exist.
Okay.
The length of the list here is four.
Okay.
With the indexes.
Zero, one, two, three, four.
Well, actually zero one, two and three.
If we reach number four, which corresponds to the length of the list, then we are out of the list
and we're going to get an error.
So we check that if we arrive at the length, we simply do.
Servo delay index is equal to zero.
We come back to zero and now we are sure that we have a correct index and we simply do.
Servo delay is equal to several delays with the servo delay index.
And so when we receive the button pressed, we are simply going to change this value here with the next
value on the list.
And this is used where this is used here on this action.
So we are going to change the speed of the action basically.
Okay, let's try that.
So I am going to the Arduino code doesn't change.
I'm going to run this again.
So this level is sweeping.
You can see a little bit faster now every five milliseconds.
Press on the push button.
It's going slower.
I press on the push button again.
It's going slower now.
Every 15 milliseconds I press again.
It's going slower.
Every 20 milliseconds.
And I'm going to press and it's going to come back to the first element, which is five milliseconds.
So five, ten, 15.
And 20 and five again.
All right.
So I'm going to disconnect this just so it doesn't make any noise and the code is working.
So I'm going to press Ctrl C, So here I didn't put any log on the Python side.
You could put some logs here when you actually change the delay.
Okay.
To make sure that you have the correct delay.
Adding some logs here may be too much.
Okay.
If you print a log every five milliseconds to show the angle, you may do that for debugging, but then,
well, maybe that's too much for a normal use of the application.
But here for the change of the delay, why not?
Okay.
And that's the end of this activity.
So here you can see that basically the code that you could do on the Arduino, which is so the application
to make the servo sweep, you can do that also from the Raspberry Pi.
Okay.
So you can choose basically where you control the logic of different parts of your application.
So actually here I did the sweep on the raspberry Pi with Python as a good practice exercise, but maybe
the best option well, depends on your application, but maybe the best option would be to do the sweep
on the Arduino.
So to handle this on the Arduino and then just send from the Raspberry Pi the delay that you want to
use.
Okay.
And the application will be exactly the same.
Okay.
The result will be the same, but the code to sweep the servo would be on the Arduino.
Okay.
So here you can see we are in the example where we could do this here on Python or this on Cplusplus.
And so sometimes it's not necessarily super clear where you should put which code and well, you're
going to simply learn more about that when you practice with this course.
And also after this course, when you practice more and you do more projects, you're going to have
a better idea of what to put on your Raspberry Pi, what to put on the Arduino.
All right.
And that's the end of this section on the Arduino functionalities.
Now we are going to go to the Raspberry Pi functionalities and continue to explore the different things
we can do with Raspberry Pi and Arduino.
And after that, we are going to start the final project of the course.