WEBVTT

00:00.110 --> 00:03.080
Here is the solution for the activity number five.

00:03.080 --> 00:07.940
So I'm first going to do the solution with the code that we have seen previously.

00:07.940 --> 00:11.900
And then I'm going to give you two additional things that are very important.

00:11.930 --> 00:16.370
First is a way to improve the program and make it more efficient.

00:16.370 --> 00:21.920
And the second is well we're going to use a different way to write the program and that we will use

00:21.920 --> 00:24.650
directly for the next activity of this course.

00:24.650 --> 00:26.240
So let's get started.

00:26.240 --> 00:29.690
The first thing we need to do is to import stuff.

00:29.690 --> 00:34.160
So from Gpio zero import.

00:34.190 --> 00:41.660
So we need to import the LED and the button because we're going to use both so LED maybe you have written

00:41.660 --> 00:49.130
this from Gpio zero import like this import button.

00:49.130 --> 00:50.540
And that is correct okay.

00:50.570 --> 00:52.970
It's going to import the LED and the button.

00:52.970 --> 00:56.840
But what we can also do is to add a comma.

00:56.870 --> 01:00.810
So I showed you that before we can add a comma and just do button.

01:00.840 --> 01:01.290
Okay.

01:01.290 --> 01:08.460
So from library import functionality, one comma functionality two comma etc..

01:08.490 --> 01:08.820
Great.

01:08.820 --> 01:12.900
So now I will initialize my LED just like that.

01:12.900 --> 01:23.340
So LED I put the Gpio number 17 and then button I put button and the Gpio number 26.

01:23.550 --> 01:30.930
So first things first we initialize the Gpio 17 and we initialize the Gpio 26.

01:30.960 --> 01:32.910
And then we want to detect.

01:32.910 --> 01:36.420
So when the button is pressed we power on the LED.

01:36.540 --> 01:39.780
If it's not pressed while the LED is going to be turned off.

01:39.780 --> 01:46.530
And I don't want to just do that one time here when we run the program, I want to do it continuously.

01:46.530 --> 01:50.160
So I'm also going to use this while true loop.

01:50.160 --> 01:51.780
So an infinite loop.

01:51.780 --> 01:59.650
And I'm going to say simply if button dot is pressed I'm going to put a colon.

01:59.650 --> 02:01.240
And if it's true.

02:01.270 --> 02:04.450
Well, we do led dot on.

02:05.350 --> 02:08.680
If it is not true then I'm going to go back here.

02:08.680 --> 02:11.920
So if it's not true it can only be false.

02:11.950 --> 02:17.260
So I'm going to put else and then led dot of okay.

02:17.290 --> 02:19.450
And pay attention to the indentation here.

02:19.480 --> 02:25.480
The whole if is with one indentation because it's inside the while.

02:25.510 --> 02:31.870
But then for example that instruction has one indentation of four spaces plus an extra one.

02:31.870 --> 02:34.150
And now I can test my program.

02:34.150 --> 02:37.300
So let's just run it okay.

02:37.330 --> 02:38.080
It's running.

02:38.080 --> 02:40.360
So there's nothing printed here but it's running.

02:40.360 --> 02:42.760
And I can just press on the push button.

02:42.760 --> 02:47.050
You see the LED is on now I release okay.

02:47.080 --> 02:49.000
And you can see when it pressed it's on.

02:49.000 --> 02:54.760
When it's released it's off on off on off etc..

02:54.760 --> 02:55.870
So that's working.

02:55.870 --> 03:00.770
And if you have written this code, well you have solved the activity, but now I'm going to show you

03:00.770 --> 03:02.180
something additional.

03:02.180 --> 03:03.560
So actually two things.

03:03.560 --> 03:05.390
Let's start with the first one.

03:05.390 --> 03:09.350
And the first one is about optimizing the program a bit.

03:09.350 --> 03:11.600
And why do I care about optimizing here.

