WEBVTT

00:00.080 --> 00:06.380
This is the solution for the activity number seven, where we start from the code of the activity,

00:06.410 --> 00:10.400
six, where we switch the LED when we press on the push button.

00:10.400 --> 00:14.120
And we improve this with lists.

00:14.120 --> 00:21.560
So the goal is to make that code work for any amount of LEDs and to have a code that's as clean as possible.

00:21.560 --> 00:22.970
And let's get started.

00:22.970 --> 00:28.130
So I have written this for you to know that we have the Len function.

00:28.130 --> 00:30.950
To know the length of a list, I'm going to remove that.

00:31.400 --> 00:38.930
And what I have already done is to create this LED list here with LED one, two and three.

00:38.930 --> 00:40.280
So what can we do?

00:40.310 --> 00:43.400
First you see that we turn off all LEDs.

00:43.400 --> 00:49.070
So that's going to be a small first exercise to practice a bit with lists and loops.

00:49.070 --> 00:51.680
If we want to turn off all the LEDs here.

00:51.680 --> 00:56.360
So we want to do something on each element of the list we can use.

00:56.360 --> 00:58.040
So I'm going to remove this.

00:58.490 --> 01:04.380
Those lines here we can use a for loop and we're going to iterate over each element.

01:04.410 --> 01:05.430
So for.

01:05.460 --> 01:09.990
I will need to put a local variable that I'm going to use for each element.

01:10.020 --> 01:11.070
Let's call it LED.

01:11.490 --> 01:14.550
For LED in LED list.

01:14.580 --> 01:15.150
Okay.

01:15.150 --> 01:21.180
So for element in list I put a colon I go to a new line.

01:21.180 --> 01:22.680
And then when I do LED.

01:23.250 --> 01:26.970
This LED corresponds to well each element.

01:26.970 --> 01:29.970
So the for loop is going to be run here three times.

01:30.000 --> 01:35.670
The first time we're going to get LED one and then led two and then led three.

01:35.670 --> 01:38.730
So I can just do LED dot off.

01:38.940 --> 01:39.570
Okay.

01:39.600 --> 01:41.940
That's going to turn the LED off.

01:41.940 --> 01:44.340
So the first one the second one and the third one.

01:44.340 --> 01:47.520
So with this we can already initialize all LEDs.

01:47.520 --> 01:54.240
And you can see that if we have one, two, 7 or 20 LEDs the code is going to be the same.

01:54.240 --> 01:58.470
Then what I told you we keep this LED index at the end.

01:58.500 --> 01:59.890
We keep those two lines.

01:59.890 --> 02:05.470
So now the challenge is what do we do with this switch LED.

02:05.860 --> 02:12.730
I'm going to keep this global lead index because we're going to still need to use and modify the LED

02:12.730 --> 02:13.840
index variable.

02:13.870 --> 02:19.720
But then I'm going to write the new code here and I will just comment on those lines.

02:19.780 --> 02:21.430
So we will remove those.

02:21.430 --> 02:25.330
But I will first comment those lines.

02:25.330 --> 02:26.650
So comment.

02:29.350 --> 02:34.210
Just so we can keep them for now to see what was the previous code.

02:34.210 --> 02:37.000
And then when we are done I'm just going to remove everything.

02:37.000 --> 02:45.370
So in the end let's look at what we did here and what kind of global behavior we can implement.

02:45.370 --> 02:47.800
That's going to be valid for any amount of LEDs.

02:47.800 --> 02:56.500
So every time what you can see is that we turn off all the LEDs and we turn on only the LED that corresponds

02:56.500 --> 02:57.640
to the index.

02:57.670 --> 02:58.000
All right.

02:58.030 --> 03:00.860
So actually one we reset all the LEDs.

03:00.860 --> 03:04.400
Two we set the LED that corresponds to the index.

03:04.400 --> 03:05.690
So we turn on that LED.

03:05.690 --> 03:09.590
And then three we do LED index plus one.

