WEBVTT

00:00.050 --> 00:05.930
This is the solution for the activity number six, where you have to switch the LED that's turned on

00:05.930 --> 00:08.720
whenever you press on the push button.

00:08.720 --> 00:11.570
So we will start with all LEDs turned off.

00:11.570 --> 00:17.690
And then we will need to create some kind of algorithm inside this switch LED function, so that we

00:17.690 --> 00:23.030
can switch a different LED every time that we actually call the function.

00:23.030 --> 00:25.790
So every time the button is pressed.

00:25.790 --> 00:26.990
So let's get started.

00:26.990 --> 00:28.070
We will start with.

00:28.070 --> 00:31.460
So we will just ignore this code for now.

00:31.460 --> 00:35.390
And we will start with the imports and initializing the hardware.

00:35.390 --> 00:38.840
So we are going to import the Gpio zero library.

00:38.840 --> 00:41.030
So we will do from Gpio zero.

00:41.030 --> 00:42.650
And what do we need.

00:42.680 --> 00:46.850
Well we need an LED and then comma button.

00:47.090 --> 00:53.390
Then I'm also going to import from signal import pause okay.

00:53.420 --> 00:56.000
So that's the same thing we saw in activity five.

00:56.030 --> 01:00.940
You see from signal import pause so that we can actually Right.

01:00.940 --> 01:04.330
I'm just going to write it now pose here.

01:04.360 --> 01:04.750
Okay.

01:04.750 --> 01:10.150
So we are going to register this function that will be called when the button is pressed.

01:10.150 --> 01:14.440
But then we need to pose the program so that the program stays alive.

01:14.440 --> 01:17.410
And so we can actually see this function being called.

01:17.410 --> 01:21.400
And then well let's see if we need the time functionality or not.

01:21.400 --> 01:22.300
Probably not.

01:22.330 --> 01:27.160
But as we don't know if we need it right now, we probably don't need to import it.

01:27.160 --> 01:29.110
So I'm just going to keep those two imports.

01:29.110 --> 01:32.530
Then I will start to initialize my LEDs.

01:32.530 --> 01:35.860
So we have LED well here I can just call it LED.

01:35.890 --> 01:42.220
One for example is equal to LED and I will put 17.

01:42.220 --> 01:43.750
So that's going to be our first LED.

01:43.930 --> 01:51.430
And then led two is equal to LED with a Gpio number 27.

01:51.430 --> 01:58.150
And the third one LED three with so LED and Gpio number 22 okay.

01:58.180 --> 02:04.320
So just as you see on the image Those are the three gpios that we use for the LEDs.

02:04.320 --> 02:06.360
Then I can initialize the button.

02:06.360 --> 02:15.000
So I'm going to write it button like this is equal to button with uppercase and 26.

02:15.030 --> 02:15.480
All right.

02:15.480 --> 02:20.040
So we have our four components with our four gpios.

02:20.040 --> 02:22.320
And they are all configured here.

02:22.350 --> 02:28.140
Now the first thing I will do is I will actually power off all the LEDs.

02:28.140 --> 02:30.120
So it should be done by default.

02:30.120 --> 02:34.050
But just in case I want the LEDs to be powered off.

02:34.050 --> 02:45.780
So I'm just going to do led1 dot off and then LED two dot off and then led three dot off.

02:45.780 --> 02:50.760
And at this point you might think, well, maybe it would be nice to use a list, okay.

02:50.760 --> 02:56.670
And maybe some for loops and stuff like that to optimize the code instead of writing the same code three

02:56.670 --> 02:57.300
times.

02:57.300 --> 02:58.320
And you are right.

02:58.320 --> 03:01.460
And that's actually what we will do in the next activity.

03:01.460 --> 03:05.750
But for now, let's keep working with this without any lists.

03:05.750 --> 03:09.410
So we initialize the states for all the LEDs.

03:09.410 --> 03:14.450
We have our LED index, so we will start at zero.

03:14.480 --> 03:17.480
Then there is this function that I need to write the code in.

03:17.480 --> 03:23.120
And then we finish the program with button dot when pressed is equal to switch LED.

03:23.540 --> 03:26.690
So whenever the button is pressed this function is going to be called.

03:26.690 --> 03:34.610
And then we pause the program so that the program can keep monitoring the button state and call this

