WEBVTT

00:00.080 --> 00:01.820
In this new activity.

00:01.820 --> 00:08.180
And just before we go to check the Raspberry Pi functionalities, we are going to use the servo motor

00:08.180 --> 00:09.800
and also the push button.

00:09.800 --> 00:16.310
So basically, the Raspberry Pi here will be responsible to do a sweep of the servo.

00:16.310 --> 00:18.140
So you can check one of the previous lesson.

00:18.140 --> 00:21.020
When did the code for the servo motor?

00:21.050 --> 00:28.730
And basically the sweep is that you're going to go from the angle zero to angle 180 on the servo motor

00:28.730 --> 00:34.370
and then come back from 182 zero with a small delay in between.

00:34.370 --> 00:34.550
Okay.

00:34.580 --> 00:36.680
For example, ten milliseconds.

00:36.680 --> 00:38.510
That was our example.

00:38.510 --> 00:43.910
And so you will see the servo going from the minimum position to the maximum coming back to the minimum.

00:43.910 --> 00:44.540
ET cetera.

00:44.570 --> 00:46.580
ET cetera, in an infinite loop.

00:46.790 --> 00:50.630
But this time we are not going to do the sweep on the Arduino.

00:50.660 --> 00:54.080
We are going to do the sweep on the Raspberry Pi with Python code.

00:54.200 --> 00:54.590
Okay.

00:54.590 --> 00:58.370
So on the Arduino side, the code is going to be actually quite simple.

00:58.430 --> 00:58.760
Okay.

00:58.760 --> 01:05.200
So you're going to read from Serial and receive a servo command, which contains an angle between 0

01:05.200 --> 01:06.390
and 180.

01:06.420 --> 01:13.110
So for this, I don't give you the protocol, but you can get some inspiration from what we did with

01:13.110 --> 01:16.860
the RGB led to send a servo command and a number.

01:16.890 --> 01:17.130
Okay.

01:17.130 --> 01:19.500
So you can do something similar for the servo.

01:19.530 --> 01:23.640
You're also going to read from the push button like we did before.

01:23.640 --> 01:27.750
And when it's pressed, you send a message to the Raspberry Pi and on the Raspberry Pi.

01:27.780 --> 01:30.720
So that is where actually we will have the most code here.

01:30.720 --> 01:35.670
So I have given you some few code examples to help you get started.

01:35.670 --> 01:37.200
So you're going to do a sweep here.

01:37.200 --> 01:39.540
We have, for example, five milliseconds.

01:39.540 --> 01:39.780
Okay.

01:39.780 --> 01:41.610
So here we use seconds.

01:41.610 --> 01:41.820
Okay.

01:41.820 --> 01:50.430
So five milliseconds is 0.005 second in your kind of void loop here in the while true, what you're

01:50.430 --> 01:56.670
going to do is so I have first a timeslip with 0.001.

01:56.670 --> 02:00.090
So again we don't want to make the while true.

02:00.120 --> 02:01.800
Go at full speed.

02:01.800 --> 02:06.990
We want to reduce the speed a bit, which is going to reduce a lot the CPU usage.

02:07.290 --> 02:15.240
And so here before we use 0.01, which is going to make the while loop go at 100Hz here, I'm going

02:15.240 --> 02:18.480
to use 0.001.

02:18.480 --> 02:22.080
So the loop is going to run at 1000Hz, okay?

02:22.080 --> 02:24.150
Because we're going to use that kind of time.

02:24.150 --> 02:24.600
Okay.

02:24.600 --> 02:26.010
Five millisecond.

02:26.010 --> 02:30.390
So we need a loop that runs faster than the delay, actually.

02:30.390 --> 02:30.900
Okay.

02:31.080 --> 02:38.610
And why do this here is because well, we are not going to use time to wait between two changes in the

02:38.610 --> 02:38.970
angle.

02:39.000 --> 02:39.600
Okay.