03:09.590 --> 03:12.830
So that the next time it's going to be for the next LED.

03:13.010 --> 03:13.280
All right.

03:13.310 --> 03:16.190
So that's how we can see what we have to do.

03:16.220 --> 03:17.660
We have three steps here.

03:17.660 --> 03:19.520
First we turn off all the LEDs.

03:19.520 --> 03:22.880
Then we turn on only the LED that corresponds to the index.

03:22.880 --> 03:25.460
And then we increment that index.

03:25.460 --> 03:30.080
And now the thing is maybe you implemented something a bit different.

03:30.110 --> 03:30.530
Okay.

03:30.560 --> 03:32.840
There is not only one solution okay.

03:32.870 --> 03:35.270
This solution I'm showing you is my solution.

03:35.270 --> 03:37.550
And well, it's a pretty efficient one.

03:37.550 --> 03:43.460
But maybe you've come up with something else that works and that is as valid as my solution.

03:43.460 --> 03:45.440
So let me implement that.

03:45.440 --> 03:49.100
What I will do is I will just say that we turn off all LEDs.

03:49.100 --> 03:57.050
So for LED in LED list we do LED dot off.

03:57.810 --> 03:59.850
That's the that's the same thing we did here.

03:59.880 --> 04:00.480
Okay.

04:00.660 --> 04:07.020
And then I will do what I will do because we have a list and we have an index.

04:07.020 --> 04:11.820
And we can get access directly to an element of a list with the index.

04:11.820 --> 04:21.030
So if I do LED list and I put those brackets and I do LED index the first time, it's going to be zero.

04:21.030 --> 04:25.950
So you can see if I do LED list zero I get the first LED.

04:26.070 --> 04:33.750
If the index is one then I will get LED list with one, I will get the second element etc. so I can

04:33.750 --> 04:36.120
just do LED list with the index.

04:36.120 --> 04:39.330
I will get the LED and then I do dot on.

04:39.870 --> 04:40.320
All right.

04:40.320 --> 04:45.120
We turn off all LEDs and we turn on only one LED.

04:45.210 --> 04:51.090
You might think that's a bit redundant because here let's say we want to turn on the first LED.

04:51.120 --> 04:54.000
We're going to turn it off first and then turn it on.

04:54.000 --> 04:57.350
So yes, there is an extra step that's going to be done.

04:57.350 --> 05:00.050
But first it's going to happen very fast.

05:00.050 --> 05:02.900
So it's not going to slow down the program or anything.

05:02.900 --> 05:06.380
And it's going to make our program and our life much easier to do this.

05:06.410 --> 05:06.890
All right.

05:06.890 --> 05:12.320
So let's see what's going to happen for now when we press on the pause button this is going to be called

05:12.320 --> 05:14.210
we turn off all the LEDs.

05:14.210 --> 05:18.470
And then we're going to turn on the first one with index zero.

05:18.470 --> 05:19.760
So that's going to be led one.

05:19.790 --> 05:20.450
Great.

05:20.480 --> 05:23.840
And then the next time we press on the push button same thing.

05:23.840 --> 05:28.160
So now we need to make sure that we can go to the next LED.

05:28.190 --> 05:29.090
How to do this.

05:29.090 --> 05:33.590
Well we do LED index plus equals one.

05:33.590 --> 05:37.130
So we add one to the LED index so that the next time.

05:37.130 --> 05:39.050
So the LED index will be one.

05:39.050 --> 05:43.880
When we call the switch LED again we're going to turn off all LEDs.

05:43.880 --> 05:49.940
And then we turn on the one with the index one which is actually the second LED.

05:49.940 --> 05:51.740
And then we're going to do plus one.

05:51.740 --> 05:53.930
So the index is going to be two.

05:53.960 --> 06:00.620
Which means that the third time we call the switch LED function, we're going to turn on the third LED.

06:00.680 --> 06:00.950
All right.

