WEBVTT

00:00.050 --> 00:00.350
All right.

00:00.350 --> 00:06.500
This is the solution of the previous activity where you need to make the servo sweep from the Raspberry

00:06.500 --> 00:11.390
Pi and then you're going to change the speed when you press on the push button on the Arduino.

00:11.390 --> 00:12.110
And so.

00:12.110 --> 00:15.290
Well, once again, let's start with the Arduino side.

00:15.290 --> 00:23.330
So I'm first going to do the button press code and actually let's open the previous activity, which

00:23.330 --> 00:24.350
was five.

00:26.520 --> 00:34.440
So I am not going to write the complete code for the push button because we just did it in the activity

00:34.440 --> 00:36.330
and in one of the previous lessons.

00:36.330 --> 00:37.080
So.

00:37.380 --> 00:40.440
Well, if you want more explanation, please go back.

00:42.040 --> 00:44.770
In a previous video to get the explanations of that.

00:44.770 --> 00:46.840
I'm just going to hit copy.

00:48.730 --> 00:51.340
The code that we already wrote before.

00:51.370 --> 00:54.140
Okay, so that doesn't come from nowhere.

00:54.160 --> 00:56.650
That comes from something we have already done.

00:57.730 --> 00:58.240
Okay.

00:58.240 --> 01:03.310
And I'm going to also initialize the serial, as always.

01:03.310 --> 01:07.300
And then well, you see, that's the part of the code I want to copy here because.

01:07.300 --> 01:10.870
Well, we're not going to write that again.

01:12.040 --> 01:12.540
Okay.

01:12.670 --> 01:13.720
And that's it.

01:13.740 --> 01:14.920
I'm going to close that.

01:14.920 --> 01:22.090
So in our code for now, we have so the define for the button, some variables we initialize the button

01:22.090 --> 01:23.320
and the previous state.

01:24.360 --> 01:25.620
And then in the void loop.

01:26.430 --> 01:29.910
So make sure you have the correct blocks of code here.

01:29.910 --> 01:33.990
So well in the void loop, we read the current time, we check the duration.

01:33.990 --> 01:36.510
Since last time the button has changed.

01:36.540 --> 01:42.690
If this is greater than the debounce delay, then we read the push button, we compare it, we update

01:42.690 --> 01:47.880
the variables and if it's high, the button has been pressed and we send button pressed.

01:47.880 --> 01:53.040
So just to be sure, let's plot that to the Arduino activity.

01:54.480 --> 01:55.260
Six.

01:59.150 --> 02:00.350
Down uploading.

02:00.350 --> 02:01.780
Let's open the monitor.

02:01.970 --> 02:03.440
Press on the push button.

02:03.650 --> 02:05.300
Button pressed.

02:05.450 --> 02:07.300
If I press multiple times.

02:07.310 --> 02:08.300
Okay, great.

02:08.750 --> 02:11.960
Now let's do the code for the servo.

02:12.380 --> 02:15.110
So I'm going to initialize the servo.

02:15.110 --> 02:20.450
So define servo pin 12.

02:20.630 --> 02:22.730
Let's include

02:25.970 --> 02:29.090
the servo dot h library.

02:29.390 --> 02:32.930
And let's create so we can create it before that or after.

02:32.930 --> 02:33.950
It doesn't matter.

02:34.040 --> 02:34.940
Servo.

02:35.210 --> 02:36.140
Servo.

02:36.620 --> 02:37.790
Semicolon.

02:39.080 --> 02:46.310
And here let's do several dot attach servo pin.

02:47.330 --> 02:47.600
Okay.

02:47.600 --> 02:50.510
The servo is initialized and now in the void loop.

02:50.510 --> 02:53.630
After we have read or before, it doesn't matter again.

02:53.630 --> 03:06.950
So after we have read from the push button, let's do if serial dot available greater than zero and

03:06.950 --> 03:08.210
then let's check.

03:08.300 --> 03:18.200
So what command we're going to receive, let's do string cmd is equal to read string until backslash

03:18.200 --> 03:23.090
n and then if command dot.

03:23.090 --> 03:25.340
So what do we want to receive?

03:25.370 --> 03:28.490
We want to receive the servo command with a number.

03:28.490 --> 03:33.650
So let's say that we're going to receive something that starts with.

03:36.510 --> 03:40.260
Several colon like this.

03:41.710 --> 03:44.410
And then the angle we want.

03:44.710 --> 03:47.710
And so we can do command dot remove.

03:48.590 --> 03:48.810
Okay.

