This is the solution for the activity number six, where you have to switch the LED that's turned on
whenever you press on the push button.
So we will start with all LEDs turned off.
And then we will need to create some kind of algorithm inside this switch LED function, so that we
can switch a different LED every time that we actually call the function.
So every time the button is pressed.
So let's get started.
We will start with.
So we will just ignore this code for now.
And we will start with the imports and initializing the hardware.
So we are going to import the Gpio zero library.
So we will do from Gpio zero.
And what do we need.
Well we need an LED and then comma button.
Then I'm also going to import from signal import pause okay.
So that's the same thing we saw in activity five.
You see from signal import pause so that we can actually Right.
I'm just going to write it now pose here.
Okay.
So we are going to register this function that will be called when the button is pressed.
But then we need to pose the program so that the program stays alive.
And so we can actually see this function being called.
And then well let's see if we need the time functionality or not.
Probably not.
But as we don't know if we need it right now, we probably don't need to import it.
So I'm just going to keep those two imports.
Then I will start to initialize my LEDs.
So we have LED well here I can just call it LED.
One for example is equal to LED and I will put 17.
So that's going to be our first LED.
And then led two is equal to LED with a Gpio number 27.
And the third one LED three with so LED and Gpio number 22 okay.
So just as you see on the image Those are the three gpios that we use for the LEDs.
Then I can initialize the button.
So I'm going to write it button like this is equal to button with uppercase and 26.
All right.
So we have our four components with our four gpios.
And they are all configured here.
Now the first thing I will do is I will actually power off all the LEDs.
So it should be done by default.
But just in case I want the LEDs to be powered off.
So I'm just going to do led1 dot off and then LED two dot off and then led three dot off.
And at this point you might think, well, maybe it would be nice to use a list, okay.
And maybe some for loops and stuff like that to optimize the code instead of writing the same code three
times.
And you are right.
And that's actually what we will do in the next activity.
But for now, let's keep working with this without any lists.
So we initialize the states for all the LEDs.
We have our LED index, so we will start at zero.
Then there is this function that I need to write the code in.
And then we finish the program with button dot when pressed is equal to switch LED.
So whenever the button is pressed this function is going to be called.
And then we pause the program so that the program can keep monitoring the button state and call this
function.
And when we stop the program, well that's going to exit from the pose.
So now let's write the code inside this function okay.
So for now everything we've done is quite easy.
That's things that we've done already before.
But now what do we write inside this function.
So we have three LEDs I started the LED index at zero.
So let's say that the first LED will be zero, the second will be one and the third will be two.
Okay, so that's an index.
You start to count from zero.
You go from zero, one and two.
Because we have three LEDs.
What I can do.
So the algorithm I'm going to write is to say well we're going to check which index we are right now.
And depending on that index we are going to turn on one LED only and turn off the other ones.
And then we are going to increment the LED index by one so that the next time that we call the function
again.
So the next time that the user presses on the button, we can well, we can use another index and thus
we can turn on another LED.
So let's write this after the global LED index.
Here I will need the user if because I want to check for example, if the LED index is equal with two
equal signs very important two zero.
So I will start with zero.
If the LED index is zero.
What do I do?
Well, I will do led1 dot on.
Then I do led two dot off and led three dot off.
Okay, I turn on only one led.
So the first one and I turn off the two other LEDs.
And then what do I do?
What I will do is I will do LED index.
So I'm still in the if plus one.
Because if you remember plus equal one is going to add this value to that variable.
So let's say I press on the button.
The first time switch LED is going to be called.
We check is the LED index zero.
Yes because that's the current value.
So we turn on the first LED.
And then we say that LED index is so the same value zero plus one.
And note that I was talking about how to modify a variable.
You need to use the global keyword here.
And this is because of this line.
If you try to run this code and you don't use the global, you will get an error here because you are
trying to modify a global variable.
So for that you need to use the global keyword.
Okay.
Just a tiny detail.
Great.
So that's going to work for the first iteration.
Okay.
So if we just run the program like this it's probably going to work.
We're going to turn on the first LED.
But then if we press again where nothing is going to happen.
So I'm going to add an Elif here, Elif, and I'm going to do Elif.
LED index is equal with two equal sign to one.
So the first time LED index is zero, we do this and then we do plus one.
We're going to exit from this if and exit from this function.
But then the function is going to be called again the next time the button is pressed.
And the next time the button is pressed, the LED index will not be zero.
It will be one.
So what do we do?
LED one off.
So we turn off the first LED.
We turn on.
So LED two dot on.
We turn on the second LED.
And we don't really need to do that.
But I'm still going to be as explicit as possible.
We also turn off the third LED because it's already off.
But here it's more explicit.
Great.
And after I do this I will do the same thing LED index plus one, which means that after this LED index
will be equal to two.
And the next time we come to the switch led, then I'm simply going to add a Elif.
And actually do I need to do an Elif?
LED index is equal to two.
We don't really need to do that, because that's going to be the last one, and we're not going to go
after two.
So I'm just going to put else that's going to work the same LED one dot off.
LED two dot off and.
LED three dot on.
Okay, we turn on the third LED.
So I put LS because I know that if it's not zero.
If it's not one, it's going to be two because there is no other value after that.
We don't have any fourth LED.
Okay.
And one important thing to do is when we reach the last index that we want, if I don't do anything
else, well that's going to work the first time.
We're going to turn on the first, second and third LED, but then nothing else is going to happen.
So what we need to do is we need to go back to the first index.
So in this case I will do LED index is equal.
So just one equal okay.
Because we assign a value is equal to zero and the function is complete.
So the first time this function is called you're going to go to the first part of the.
If the second time you're going to go to the second part of the if the third time you're going to go
to the third part of the if and then the fourth time, you actually go back to the first one.
ET cetera, et cetera.
So looks like it's complete.
Make sure that you have written the button when pressed is equal to switch LED without the parentheses
here.
Okay.
Make sure it's the same name.
And then make sure you have the pause at the end with the parenthesis.
Because here you call the function.
I'm going to run that and let's see what happens.
So you can see now all LEDs are turned off.
Now if I press and I release you see the first LED is turned on.
I press again, the second LED and I press again.
We have the third LED.
And now if I press again we go back to the first one.
ET cetera.
Okay.
And there's just one more thing I want to show you is that, um, let's see if that works at some point.
So, of course, it never works when you want to show something to people.
Maybe.
Okay, you can see here what happened.
So it was very quick.
But what happened is that the blue LED was turned on.
And then I pressed on the button and very fast it went to the green one and the red one again.
But I just pressed once.
Okay.
So what happened here?
Well, this is an issue that's actually a mechanical issue with the push button.
It's not really an issue.
It's just a behavior you can expect from a push button.
So when you press on the button, what can happen is that the button is actually bouncing.
Okay.
So the mechanical part is bouncing a bit.
And in some occasions it might look like you have actually pressed on the button twice.
So it's not a software bug, it's just how the hardware is.
Okay.
And so depending on how fast you read from the push button from the software, maybe you're going to
see that you press twice on the button, but actually you just press once, but the button is still
physically bouncing.
And so how to solve that?
Well, here it's very easy with this library.
You can go back to the button here.
When we create the button we need to give the Gpio.
But then we can give another argument to that.
So I'm going to put a comma.
And this argument is called bounce time.
So you can just write bounce time like this is equal to.
And let's give a value for example 0.05 which is 50 milliseconds.
And what is this doing.
Well it's going to say to the software that whenever we read a value from the push button, for example,
if we see that the button is pressed, we're going to ignore the next values, the next few values over
the next here 50 milliseconds.
And this is very convenient because over the next 50 milliseconds that's a quite common value to use.
You might have some physical bounces.
So what you do is you just ignore those bounces, and it's only going to start to monitor the button
again after 50 milliseconds.
All right.
So for the software it's gonna prevent the bounce to happen.
I mean the bounce is still going to happen, but it's not going to read the bounce.
And then for you as the user.
Well 50 milliseconds is so fast you won't even see it.
That basically means that you could press the button 20 times per second and it would still work correctly.
Okay, but now I challenge you to press the button 20 times per second.
Okay.
As a human, it's very difficult to do that.
So well, that's a small parenthesis.
You can add this bounce time, which is going to solve the bounce issue that we have just seen previously.
And if you want to learn more about this bounce well you can find many resources online.
Okay.
You just type push button bounce.
You will find a lot of things related to Arduino as well.
All right.
But for this course here I'm going to keep this explanation here.
Great.
And this activity is now complete.
And I have another one for you just in the next video.