03:11.600 --> 03:13.310
Because I'm going to show you something.

03:13.310 --> 03:22.760
So while the program is running you can go here and search for it's going to be the task manager here.

03:24.830 --> 03:25.490
Okay.

03:25.490 --> 03:29.180
And let's wait that it initializes okay.

03:29.210 --> 03:33.080
And you can see we have here a command Python three.

03:33.080 --> 03:35.120
So that's basically the program that's running.

03:35.270 --> 03:36.530
You can ignore the rest.

03:36.530 --> 03:41.930
And you can see that this Python three command is taking 25% of the CPU.

03:41.930 --> 03:43.160
And what does that mean.

03:43.160 --> 03:48.590
So actually on the Raspberry Pi here on the Raspberry Pi five I have four CPUs.

03:48.590 --> 03:54.740
And when you run a Python script it's going to run using one CPU unless you have a multi-threaded application.

03:54.740 --> 03:56.210
But that's way more complex.

03:56.210 --> 04:03.120
And for now it's just using one CPU, so when you see 25%, it means that the program is actually using

04:03.120 --> 04:04.320
the whole CPU.

04:04.320 --> 04:08.250
So a program as simple as this is using the whole CPU.

04:08.280 --> 04:09.420
Why is that?

04:09.450 --> 04:10.080
Why is that?

04:10.080 --> 04:14.940
Because well, we have an infinite loop that just runs at full speed.

04:14.940 --> 04:22.140
And if it runs at full speed, it's basically going to run as fast as it can and use 100% of the resources

04:22.140 --> 04:24.180
of one CPU.

04:24.180 --> 04:25.440
So we want to avoid that.

04:25.440 --> 04:26.010
For what?

04:26.040 --> 04:26.910
For a few reasons.

04:26.910 --> 04:30.000
First, it's going to make your Raspberry Pi heat more.

04:30.000 --> 04:34.920
So if you don't have any fan or anything to protect from the heat, well, it's going to heat quite

04:34.920 --> 04:35.430
a lot.

04:35.460 --> 04:41.040
Not necessarily a problem, but well, you don't want to have your Raspberry Pi too hot and then while

04:41.040 --> 04:42.780
you are using one full CPU.

04:42.780 --> 04:45.810
So anything else that you run is going to be slower.

04:45.810 --> 04:49.410
And if there is an easy way to solve that, then we're going to do it.

04:49.650 --> 04:53.280
So instead of running this full speed, we don't need to run this full speed.

04:53.310 --> 05:00.130
We could add a small delay actually between each iteration of the while loop.

05:00.160 --> 05:02.890
I'm going to add here I'm going to import time.

05:02.980 --> 05:08.200
And what I can do is after I read the button, I'm going to add.

05:08.200 --> 05:09.850
So I go back to this indentation.

05:09.880 --> 05:10.060
Okay.

05:10.090 --> 05:13.060
After the if time dot sleep.

05:13.060 --> 05:15.910
And let's actually pause the program.

05:15.910 --> 05:21.070
But for a very short time for example 0.01 second.

05:21.070 --> 05:22.660
So that's very very tiny.

05:22.660 --> 05:25.360
That's basically ten milliseconds.

05:25.360 --> 05:27.310
So you will not see that okay.

05:27.340 --> 05:32.830
When you run the program you will not see any difference because we as human, we well, we can't see

05:32.860 --> 05:35.020
a ten millisecond difference in the program.

05:35.020 --> 05:36.910
So it's going to do the exact same thing.

05:36.910 --> 05:44.080
But this loop is going to be running at a much, much slower speed, which is still high enough for

05:44.080 --> 05:44.830
this application.

05:44.830 --> 05:48.310
So now let's stop and let's run this again.

05:48.310 --> 05:51.460
Now you can well, you can press the push button again.

05:51.460 --> 05:54.100
This is going to be exactly the same thing.

05:54.100 --> 05:58.700
But if we have a look at our task manager again.