03:34.610 --> 03:35.360
function.

03:35.360 --> 03:39.950
And when we stop the program, well that's going to exit from the pose.

03:39.950 --> 03:43.820
So now let's write the code inside this function okay.

03:43.850 --> 03:47.810
So for now everything we've done is quite easy.

03:47.840 --> 03:50.510
That's things that we've done already before.

03:50.510 --> 03:54.170
But now what do we write inside this function.

03:54.170 --> 03:59.690
So we have three LEDs I started the LED index at zero.

03:59.690 --> 04:06.010
So let's say that the first LED will be zero, the second will be one and the third will be two.

04:06.040 --> 04:07.480
Okay, so that's an index.

04:07.480 --> 04:08.710
You start to count from zero.

04:08.740 --> 04:10.870
You go from zero, one and two.

04:10.900 --> 04:12.370
Because we have three LEDs.

04:12.370 --> 04:13.480
What I can do.

04:13.480 --> 04:19.660
So the algorithm I'm going to write is to say well we're going to check which index we are right now.

04:19.660 --> 04:26.380
And depending on that index we are going to turn on one LED only and turn off the other ones.

04:26.380 --> 04:33.190
And then we are going to increment the LED index by one so that the next time that we call the function

04:33.190 --> 04:33.430
again.

04:33.430 --> 04:39.970
So the next time that the user presses on the button, we can well, we can use another index and thus

04:39.970 --> 04:41.770
we can turn on another LED.

04:41.770 --> 04:46.000
So let's write this after the global LED index.

04:46.000 --> 04:53.230
Here I will need the user if because I want to check for example, if the LED index is equal with two

04:53.860 --> 04:56.830
equal signs very important two zero.

04:56.890 --> 04:58.420
So I will start with zero.

04:58.450 --> 05:00.690
If the LED index is zero.

05:00.720 --> 05:01.620
What do I do?

05:01.650 --> 05:05.160
Well, I will do led1 dot on.

05:05.520 --> 05:12.240
Then I do led two dot off and led three dot off.

05:12.270 --> 05:15.270
Okay, I turn on only one led.

05:15.300 --> 05:19.590
So the first one and I turn off the two other LEDs.

05:19.590 --> 05:21.090
And then what do I do?

05:21.120 --> 05:24.780
What I will do is I will do LED index.

05:24.780 --> 05:28.590
So I'm still in the if plus one.

05:29.070 --> 05:34.950
Because if you remember plus equal one is going to add this value to that variable.

05:34.950 --> 05:37.290
So let's say I press on the button.

05:37.320 --> 05:40.770
The first time switch LED is going to be called.

05:40.770 --> 05:43.650
We check is the LED index zero.

05:43.680 --> 05:45.840
Yes because that's the current value.

05:45.840 --> 05:47.700
So we turn on the first LED.

05:48.090 --> 05:54.450
And then we say that LED index is so the same value zero plus one.

05:54.450 --> 05:58.830
And note that I was talking about how to modify a variable.

05:58.830 --> 06:01.580
You need to use the global keyword here.

06:01.580 --> 06:03.410
And this is because of this line.

06:03.410 --> 06:09.560
If you try to run this code and you don't use the global, you will get an error here because you are

06:09.560 --> 06:12.770
trying to modify a global variable.

06:12.800 --> 06:14.870
So for that you need to use the global keyword.

06:14.900 --> 06:15.110
Okay.

06:15.200 --> 06:16.340
Just a tiny detail.

06:16.370 --> 06:16.820
Great.

06:16.820 --> 06:20.660
So that's going to work for the first iteration.

06:20.690 --> 06:20.900
Okay.

06:20.900 --> 06:23.630
So if we just run the program like this it's probably going to work.

06:23.630 --> 06:25.460
We're going to turn on the first LED.

06:25.730 --> 06:29.000
But then if we press again where nothing is going to happen.

06:29.000 --> 06:35.570
So I'm going to add an Elif here, Elif, and I'm going to do Elif.

06:35.600 --> 06:40.370
LED index is equal with two equal sign to one.

06:41.210 --> 06:48.140
So the first time LED index is zero, we do this and then we do plus one.

06:48.140 --> 06:51.770
We're going to exit from this if and exit from this function.

06:51.770 --> 06:56.090
But then the function is going to be called again the next time the button is pressed.