03:48.880 --> 03:53.470
We start at zero one, two, three, four, five, six.

03:53.470 --> 03:58.360
So zero six we are left with the number.

03:58.360 --> 04:04.860
So int angle is equal to cmd dot two int.

04:05.910 --> 04:07.170
What we could do also.

04:07.170 --> 04:08.400
Now we're not going to do it.

04:08.400 --> 04:14.370
But what we could do as an improvement is to check that the angle is actually between 0 and 180.

04:14.400 --> 04:14.630
Okay.

04:14.640 --> 04:22.830
So that we don't receive values that are negative, for example, or values that are greater than 180.

04:23.310 --> 04:29.850
And then we do servo dot right angle and that's pretty much it.

04:29.940 --> 04:37.320
We have the code to send the button pressed message and we have the code to receive and execute the

04:37.320 --> 04:38.970
servo command.

04:39.810 --> 04:41.270
Okay, let's check.

04:41.280 --> 04:46.830
Yes, I'm going to upload the code and test this code.

04:48.240 --> 04:55.090
And well, actually it's better if I do serial dot read string until because this is part of the serial

04:55.140 --> 04:58.920
library, not just a random function like this.

04:59.100 --> 05:00.240
Let's upload.

05:02.490 --> 05:05.210
Uploading, download, uploading.

05:05.910 --> 05:09.810
Let's open the serial monitor and let's see what we have on the circuit.

05:09.810 --> 05:17.190
So first, well, if you have like like me, if you have disconnected the servo pin here on pin 12,

05:17.220 --> 05:18.290
put it back.

05:18.300 --> 05:18.900
Okay?

05:18.990 --> 05:21.180
Because if not, that's not going to work.

05:21.180 --> 05:24.900
So if I press on the push button, it's still working, button pressed.

05:24.900 --> 05:30.810
And then I'm going to do several, for example, zero and let's press enter.

05:31.230 --> 05:37.050
Okay, it goes to zero and then servo with 153.

05:37.080 --> 05:40.680
For example, it goes to 153.

05:40.710 --> 05:42.540
That's correctly working.

05:42.660 --> 05:48.930
So I'm going to close that and where I think we are done with the Arduino code.

05:48.930 --> 05:52.710
Let's now switch to the Raspberry Pi code so you could see that on the Arduino.

05:52.710 --> 05:54.930
Well, we didn't have many things to do.

05:54.930 --> 05:55.470
Okay.

05:56.740 --> 06:00.610
We just send something here, we just receive something here.

06:00.610 --> 06:03.390
There is no link between the two actions.

06:03.400 --> 06:03.840
Okay?

06:03.850 --> 06:07.870
The link between the two actions is going to be on the python side.

06:08.410 --> 06:11.080
So let's remove.

06:11.080 --> 06:12.580
Well, let's remove all of that.

06:12.580 --> 06:12.820
Okay.

06:12.820 --> 06:14.740
We know what to do.

06:14.980 --> 06:17.380
Let's start from a blank page.

06:17.770 --> 06:18.910
Well, a blank page.

06:18.910 --> 06:20.830
No, because I'm going to use, of course, the structure.

06:20.830 --> 06:28.300
So let's go to any of the previous activity and let's just paste the structure, remove what we don't

06:28.300 --> 06:28.930
need.

06:30.460 --> 06:31.480
Like this.

06:32.560 --> 06:36.010
Okay, That's the structure to import stuff.

06:36.790 --> 06:42.340
Initialize communication and then our own kind of void loop.

06:42.370 --> 06:44.690
I'm going to put here 0.01.

06:44.710 --> 06:45.220
Okay.

06:45.370 --> 06:48.300
So we can run this loop at 1000Hz.

06:48.310 --> 06:51.820
And now, well, let's do the servo sweep first.

06:51.820 --> 06:53.740
So we are going to start here.

06:53.740 --> 07:04.840
I'm going to put last time servo move, for example, is equal to time dot time and we have the time

07:04.840 --> 07:05.980
already imported.

07:05.980 --> 07:12.010
And then let's say several delay is equal to.

07:12.280 --> 07:21.730
So we're going to put the different delays after I'm going to start with 005 or let's do actually this,

07:21.730 --> 07:27.730
which is ten milliseconds now in our while true after the time dot sleep.

07:27.730 --> 07:31.210
So I usually put this at the beginning, okay.

07:31.210 --> 07:37.070
Because if you put this at the end of your program and then you have something that is quite big, then