05:59.270 --> 06:04.370
So let's wait a few seconds for that initialization.

06:04.370 --> 06:06.350
And Python three is here.

06:06.350 --> 06:08.240
And now it's using zero.

06:08.240 --> 06:11.330
So not 0% but zero point something percent.

06:11.330 --> 06:20.390
So you see we went from 100% of one CPU to something that's very very very tiny okay.

06:20.450 --> 06:23.600
So this improvement is actually quite important.

06:23.600 --> 06:30.980
If you run an infinite loop like this for a program that you run on Raspberry Pi, it's better if this

06:30.980 --> 06:33.710
loop is going to run at full speed.

06:33.710 --> 06:41.180
So the full CPU speed is better to slow it down a bit by adding here a time.sleep with a small amount.

06:41.180 --> 06:45.320
And basically this is ten milliseconds, so it's 0.01 second.

06:45.350 --> 06:51.920
That means that you are going to run this at about 100 times per second.

06:51.950 --> 06:52.130
Okay.

06:52.160 --> 06:53.090
So that's still a lot.

06:53.120 --> 06:54.470
And we are talking here about.

06:54.470 --> 06:56.550
So if it's 100 times per second.

06:56.550 --> 07:01.140
We also talk about a loop that runs at 100Hz.

07:01.140 --> 07:03.180
So that's for the technical terms.

07:03.180 --> 07:10.230
And one thing is maybe I'm gonna just put that at the beginning of the loop.

07:10.230 --> 07:12.120
It doesn't really change anything okay.

07:12.150 --> 07:16.620
I just put it at the beginning so that I don't forget it later if I have more code.

07:16.620 --> 07:21.450
And also well, as I add code, maybe I'm going to mess up the indentation for that.

07:21.450 --> 07:26.460
So here I make sure that it's at the beginning of the loop and it's going to slow the loop.

07:26.460 --> 07:29.490
So we run it at 100 times per second.

07:29.520 --> 07:29.880
Great.

07:29.880 --> 07:31.020
Let me stop that.

07:31.020 --> 07:33.720
And now I want to show you something else.

07:33.720 --> 07:36.720
So that's not related to optimization or anything.

07:36.720 --> 07:42.900
It's just that we are going to use another functionality from the Gpio library to do actually the exact

07:42.900 --> 07:47.250
same thing, but the code is going to be much smaller.

07:47.250 --> 07:50.460
And that's the code we are going to use also for the next activity.

07:50.460 --> 07:55.450
So first, that was actually a very good practice for you to work on loops and conditions.

07:55.480 --> 07:57.160
Okay, so that's why I kept it like this.

07:57.160 --> 08:00.040
And now let's see how we can write this code differently.

08:00.070 --> 08:06.280
So what I'm going to do is I'm going to comment that like this comment.

08:06.280 --> 08:07.900
So we don't execute it.

08:07.900 --> 08:12.310
And there is in fact inside this button functionality.

08:12.340 --> 08:14.740
What we can do is just write button.

08:14.740 --> 08:21.400
So using this button here dot when pressed is equal to.

08:21.430 --> 08:24.700
And then just give a function here.

08:24.700 --> 08:27.730
So for example led dot on.

08:27.730 --> 08:31.840
And note that we don't provide any parentheses.

08:31.870 --> 08:34.120
Okay I'm gonna explain that just in one minute.

08:34.120 --> 08:38.950
So we can say that when the button is pressed when pressed we're going to turn the LED on.

08:38.950 --> 08:46.180
And then we can also do button dot when released LED dot off.

08:46.360 --> 08:48.850
And so this line is actually not going to stop.

08:48.850 --> 08:54.380
It's just going to register here this this function and this one as well.

08:54.380 --> 08:59.420
So if we just run the program like this, well it's going to run and then it's going to exit.

08:59.420 --> 09:00.950
So it's not going to do anything.

09:00.950 --> 09:05.270
So we need to somehow pause the program.