06:56.090 --> 07:00.140
And the next time the button is pressed, the LED index will not be zero.

07:00.140 --> 07:01.290
It will be one.

07:01.290 --> 07:02.700
So what do we do?

07:02.730 --> 07:04.920
LED one off.

07:05.250 --> 07:06.870
So we turn off the first LED.

07:08.040 --> 07:09.150
We turn on.

07:09.150 --> 07:11.970
So LED two dot on.

07:11.970 --> 07:13.740
We turn on the second LED.

07:13.740 --> 07:15.840
And we don't really need to do that.

07:15.840 --> 07:19.320
But I'm still going to be as explicit as possible.

07:19.320 --> 07:23.010
We also turn off the third LED because it's already off.

07:23.010 --> 07:25.140
But here it's more explicit.

07:25.170 --> 07:25.530
Great.

07:25.530 --> 07:34.800
And after I do this I will do the same thing LED index plus one, which means that after this LED index

07:34.800 --> 07:36.210
will be equal to two.

07:36.240 --> 07:42.390
And the next time we come to the switch led, then I'm simply going to add a Elif.

07:42.390 --> 07:45.480
And actually do I need to do an Elif?

07:45.690 --> 07:49.380
LED index is equal to two.

07:49.410 --> 07:54.840
We don't really need to do that, because that's going to be the last one, and we're not going to go

07:54.840 --> 07:55.650
after two.

07:55.680 --> 08:02.060
So I'm just going to put else that's going to work the same LED one dot off.

08:02.630 --> 08:05.510
LED two dot off and.

08:05.600 --> 08:07.850
LED three dot on.

08:07.880 --> 08:09.680
Okay, we turn on the third LED.

08:10.460 --> 08:13.610
So I put LS because I know that if it's not zero.

08:13.610 --> 08:18.290
If it's not one, it's going to be two because there is no other value after that.

08:18.320 --> 08:20.840
We don't have any fourth LED.

08:21.080 --> 08:21.830
Okay.

08:21.830 --> 08:27.740
And one important thing to do is when we reach the last index that we want, if I don't do anything

08:27.740 --> 08:29.600
else, well that's going to work the first time.

08:29.600 --> 08:34.250
We're going to turn on the first, second and third LED, but then nothing else is going to happen.

08:34.250 --> 08:38.720
So what we need to do is we need to go back to the first index.

08:38.720 --> 08:43.100
So in this case I will do LED index is equal.

08:43.100 --> 08:44.750
So just one equal okay.

08:44.780 --> 08:50.720
Because we assign a value is equal to zero and the function is complete.

08:50.720 --> 08:55.520
So the first time this function is called you're going to go to the first part of the.

08:55.550 --> 09:01.990
If the second time you're going to go to the second part of the if the third time you're going to go

09:01.990 --> 09:06.970
to the third part of the if and then the fourth time, you actually go back to the first one.

09:07.000 --> 09:08.860
ET cetera, et cetera.

09:08.890 --> 09:12.100
So looks like it's complete.

09:12.130 --> 09:18.250
Make sure that you have written the button when pressed is equal to switch LED without the parentheses

09:18.250 --> 09:18.880
here.

09:18.910 --> 09:19.420
Okay.

09:19.450 --> 09:20.830
Make sure it's the same name.

09:20.830 --> 09:24.760
And then make sure you have the pause at the end with the parenthesis.

09:24.760 --> 09:26.500
Because here you call the function.

09:26.530 --> 09:30.250
I'm going to run that and let's see what happens.

09:30.250 --> 09:33.490
So you can see now all LEDs are turned off.

09:33.520 --> 09:38.770
Now if I press and I release you see the first LED is turned on.

09:38.800 --> 09:42.760
I press again, the second LED and I press again.

09:42.940 --> 09:44.290
We have the third LED.

09:44.470 --> 09:47.050
And now if I press again we go back to the first one.

09:47.080 --> 09:47.890
ET cetera.

09:47.920 --> 09:48.820
ET cetera.

09:48.850 --> 09:49.630
Okay.

09:49.630 --> 09:57.040
And there's just one more thing I want to show you is that, um, let's see if that works at some point.

09:58.150 --> 10:01.860
So, of course, it never works when you want to show something to people.

10:03.060 --> 10:04.170
Maybe.