07:37.070 --> 07:40.460
you might miss it or you might put a wrong indentation.

07:40.460 --> 07:43.670
So that's why I usually put that first, okay, as a best practice.

07:43.670 --> 07:49.700
So I don't remove it by mistake or I don't put indentation by mistake, I don't do any mistake.

07:49.700 --> 07:53.060
So any code I write after this time dot sleep.

07:53.060 --> 08:03.260
And so now I do time now is equal to time dot time, which is the current time in second if time dot

08:03.260 --> 08:09.410
now minus the last time servo move if this is greater than the servo delay.

08:09.440 --> 08:13.850
So you can see this is really similar to what we did here for example with the push button.

08:13.850 --> 08:19.100
Okay, time now minus last time is greater than the delay.

08:19.130 --> 08:22.010
That's the exact same code but with Python.

08:22.010 --> 08:31.580
So we do last time servo move is equal to time now and then we can do the action.

08:31.580 --> 08:33.230
So what is the action?

08:33.230 --> 08:35.780
We're going to start from somewhere.

08:35.810 --> 08:36.080
Okay.

08:36.080 --> 08:41.860
So let's say servo angle is equal to 90.

08:41.890 --> 08:44.590
Let's start at 90, okay, which is at the middle.

08:44.590 --> 08:49.720
And we're going to do servo going up is equal to true.

08:49.720 --> 08:54.130
So we start at 90 and we go until 181st.

08:54.130 --> 09:09.040
And so what we do is so if servo going up, then we do servo angle plus one and then else.

09:09.040 --> 09:17.230
So if the servo is not going up, which means it's going down several angle minus one.

09:17.230 --> 09:22.390
And so here the angle was 90 we put true here.

09:22.390 --> 09:26.200
So we go here, the servo angle is going to be 91.

09:26.230 --> 09:29.500
Now we also need to check, of course, when we reach the max.

09:29.500 --> 09:39.920
So if several angle is equal to 180 in that case we are going to do several.

09:40.880 --> 09:50.330
Going up is equal to false so that the next time what we do the next time is we don't go here, the

09:50.330 --> 09:51.470
next time we go here.

09:51.470 --> 10:00.410
And then so the servo angle will be 180, but we do minus one, so 179 and then 178 et cetera.

10:00.410 --> 10:02.450
And then we also need to check here.

10:02.450 --> 10:10.340
If servo angle is equal to zero, then we don't go lower and we do several.

10:11.420 --> 10:16.490
Going up is equal to true.

10:16.490 --> 10:23.450
So the next time we don't go inside this block of code, we go inside this one and we start from zero,

10:23.480 --> 10:29.930
one, two, three, four, five, etcetera, until we reach 180 and then we go back here.

10:29.930 --> 10:31.910
Until we reach zero, we go back here.

10:31.940 --> 10:32.750
ET cetera.

10:32.870 --> 10:33.680
ET cetera.

10:34.190 --> 10:34.640
Okay.

10:34.640 --> 10:41.210
And now that we have the servo angle, so let's skip the indentation we are in that if I'm going to

10:41.210 --> 10:45.200
do command is equal to servo.

10:46.960 --> 10:56.980
Like this plus several angle, but as a string plus backslash n.

10:57.460 --> 11:01.360
Okay, so we make sure that we have the same command as here.

11:01.390 --> 11:02.140
Several.

11:03.160 --> 11:09.280
Colon and then a number k in backslash n for the read string until.

11:09.310 --> 11:10.300
Backslash n.

11:10.720 --> 11:11.140
Okay.

11:11.140 --> 11:12.670
It seems good.

11:12.670 --> 11:14.440
So we just need now to send the command.

11:14.440 --> 11:15.940
So say dot write.

11:16.150 --> 11:21.250
Comment dot encode UTF eight.

11:22.720 --> 11:22.990
Okay.

11:22.990 --> 11:27.190
So we have the delay mechanism with that if.

11:27.520 --> 11:27.690
Okay.

11:27.700 --> 11:32.680
And the time now and we have the sweep mechanism where we change the angle.

11:32.710 --> 11:34.990
Here and then we send the command.

11:35.080 --> 11:37.150
So here we're going to send the command every.

11:37.180 --> 11:38.680
Ten millisecond.

11:39.040 --> 11:46.690
Okay, let's save this as activity six dot p y.

11:47.290 --> 11:49.630
Let's first try this actually.

11:49.630 --> 11:50.230
Okay.

11:50.440 --> 11:54.130
So I am going to run the script.