06:00.980 --> 06:02.210
But then you can see there's a problem.

06:02.210 --> 06:07.670
If I just leave the code like this, then we're going to get a out of range.

06:07.670 --> 06:14.990
Because next time the LED index will be three which is zero, one, two and three which doesn't exist.

06:14.990 --> 06:16.340
So we're going to get an error.

06:16.340 --> 06:21.590
So we need to go back at the beginning of the list when we have reached the end.

06:21.620 --> 06:23.450
Now what is the end of the list.

06:23.480 --> 06:33.230
We're not just going to write a if LED index is equal to three.

06:33.260 --> 06:39.020
Because what we want to make it dynamic so that it works for any amount of LEDs.

06:39.050 --> 06:39.260
Okay.

06:39.290 --> 06:48.320
So instead of hard coding a value we will just check if the LED index is greater or equal than the length

06:48.320 --> 06:49.880
of the list.

06:49.880 --> 06:53.540
So that's why we use the Len function here.

06:55.020 --> 06:57.600
So what is the length of the list here?

06:57.600 --> 07:04.350
The length is three, but if your array or your list contains seven elements, then the length is going

07:04.350 --> 07:08.280
to be seven so that you can count until seven.

07:08.280 --> 07:12.270
So we check if the LED index is equal to or greater.

07:12.300 --> 07:15.180
Actually just equal would work.

07:15.480 --> 07:19.140
But just to be safe we can put greater or equal.

07:19.140 --> 07:20.670
Then the length of the list.

07:20.700 --> 07:27.090
We know we have reached the end, so we just need to do LED index is equal to zero okay.

07:27.120 --> 07:29.460
This is a very common thing to do in Python.

07:29.550 --> 07:32.670
You increment by one and you check when you reach the end.

07:32.670 --> 07:34.980
When you reach the end, you go back to the beginning.

07:35.010 --> 07:35.520
All right.

07:35.520 --> 07:39.570
And with this we should be able to run the code.

07:39.570 --> 07:42.210
So I can remove this.

07:42.210 --> 07:46.680
You can see that now the code is even smaller than this.

07:46.740 --> 07:47.940
So I'm going to remove that.

07:47.970 --> 07:53.670
The code is smaller but it can also work for any amount of LEDs.

07:53.670 --> 07:55.730
So let's run.

07:56.690 --> 07:57.410
All right.

07:57.440 --> 07:57.860
All right.

07:57.890 --> 07:59.000
Activity seven.

07:59.150 --> 08:02.150
And you can see all LEDs are turned off.

08:02.150 --> 08:03.770
Now I press on the push button.

08:03.770 --> 08:07.460
We have the first one, then the second one, then the third one.

08:07.460 --> 08:09.800
And then we should go back to the first one.

08:09.830 --> 08:10.430
All right.

08:10.430 --> 08:12.500
And you can see it correctly works.

08:12.500 --> 08:18.140
And just to show you that it can work with any amount of LEDs, let's say that instead of three LEDs

08:18.140 --> 08:19.370
I only have two.

08:19.550 --> 08:25.580
So I'm going to I'm going to just close the bracket here, and I'm going to use a comment here just

08:25.580 --> 08:27.020
to comment the end of the line.

08:27.020 --> 08:31.610
So you can see now our list only contains two LEDs.

08:31.820 --> 08:33.500
Let's run this code instead.

08:34.790 --> 08:35.330
Okay.

08:35.360 --> 08:37.160
All LEDs are turned off.

08:37.160 --> 08:38.360
I press on the push button.

08:38.360 --> 08:42.290
We have the first one I press again, I have the second one.

08:42.290 --> 08:43.160
And now look.

08:43.160 --> 08:47.090
If I press again we go back to the first LED okay.

08:47.120 --> 08:50.870
So we are just going between the two LEDs and the third one.

08:50.870 --> 08:54.360
No, because the third one is not in the list.

08:55.170 --> 08:55.500
All right.