10:09.990 --> 10:12.090
Okay, you can see here what happened.

10:12.090 --> 10:13.110
So it was very quick.

10:13.110 --> 10:16.620
But what happened is that the blue LED was turned on.

10:16.620 --> 10:22.920
And then I pressed on the button and very fast it went to the green one and the red one again.

10:22.920 --> 10:24.720
But I just pressed once.

10:24.750 --> 10:25.290
Okay.

10:25.290 --> 10:26.850
So what happened here?

10:26.850 --> 10:32.040
Well, this is an issue that's actually a mechanical issue with the push button.

10:32.040 --> 10:32.880
It's not really an issue.

10:32.910 --> 10:35.730
It's just a behavior you can expect from a push button.

10:35.730 --> 10:41.700
So when you press on the button, what can happen is that the button is actually bouncing.

10:41.760 --> 10:41.940
Okay.

10:41.970 --> 10:44.430
So the mechanical part is bouncing a bit.

10:44.430 --> 10:49.950
And in some occasions it might look like you have actually pressed on the button twice.

10:49.950 --> 10:53.730
So it's not a software bug, it's just how the hardware is.

10:53.760 --> 10:54.270
Okay.

10:54.300 --> 10:59.070
And so depending on how fast you read from the push button from the software, maybe you're going to

10:59.070 --> 11:04.430
see that you press twice on the button, but actually you just press once, but the button is still

11:04.430 --> 11:06.230
physically bouncing.

11:06.260 --> 11:07.820
And so how to solve that?

11:07.850 --> 11:10.340
Well, here it's very easy with this library.

11:10.370 --> 11:12.860
You can go back to the button here.

11:12.860 --> 11:15.620
When we create the button we need to give the Gpio.

11:15.620 --> 11:18.050
But then we can give another argument to that.

11:18.050 --> 11:19.370
So I'm going to put a comma.

11:19.370 --> 11:23.450
And this argument is called bounce time.

11:23.450 --> 11:27.380
So you can just write bounce time like this is equal to.

11:27.410 --> 11:34.280
And let's give a value for example 0.05 which is 50 milliseconds.

11:34.280 --> 11:35.750
And what is this doing.

11:35.750 --> 11:41.390
Well it's going to say to the software that whenever we read a value from the push button, for example,

11:41.390 --> 11:46.910
if we see that the button is pressed, we're going to ignore the next values, the next few values over

11:46.910 --> 11:50.210
the next here 50 milliseconds.

11:50.210 --> 11:55.160
And this is very convenient because over the next 50 milliseconds that's a quite common value to use.

11:55.160 --> 11:57.530
You might have some physical bounces.

11:57.530 --> 12:03.370
So what you do is you just ignore those bounces, and it's only going to start to monitor the button

12:03.370 --> 12:05.800
again after 50 milliseconds.

12:05.830 --> 12:06.190
All right.

12:06.220 --> 12:09.550
So for the software it's gonna prevent the bounce to happen.

12:09.580 --> 12:12.940
I mean the bounce is still going to happen, but it's not going to read the bounce.

12:12.940 --> 12:14.170
And then for you as the user.

12:14.200 --> 12:17.650
Well 50 milliseconds is so fast you won't even see it.

12:17.650 --> 12:24.370
That basically means that you could press the button 20 times per second and it would still work correctly.

12:24.400 --> 12:27.880
Okay, but now I challenge you to press the button 20 times per second.

12:27.910 --> 12:28.120
Okay.

12:28.150 --> 12:30.400
As a human, it's very difficult to do that.

12:30.400 --> 12:32.950
So well, that's a small parenthesis.

12:32.950 --> 12:38.680
You can add this bounce time, which is going to solve the bounce issue that we have just seen previously.

12:38.680 --> 12:43.480
And if you want to learn more about this bounce well you can find many resources online.

12:43.510 --> 12:43.720
Okay.

12:43.750 --> 12:45.880
You just type push button bounce.

12:45.880 --> 12:49.480
You will find a lot of things related to Arduino as well.

12:49.510 --> 12:49.960
All right.

12:50.080 --> 12:54.340
But for this course here I'm going to keep this explanation here.

12:54.370 --> 12:54.970
Great.

12:54.970 --> 12:57.610
And this activity is now complete.

12:57.610 --> 13:00.820
And I have another one for you just in the next video.
