This is the solution for the activity number seven, where we start from the code of the activity,
six, where we switch the LED when we press on the push button.
And we improve this with lists.
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.
And let's get started.
So I have written this for you to know that we have the Len function.
To know the length of a list, I'm going to remove that.
And what I have already done is to create this LED list here with LED one, two and three.
So what can we do?
First you see that we turn off all LEDs.
So that's going to be a small first exercise to practice a bit with lists and loops.
If we want to turn off all the LEDs here.
So we want to do something on each element of the list we can use.
So I'm going to remove this.
Those lines here we can use a for loop and we're going to iterate over each element.
So for.
I will need to put a local variable that I'm going to use for each element.
Let's call it LED.
For LED in LED list.
Okay.
So for element in list I put a colon I go to a new line.
And then when I do LED.
This LED corresponds to well each element.
So the for loop is going to be run here three times.
The first time we're going to get LED one and then led two and then led three.
So I can just do LED dot off.
Okay.
That's going to turn the LED off.
So the first one the second one and the third one.
So with this we can already initialize all LEDs.
And you can see that if we have one, two, 7 or 20 LEDs the code is going to be the same.
Then what I told you we keep this LED index at the end.
We keep those two lines.
So now the challenge is what do we do with this switch LED.
I'm going to keep this global lead index because we're going to still need to use and modify the LED
index variable.
But then I'm going to write the new code here and I will just comment on those lines.
So we will remove those.
But I will first comment those lines.
So comment.
Just so we can keep them for now to see what was the previous code.
And then when we are done I'm just going to remove everything.
So in the end let's look at what we did here and what kind of global behavior we can implement.
That's going to be valid for any amount of LEDs.
So every time what you can see is that we turn off all the LEDs and we turn on only the LED that corresponds
to the index.
All right.
So actually one we reset all the LEDs.
Two we set the LED that corresponds to the index.
So we turn on that LED.
And then three we do LED index plus one.
So that the next time it's going to be for the next LED.
All right.
So that's how we can see what we have to do.
We have three steps here.
First we turn off all the LEDs.
Then we turn on only the LED that corresponds to the index.
And then we increment that index.
And now the thing is maybe you implemented something a bit different.
Okay.
There is not only one solution okay.
This solution I'm showing you is my solution.
And well, it's a pretty efficient one.
But maybe you've come up with something else that works and that is as valid as my solution.
So let me implement that.
What I will do is I will just say that we turn off all LEDs.
So for LED in LED list we do LED dot off.
That's the that's the same thing we did here.
Okay.
And then I will do what I will do because we have a list and we have an index.
And we can get access directly to an element of a list with the index.
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.
So you can see if I do LED list zero I get the first LED.
If the index is one then I will get LED list with one, I will get the second element etc. so I can
just do LED list with the index.
I will get the LED and then I do dot on.
All right.
We turn off all LEDs and we turn on only one LED.
You might think that's a bit redundant because here let's say we want to turn on the first LED.
We're going to turn it off first and then turn it on.
So yes, there is an extra step that's going to be done.
But first it's going to happen very fast.
So it's not going to slow down the program or anything.
And it's going to make our program and our life much easier to do this.
All right.
So let's see what's going to happen for now when we press on the pause button this is going to be called
we turn off all the LEDs.
And then we're going to turn on the first one with index zero.
So that's going to be led one.
Great.
And then the next time we press on the push button same thing.
So now we need to make sure that we can go to the next LED.
How to do this.
Well we do LED index plus equals one.
So we add one to the LED index so that the next time.
So the LED index will be one.
When we call the switch LED again we're going to turn off all LEDs.
And then we turn on the one with the index one which is actually the second LED.
And then we're going to do plus one.
So the index is going to be two.
Which means that the third time we call the switch LED function, we're going to turn on the third LED.
All right.
But then you can see there's a problem.
If I just leave the code like this, then we're going to get a out of range.
Because next time the LED index will be three which is zero, one, two and three which doesn't exist.
So we're going to get an error.
So we need to go back at the beginning of the list when we have reached the end.
Now what is the end of the list.
We're not just going to write a if LED index is equal to three.
Because what we want to make it dynamic so that it works for any amount of LEDs.
Okay.
So instead of hard coding a value we will just check if the LED index is greater or equal than the length
of the list.
So that's why we use the Len function here.
So what is the length of the list here?
The length is three, but if your array or your list contains seven elements, then the length is going
to be seven so that you can count until seven.
So we check if the LED index is equal to or greater.
Actually just equal would work.
But just to be safe we can put greater or equal.
Then the length of the list.
We know we have reached the end, so we just need to do LED index is equal to zero okay.
This is a very common thing to do in Python.
You increment by one and you check when you reach the end.
When you reach the end, you go back to the beginning.
All right.
And with this we should be able to run the code.
So I can remove this.
You can see that now the code is even smaller than this.
So I'm going to remove that.
The code is smaller but it can also work for any amount of LEDs.
So let's run.
All right.
Activity seven.
And you can see all LEDs are turned off.
Now I press on the push button.
We have the first one, then the second one, then the third one.
And then we should go back to the first one.
All right.
And you can see it correctly works.
And just to show you that it can work with any amount of LEDs, let's say that instead of three LEDs
I only have two.
So I'm going to I'm going to just close the bracket here, and I'm going to use a comment here just
to comment the end of the line.
So you can see now our list only contains two LEDs.
Let's run this code instead.
Okay.
All LEDs are turned off.
I press on the push button.
We have the first one I press again, I have the second one.
And now look.
If I press again we go back to the first LED okay.
So we are just going between the two LEDs and the third one.
No, because the third one is not in the list.
All right.
So you can see the problem is much more dynamic now.
So I will do a quick recap of what we did.
And then there are still a few improvements in the code that we can do.
So let's recap first.
All right so we start with the LED index at zero.
We register this function that's going to be called whenever we press on the push button.
So the first time the index is zero we turn off all LEDs and we turn on the first LED which corresponds
to the first index.
Then we do plus one.
We check actually if the LED index, which is not equal to one, is greater or equal than the length
of the list.
So with this example, the length of the list is two.
So is one greater or equal than two.
This is false.
So we don't execute that line okay.
Now the next time we switch the button we go back here.
Now the LED index is one.
So we turn off all LEDs.
We turn on the LED with the index one, which is here, the LED number two.
And then we do plus one.
So the index is now equal to two.
And with this configuration with two LEDs well the length is going to be two.
So is two greater or equal than two.
Yes.
So we go back to index zero okay.
And we go back then to the first LED here.
All right.
So you can see it works with any number of LEDs.
Because then if I remove this comment and that bracket now when we reach LED index two the length is
three.
So we're going to go one more time.
All right.
If you have six LEDs you're going to go from 012345.
When you reach six you go back to zero.
Great.
And now just a few things to optimize the code a bit more.
So first of all, so I told you you could do this.
You just add you create the LEDs and then you add them in the list.
But here LED one.
We only use it to actually put it inside the list.
We don't use it anywhere else directly, so instead of creating a variable and putting it in the list,
we can just create it in the list.
So I can do LED here.
17.
All right.
That's going to create an LED variable and put it inside the LED list.
Same thing for LED two I can do LED with 27.
And then I can do LED with 22.
All right.
And that's it.
And I can remove those three lines.
So now let's say you have added another LED.
So you put LED like this.
You put the Gpio number and you don't need to modify anything else in the code.
All right.
So I'm going to remove that.
And the thing we can improve here is also that you see we run this for loop here and also here.
What do we do with this for loop?
Basically we turn off all LEDs.
We could also say that we reset the LEDs.
Okay, so instead of writing the code twice we could create a function.
So what I could do is I could create a function def reset LEDs like that.
No input parameters, no need.
And inside I will just put those lines.
So instead of repeating this block of code, every time that we need to run that we can just call reset
LEDs.
Okay.
And because I've created a function now I should actually call it.
So I should do reset LEDs.
And just to make things a bit, uh, well, a bit cleaner because here we create a function.
Then we call the function and then we define another function.
So instead of putting reset LEDs here I'm going to go down and I'm going to put it there.
Okay.
So what we do is we first initialize some stuff.
So maybe I can even put the variable here.
You can see I can move stuff around.
As long as I define everything before I use it I can move stuff around.
All right.
And with this it's better we create.
So we have our input.
Then we create our global variables.
Then we have our functions, and then we have our code which is just reset LEDs and then register this
function and then pause.
All right.
And what I have created this function here so I can use it here as well.
So I'm going to remove this block of code and just say reset LEDs like that.
And don't forget to put the parentheses here because we call the function great.
And you can see now the code is dynamic and optimized.
Okay.
So the code is 22 lines of code.
It's not a lot but it's doing quite a lot already.
Let's just check that it works so you can just run.
I'm just going to press just to check.
And what I see that it works.
So all good okay.
Don't forget to test your program.
If you improve them even if you didn't add a new functionality.
Great.
So that's the end of this activity.
It was a bit more complex than what we've done previously, but that was to show you that with just
a few lines of Python code, you can do a lot of things with Gpios.
Now don't worry if you found those two last activities quite difficult.
Okay, we're going to continue with more hardware components and with more concepts in the Raspberry
Pi.
So you may want to do those activities again in a few days.
But in the meantime, I encourage you to also continue with the course to discover the next few concepts
which will allow you to do really nice projects with your Raspberry Pi.