08:55.500 --> 08:58.590
So you can see the problem is much more dynamic now.

08:58.800 --> 09:00.900
So I will do a quick recap of what we did.

09:00.900 --> 09:04.830
And then there are still a few improvements in the code that we can do.

09:04.830 --> 09:07.170
So let's recap first.

09:07.200 --> 09:10.680
All right so we start with the LED index at zero.

09:10.710 --> 09:15.090
We register this function that's going to be called whenever we press on the push button.

09:15.090 --> 09:23.190
So the first time the index is zero we turn off all LEDs and we turn on the first LED which corresponds

09:23.190 --> 09:25.320
to the first index.

09:25.320 --> 09:27.300
Then we do plus one.

09:27.300 --> 09:34.230
We check actually if the LED index, which is not equal to one, is greater or equal than the length

09:34.230 --> 09:35.160
of the list.

09:35.160 --> 09:37.740
So with this example, the length of the list is two.

09:37.770 --> 09:40.230
So is one greater or equal than two.

09:40.260 --> 09:41.250
This is false.

09:41.250 --> 09:43.200
So we don't execute that line okay.

09:43.230 --> 09:46.590
Now the next time we switch the button we go back here.

09:46.620 --> 09:49.350
Now the LED index is one.

09:49.350 --> 09:51.360
So we turn off all LEDs.

09:51.360 --> 09:56.200
We turn on the LED with the index one, which is here, the LED number two.

09:56.230 --> 09:57.430
And then we do plus one.

09:57.430 --> 09:59.710
So the index is now equal to two.

09:59.740 --> 10:04.540
And with this configuration with two LEDs well the length is going to be two.

10:04.570 --> 10:07.450
So is two greater or equal than two.

10:07.480 --> 10:07.900
Yes.

10:07.900 --> 10:11.020
So we go back to index zero okay.

10:11.050 --> 10:14.290
And we go back then to the first LED here.

10:14.320 --> 10:14.650
All right.

10:14.680 --> 10:17.860
So you can see it works with any number of LEDs.

10:17.890 --> 10:27.460
Because then if I remove this comment and that bracket now when we reach LED index two the length is

10:27.460 --> 10:28.150
three.

10:28.180 --> 10:30.670
So we're going to go one more time.

10:30.700 --> 10:31.000
All right.

10:31.030 --> 10:35.560
If you have six LEDs you're going to go from 012345.

10:35.560 --> 10:37.630
When you reach six you go back to zero.

10:37.660 --> 10:38.140
Great.

10:38.140 --> 10:42.670
And now just a few things to optimize the code a bit more.

10:42.670 --> 10:46.480
So first of all, so I told you you could do this.

10:46.480 --> 10:50.920
You just add you create the LEDs and then you add them in the list.

10:50.920 --> 10:52.550
But here LED one.

10:52.550 --> 10:55.820
We only use it to actually put it inside the list.

10:55.850 --> 11:02.990
We don't use it anywhere else directly, so instead of creating a variable and putting it in the list,

11:03.020 --> 11:05.870
we can just create it in the list.

11:05.870 --> 11:08.330
So I can do LED here.

11:08.360 --> 11:09.440
17.

11:10.340 --> 11:10.670
All right.

11:10.700 --> 11:15.110
That's going to create an LED variable and put it inside the LED list.

11:15.170 --> 11:23.930
Same thing for LED two I can do LED with 27.

11:25.130 --> 11:28.790
And then I can do LED with 22.

11:29.510 --> 11:30.020
All right.

11:30.020 --> 11:30.920
And that's it.

11:30.920 --> 11:34.430
And I can remove those three lines.

11:34.910 --> 11:37.370
So now let's say you have added another LED.

11:37.820 --> 11:40.160
So you put LED like this.

11:40.160 --> 11:45.680
You put the Gpio number and you don't need to modify anything else in the code.

11:47.240 --> 11:47.480
All right.

11:47.480 --> 11:48.500
So I'm going to remove that.