11:54.160 --> 11:54.760
Okay.

11:54.760 --> 12:01.120
The server is connected and you can see the server is sweeping.

12:01.660 --> 12:02.080
Great.

12:02.110 --> 12:03.110
It's working.

12:03.110 --> 12:09.530
Okay, so here, I'm just going to disconnect that for now so it doesn't make noise and well, let's

12:09.530 --> 12:12.080
continue with the following part of the code.

12:12.080 --> 12:15.200
So Ctrl C here.

12:16.250 --> 12:21.770
So you can see here we already have a sweep command from the Raspberry Pi to the Arduino and we do the

12:21.770 --> 12:24.230
sweep here instead of in the Arduino.

12:24.470 --> 12:31.010
Now I'm going to add a list, so let's add a list here.

12:31.010 --> 12:41.930
Let's do several delays is equal to and so we have 0.005 and 0.01.

12:41.930 --> 12:44.690
So that's all the delays in milliseconds.

12:46.130 --> 12:48.020
So 15 and.

12:48.790 --> 12:49.480
20.

12:50.080 --> 12:51.340
Let's go with that.

12:51.340 --> 13:01.090
And let's say that the several well, actually, let's put that before several delay is several delays.

13:01.330 --> 13:01.990
Zero.

13:01.990 --> 13:03.850
So the first one.

13:05.170 --> 13:05.470
Okay.

13:05.470 --> 13:06.820
So we start here.

13:06.820 --> 13:09.790
We don't start at ten milliseconds.

13:09.790 --> 13:10.990
We start at five.

13:11.020 --> 13:11.560
Okay.

13:11.950 --> 13:17.110
And now well, in order to change that, I'm going to read from the soil.

13:17.230 --> 13:20.620
So we have this first block of code.

13:21.330 --> 13:23.760
So let's put a comment here.

13:25.870 --> 13:31.900
Sweep so we can put some spaces to make it more readable.

13:31.900 --> 13:35.560
And then let's do change.

13:36.660 --> 13:38.070
Several speed.

13:39.570 --> 13:41.430
So the order here doesn't matter.

13:41.430 --> 13:46.350
You could do that before or do that before because we're going to go very fast in this loop.

13:46.440 --> 13:58.590
And so what we do if, say, dot in waiting greater than zero then we do messages is equal to say dot

13:58.620 --> 14:02.100
readline dot decode UTF.

14:03.550 --> 14:06.610
Eight dots airstrip.

14:09.080 --> 14:13.910
We check if message is equal to button pressed.

14:14.750 --> 14:16.430
So let's use that directly.

14:18.070 --> 14:26.860
Then what we simply need to do is to do a change here on the server delay.

14:27.190 --> 14:29.530
And so we need to keep an index actually.

14:29.530 --> 14:35.770
So let's do several delay index is equal to zero.

14:35.800 --> 14:38.590
Let's put that here.

14:39.730 --> 14:39.940
Okay.

14:39.940 --> 14:42.760
And we're going to increment this.

14:42.760 --> 14:46.660
So we're going to do several.

14:48.340 --> 14:51.650
DeLay index plus one.

14:51.700 --> 15:00.580
And of course, we need to check if servo delay index is greater or equal.

15:00.580 --> 15:02.860
So just equal should be enough.

15:02.860 --> 15:11.710
But let's just use that as a more general condition to the length of the servo delays.

15:12.100 --> 15:15.640
So basically, here we are at zero.

15:15.670 --> 15:17.470
We do plus one.

15:17.500 --> 15:21.190
We are at one plus one, we are at two plus one.

15:21.190 --> 15:25.360
We are at three and plus one we are at index four.

15:25.390 --> 15:26.650
That doesn't exist.

15:26.680 --> 15:27.190
Okay.

15:27.220 --> 15:30.370
The length of the list here is four.

15:30.400 --> 15:30.840
Okay.

15:30.880 --> 15:31.960
With the indexes.

15:31.960 --> 15:33.940
Zero, one, two, three, four.

15:34.240 --> 15:36.280
Well, actually zero one, two and three.

15:36.310 --> 15:43.510
If we reach number four, which corresponds to the length of the list, then we are out of the list

15:43.510 --> 15:45.220
and we're going to get an error.

15:45.220 --> 15:49.270
So we check that if we arrive at the length, we simply do.

15:49.390 --> 15:55.540
Servo delay index is equal to zero.

15:55.570 --> 16:00.880
We come back to zero and now we are sure that we have a correct index and we simply do.