09:05.270 --> 09:08.720
We need to find a way to pause the execution of the program.

09:08.720 --> 09:12.830
And for this we can do from signal.

09:12.830 --> 09:16.730
So that's another library you can use import pause.

09:16.760 --> 09:23.360
You have the pause function from the signal library that we can just use at the end of our program here.

09:23.570 --> 09:26.090
Pause like that with parenthesis.

09:26.120 --> 09:26.450
All right.

09:26.450 --> 09:29.090
So what do we do here is we initialize the LED the button.

09:29.090 --> 09:32.660
And then we say that when the button is pressed we're going to turn on the LED.

09:32.660 --> 09:34.880
When the button is released we turn off the LED.

09:34.880 --> 09:36.470
And then we pause the program.

09:36.470 --> 09:43.820
And just to give you more details about this, well, this is what we can call a callback function.

09:43.820 --> 09:50.420
So to use a real life analogy, let's say that you need some information from somebody and they will

09:50.420 --> 09:53.850
tell you, okay, I will call you back when I have this information.

09:53.850 --> 09:57.510
So you give them your, for example, your phone number or your email.

09:57.540 --> 09:57.780
All right.

09:57.810 --> 10:01.170
So you kind of we're going to talk about registering your phone number.

10:01.170 --> 10:07.860
So you register your phone number so that when they have this information they can call you back using

10:07.860 --> 10:08.850
this phone number.

10:08.880 --> 10:09.090
Okay.

10:09.090 --> 10:10.380
So here there are two things.

10:10.380 --> 10:15.330
One is that there is an event that must happen and that is external.

10:15.510 --> 10:17.250
You don't really know when it's going to happen.

10:17.250 --> 10:18.600
It might happen at some point.

10:18.600 --> 10:22.800
And when this event is happening you want to be called back.

10:22.800 --> 10:25.080
So you want to be notified of this.

10:25.080 --> 10:26.970
And that's exactly what's happening here.

10:27.000 --> 10:30.150
You see we have this dot when pressed.

10:30.150 --> 10:33.600
So this is the external event when the button is pressed.

10:33.600 --> 10:36.150
But you don't know when the button is going to be pressed from the program.

10:36.180 --> 10:36.360
Okay.

10:36.390 --> 10:38.370
That's an external event.

10:38.400 --> 10:42.270
And so you register a callback and the callback is led1.

10:42.270 --> 10:47.460
So when the button is pressed here is what to do, what to call.

10:47.460 --> 10:50.520
And we're going to call this function led1.

10:50.520 --> 10:57.730
And just note it's one particular thing about callbacks is that you don't provide the parentheses,

10:57.730 --> 11:00.880
because if you provide a parentheses, you are calling the function right.

11:00.910 --> 11:05.530
Now here you are registering the function so it can be called later.

11:05.530 --> 11:11.890
So basically with this you just register those two functions that they can be called when this event

11:11.890 --> 11:14.530
or that event is happening.

11:14.560 --> 11:16.360
And then you make sure you pause the program.

11:16.360 --> 11:20.920
Because if you don't pause the program well it's just going to exit and nothing is going to happen.

11:20.920 --> 11:25.990
And now you can try to run this and it's going to be the exact same behavior as what we had before.

11:26.020 --> 11:26.530
All right.

11:26.530 --> 11:28.960
And you can see the code is much smaller actually.

11:28.960 --> 11:31.840
We don't even need the time library anymore.

11:31.840 --> 11:37.570
And if you look at if we go back to the task manager here, you will see that.

11:37.600 --> 11:43.210
So this is running and uh, well this is not using much CPU as well.

11:43.240 --> 11:43.420
Okay.

11:43.420 --> 11:46.000
So this code is already pretty much optimized.

11:46.000 --> 11:48.340
We don't need to add any time.sleep here.

11:48.370 --> 11:48.700
All right.

11:48.700 --> 11:51.820
And I will come back to this for the next activity of this course.
