In this new activity.
And just before we go to check the Raspberry Pi functionalities, we are going to use the servo motor
and also the push button.
So basically, the Raspberry Pi here will be responsible to do a sweep of the servo.
So you can check one of the previous lesson.
When did the code for the servo motor?
And basically the sweep is that you're going to go from the angle zero to angle 180 on the servo motor
and then come back from 182 zero with a small delay in between.
Okay.
For example, ten milliseconds.
That was our example.
And so you will see the servo going from the minimum position to the maximum coming back to the minimum.
ET cetera.
ET cetera, in an infinite loop.
But this time we are not going to do the sweep on the Arduino.
We are going to do the sweep on the Raspberry Pi with Python code.
Okay.
So on the Arduino side, the code is going to be actually quite simple.
Okay.
So you're going to read from Serial and receive a servo command, which contains an angle between 0
and 180.
So for this, I don't give you the protocol, but you can get some inspiration from what we did with
the RGB led to send a servo command and a number.
Okay.
So you can do something similar for the servo.
You're also going to read from the push button like we did before.
And when it's pressed, you send a message to the Raspberry Pi and on the Raspberry Pi.
So that is where actually we will have the most code here.
So I have given you some few code examples to help you get started.
So you're going to do a sweep here.
We have, for example, five milliseconds.
Okay.
So here we use seconds.
Okay.
So five milliseconds is 0.005 second in your kind of void loop here in the while true, what you're
going to do is so I have first a timeslip with 0.001.
So again we don't want to make the while true.
Go at full speed.
We want to reduce the speed a bit, which is going to reduce a lot the CPU usage.
And so here before we use 0.01, which is going to make the while loop go at 100Hz here, I'm going
to use 0.001.
So the loop is going to run at 1000Hz, okay?
Because we're going to use that kind of time.
Okay.
Five millisecond.
So we need a loop that runs faster than the delay, actually.
Okay.
And why do this here is because well, we are not going to use time to wait between two changes in the
angle.
Okay.
Why is that?
Because we will also need to read from the sale.
So we don't want to use time slip to block the program.
And so the only time that slip is going to be the one that reduces a bit the speed of the loop here.
And basically we are going to do the same.
As for the Arduino.
So in the Arduino, when we wanted to do an action every X amount of time without using the delay,
we use a structure where we compare the previous time with the current time, and that's what we do
here also.
So what you can do is import the time module and then with time, dot time, you get the current time
in seconds, okay?
And so you can initiate a last time action down to the current time and put an action delay in second
and then in the loop.
Every time simply you get the time and you do the current time minus the last time, the previous time,
which gives you a duration.
And if this duration is greater than the delay, then you do your action and you of course update the
time when the action was done to the current time.
Okay.
And after that, if our before that, if you can also check if you have received something from Serial
and then well, to make the servo sweep, what you can do is you can add a variable that is going to
be, for example, let's say.
Servo going up, like, for example, a Boolean.
Okay.
So if it's going up every time you're going to add plus one to the angle and send plus one from the
current angle until you reach 180 and then you put that to false.
And if you have false, you're going to go down.
So from 180 to 0.
And when you reach zero, you put that to true so you can go up again.
Okay, so that's how you do the sweep.
And I have added another functionality that actually when you press the push button on the Arduino,
you're going to send the message to the Raspberry Pi.
Here, you receive the message and you're going to change the speed.
So the speed is basically the delay.
So the shorter the delay, of course, the higher the speed.
And so here to make it simple, you can just create a list with different speeds.
So five milliseconds, ten, 15, 20.
And when you receive a message that the button has been pressed, you just go to the next speed, okay?
And when you reach that one, you go back to the beginning.
And so when you update actually the delay, well, that's going to also update this code because now
you're going to check with the new action delay and you don't need to do anything else.
All right.
So to recap here, you do a sweep with Python, you send the servo command to the Arduino.
Okay?
And for that, you use a non-blocking structure and you also read from cell so that when you press on
the push button, you send the message from Arduino to Raspberry Pi.
And when you receive that message, you change the delay with one of the delay that is in that list.
And I will see you in the next lesson for the solution.