16:00.910 --> 16:10.540
Servo delay is equal to several delays with the servo delay index.

16:11.260 --> 16:18.100
And so when we receive the button pressed, we are simply going to change this value here with the next

16:18.250 --> 16:19.990
value on the list.

16:19.990 --> 16:25.810
And this is used where this is used here on this action.

16:25.810 --> 16:28.660
So we are going to change the speed of the action basically.

16:28.660 --> 16:30.970
Okay, let's try that.

16:31.120 --> 16:34.960
So I am going to the Arduino code doesn't change.

16:34.960 --> 16:37.960
I'm going to run this again.

16:40.840 --> 16:44.410
So this level is sweeping.

16:44.410 --> 16:46.840
You can see a little bit faster now every five milliseconds.

16:46.990 --> 16:48.190
Press on the push button.

16:49.030 --> 16:50.410
It's going slower.

16:51.460 --> 16:53.050
I press on the push button again.

16:53.320 --> 16:54.820
It's going slower now.

16:55.000 --> 16:58.570
Every 15 milliseconds I press again.

16:58.930 --> 17:00.650
It's going slower.

17:00.670 --> 17:03.130
Every 20 milliseconds.

17:03.160 --> 17:09.190
And I'm going to press and it's going to come back to the first element, which is five milliseconds.

17:09.500 --> 17:13.990
So five, ten, 15.

17:15.600 --> 17:18.570
And 20 and five again.

17:18.870 --> 17:19.680
All right.

17:19.680 --> 17:25.530
So I'm going to disconnect this just so it doesn't make any noise and the code is working.

17:25.530 --> 17:30.330
So I'm going to press Ctrl C, So here I didn't put any log on the Python side.

17:30.510 --> 17:34.980
You could put some logs here when you actually change the delay.

17:34.980 --> 17:35.340
Okay.

17:35.340 --> 17:37.470
To make sure that you have the correct delay.

17:38.070 --> 17:40.470
Adding some logs here may be too much.

17:40.500 --> 17:40.800
Okay.

17:40.800 --> 17:46.860
If you print a log every five milliseconds to show the angle, you may do that for debugging, but then,

17:46.860 --> 17:49.830
well, maybe that's too much for a normal use of the application.

17:49.830 --> 17:53.460
But here for the change of the delay, why not?

17:53.640 --> 17:54.030
Okay.

17:54.030 --> 17:56.010
And that's the end of this activity.

17:56.010 --> 18:01.890
So here you can see that basically the code that you could do on the Arduino, which is so the application

18:01.890 --> 18:06.810
to make the servo sweep, you can do that also from the Raspberry Pi.

18:06.840 --> 18:07.010
Okay.

18:07.020 --> 18:12.120
So you can choose basically where you control the logic of different parts of your application.

18:12.120 --> 18:19.480
So actually here I did the sweep on the raspberry Pi with Python as a good practice exercise, but maybe

18:19.480 --> 18:25.690
the best option well, depends on your application, but maybe the best option would be to do the sweep

18:25.720 --> 18:27.790
on the Arduino.

18:27.790 --> 18:34.180
So to handle this on the Arduino and then just send from the Raspberry Pi the delay that you want to

18:34.180 --> 18:34.690
use.

18:34.720 --> 18:35.320
Okay.

18:35.530 --> 18:38.230
And the application will be exactly the same.

18:38.260 --> 18:38.470
Okay.

18:38.470 --> 18:44.680
The result will be the same, but the code to sweep the servo would be on the Arduino.

18:44.680 --> 18:44.890
Okay.

18:44.890 --> 18:52.840
So here you can see we are in the example where we could do this here on Python or this on Cplusplus.

18:52.840 --> 18:59.620
And so sometimes it's not necessarily super clear where you should put which code and well, you're

18:59.620 --> 19:03.670
going to simply learn more about that when you practice with this course.

19:03.670 --> 19:08.230
And also after this course, when you practice more and you do more projects, you're going to have

19:08.230 --> 19:13.120
a better idea of what to put on your Raspberry Pi, what to put on the Arduino.

19:13.120 --> 19:13.510
All right.

19:13.510 --> 19:17.410
And that's the end of this section on the Arduino functionalities.

19:17.440 --> 19:23.650
Now we are going to go to the Raspberry Pi functionalities and continue to explore the different things

19:23.650 --> 19:25.630
we can do with Raspberry Pi and Arduino.

19:25.630 --> 19:29.890
And after that, we are going to start the final project of the course.