11:48.500 --> 11:57.420
And the thing we can improve here is also that you see we run this for loop here and also here.

11:57.420 --> 11:59.130
What do we do with this for loop?

11:59.130 --> 12:02.340
Basically we turn off all LEDs.

12:02.340 --> 12:05.670
We could also say that we reset the LEDs.

12:05.700 --> 12:11.490
Okay, so instead of writing the code twice we could create a function.

12:11.580 --> 12:19.920
So what I could do is I could create a function def reset LEDs like that.

12:20.130 --> 12:22.560
No input parameters, no need.

12:22.560 --> 12:25.890
And inside I will just put those lines.

12:29.220 --> 12:35.190
So instead of repeating this block of code, every time that we need to run that we can just call reset

12:35.220 --> 12:36.000
LEDs.

12:36.030 --> 12:36.420
Okay.

12:36.450 --> 12:39.150
And because I've created a function now I should actually call it.

12:39.150 --> 12:41.730
So I should do reset LEDs.

12:42.930 --> 12:48.660
And just to make things a bit, uh, well, a bit cleaner because here we create a function.

12:48.660 --> 12:51.670
Then we call the function and then we define another function.

12:51.670 --> 12:59.470
So instead of putting reset LEDs here I'm going to go down and I'm going to put it there.

12:59.740 --> 13:00.520
Okay.

13:00.520 --> 13:06.220
So what we do is we first initialize some stuff.

13:06.220 --> 13:09.400
So maybe I can even put the variable here.

13:09.400 --> 13:11.260
You can see I can move stuff around.

13:11.260 --> 13:16.870
As long as I define everything before I use it I can move stuff around.

13:17.560 --> 13:17.830
All right.

13:17.830 --> 13:19.750
And with this it's better we create.

13:19.750 --> 13:21.220
So we have our input.

13:21.220 --> 13:23.590
Then we create our global variables.

13:23.590 --> 13:30.490
Then we have our functions, and then we have our code which is just reset LEDs and then register this

13:30.490 --> 13:32.440
function and then pause.

13:32.470 --> 13:33.190
All right.

13:33.190 --> 13:37.810
And what I have created this function here so I can use it here as well.

13:37.810 --> 13:44.500
So I'm going to remove this block of code and just say reset LEDs like that.

13:44.500 --> 13:48.760
And don't forget to put the parentheses here because we call the function great.

13:48.760 --> 13:52.950
And you can see now the code is dynamic and optimized.

13:52.980 --> 13:53.250
Okay.

13:53.280 --> 13:55.560
So the code is 22 lines of code.

13:55.560 --> 13:59.490
It's not a lot but it's doing quite a lot already.

13:59.490 --> 14:02.760
Let's just check that it works so you can just run.

14:02.790 --> 14:04.860
I'm just going to press just to check.

14:04.860 --> 14:07.560
And what I see that it works.

14:07.560 --> 14:09.540
So all good okay.

14:09.570 --> 14:11.100
Don't forget to test your program.

14:11.100 --> 14:14.250
If you improve them even if you didn't add a new functionality.

14:14.280 --> 14:14.700
Great.

14:14.700 --> 14:16.350
So that's the end of this activity.

14:16.380 --> 14:21.810
It was a bit more complex than what we've done previously, but that was to show you that with just

14:21.810 --> 14:26.340
a few lines of Python code, you can do a lot of things with Gpios.

14:26.340 --> 14:31.500
Now don't worry if you found those two last activities quite difficult.

14:31.530 --> 14:36.090
Okay, we're going to continue with more hardware components and with more concepts in the Raspberry

14:36.090 --> 14:36.480
Pi.

14:36.510 --> 14:39.660
So you may want to do those activities again in a few days.

14:39.660 --> 14:45.210
But in the meantime, I encourage you to also continue with the course to discover the next few concepts

14:45.210 --> 14:49.560
which will allow you to do really nice projects with your Raspberry Pi.
