Now that you have three LEDs and one pushbutton on your circuit, let's make a slightly bigger project.
Note that if you found the previous activities quite simple, well, this one is going to be a bit more
challenging.
If you can't find the solution by yourself.
Don't worry though, you can watch the solution and come back to it later to try again.
So here is the result you should get for this challenge when you start the program.
As you can see here, all the LEDs are powered off.
Then when you press the button here, you can see the first LED is powered on and all the other ones
are powered off.
If I press again now, I switch to the second LED.
So I turn off the first one and all the other ones and I turn on that one.
I press again, we go to the third led and then I press again.
We go back to the first, etc..
Okay.
And one important thing here is that this action is only when I press on the button, okay?
When I release, it doesn't do anything.
Uh, it doesn't matter how long I press on the button, okay?
It's just when I press on the button that we switch the LED.
So that is your challenge.
Now I'm going to give you a few tips, a few help to get started.
So first of all, if I go back to the previous activity, you see we have this button that when pressed
okay, this is going to register a function to call when the button is actually pressed okay.
So you will follow a similar structure here.
You will need to use the pose to pause the program.
And before that you will register a function to call when the button is pressed.
So for example you will have.
So you will have to create the button and everything.
And then you will do button when pressed is equal to and you will put a function.
So for example here I'm just going to create a function named switch LED.
Just like that I will write code in this later.
And then you can write switch LED okay.
And you don't put any parentheses.
Once again you don't put the parentheses because otherwise this would call the function.
Here you just want to give the name of the function.
All right.
And one more thing is that to keep track of which LED we are actually turning on, and the other ones
will be turned off.
We will add here I'm going to create a variable named LED index okay.
That starts at zero.
So you can use this variable to keep track of which LED is turned on okay.
So for now we start and we start at zero.
Because in Python as you already know, we start to count from zero.
And in this function in the switch LED that's called whenever you press the button.
What you can do is you can turn on whichever LED corresponds to the current index.
And then you can increment this index.
So you can add plus one for example, so that the next time the function is called again you can decide
to turn on the next LED.
All right.
So with this tip you can start thinking about designing a solution.
And just to finish here there's one thing you will need to do.
So previously I have told you that you can use a variable here, a global variable inside a function.
So you can definitely read this variable, meaning that you can print it, you can read it, you can
use it.
But if you want to modify this variable, if I want to do LED index is equal one for example this is
going to give you an error okay when it reaches that line because well when you use a global variable
inside a function you can read it.
But in order to write to it you first have to use this global keyword plus the name of the variable.
So we will start our switch LED function with global LED index.
Because we will not only read this variable, we will not only use it, but we will also modify it.
All right.
So I'm just going to remove that.
And this is basically the code that we will start from.
So of course you need to do all the imports and initialize the LEDs and all that stuff.
And now it's your time to practice.
So make sure you spend enough time on this activity before you go to the solution, and I will see you
in the next lesson.