02:39.630 --> 02:40.380
Why is that?

02:40.380 --> 02:44.280
Because we will also need to read from the sale.

02:44.280 --> 02:48.240
So we don't want to use time slip to block the program.

02:48.240 --> 02:55.170
And so the only time that slip is going to be the one that reduces a bit the speed of the loop here.

02:55.230 --> 02:57.030
And basically we are going to do the same.

02:57.030 --> 02:58.170
As for the Arduino.

02:58.170 --> 03:05.480
So in the Arduino, when we wanted to do an action every X amount of time without using the delay,

03:05.510 --> 03:10.790
we use a structure where we compare the previous time with the current time, and that's what we do

03:10.790 --> 03:11.570
here also.

03:11.570 --> 03:17.330
So what you can do is import the time module and then with time, dot time, you get the current time

03:17.330 --> 03:18.650
in seconds, okay?

03:18.650 --> 03:25.370
And so you can initiate a last time action down to the current time and put an action delay in second

03:25.880 --> 03:27.710
and then in the loop.

03:27.710 --> 03:35.060
Every time simply you get the time and you do the current time minus the last time, the previous time,

03:35.060 --> 03:37.250
which gives you a duration.

03:37.250 --> 03:43.310
And if this duration is greater than the delay, then you do your action and you of course update the

03:43.310 --> 03:46.400
time when the action was done to the current time.

03:46.640 --> 03:46.970
Okay.

03:46.970 --> 03:52.280
And after that, if our before that, if you can also check if you have received something from Serial

03:52.640 --> 03:58.130
and then well, to make the servo sweep, what you can do is you can add a variable that is going to

03:58.130 --> 04:00.440
be, for example, let's say.

04:01.210 --> 04:06.840
Servo going up, like, for example, a Boolean.

04:06.850 --> 04:07.090
Okay.

04:07.090 --> 04:14.110
So if it's going up every time you're going to add plus one to the angle and send plus one from the

04:14.110 --> 04:21.790
current angle until you reach 180 and then you put that to false.

04:21.790 --> 04:23.980
And if you have false, you're going to go down.

04:23.980 --> 04:26.620
So from 180 to 0.

04:26.620 --> 04:31.750
And when you reach zero, you put that to true so you can go up again.

04:31.750 --> 04:33.520
Okay, so that's how you do the sweep.

04:33.520 --> 04:39.460
And I have added another functionality that actually when you press the push button on the Arduino,

04:39.490 --> 04:41.740
you're going to send the message to the Raspberry Pi.

04:41.740 --> 04:45.970
Here, you receive the message and you're going to change the speed.

04:45.970 --> 04:49.210
So the speed is basically the delay.

04:49.570 --> 04:53.080
So the shorter the delay, of course, the higher the speed.

04:53.080 --> 04:58.150
And so here to make it simple, you can just create a list with different speeds.

04:58.150 --> 05:01.520
So five milliseconds, ten, 15, 20.

05:01.520 --> 05:08.480
And when you receive a message that the button has been pressed, you just go to the next speed, okay?

05:08.480 --> 05:10.850
And when you reach that one, you go back to the beginning.

05:10.850 --> 05:17.780
And so when you update actually the delay, well, that's going to also update this code because now

05:17.780 --> 05:21.770
you're going to check with the new action delay and you don't need to do anything else.

05:22.190 --> 05:22.670
All right.

05:22.670 --> 05:29.690
So to recap here, you do a sweep with Python, you send the servo command to the Arduino.

05:30.380 --> 05:30.790
Okay?

05:30.800 --> 05:38.930
And for that, you use a non-blocking structure and you also read from cell so that when you press on

05:38.930 --> 05:42.740
the push button, you send the message from Arduino to Raspberry Pi.

05:42.740 --> 05:49.640
And when you receive that message, you change the delay with one of the delay that is in that list.

05:49.730 --> 05:53.750
And I will see you in the next lesson for the solution.
