WEBVTT

00:00.080 --> 00:06.320
Now that you have three LEDs and one pushbutton on your circuit, let's make a slightly bigger project.

00:06.350 --> 00:11.510
Note that if you found the previous activities quite simple, well, this one is going to be a bit more

00:11.510 --> 00:12.380
challenging.

00:12.380 --> 00:14.480
If you can't find the solution by yourself.

00:14.480 --> 00:19.460
Don't worry though, you can watch the solution and come back to it later to try again.

00:19.880 --> 00:24.440
So here is the result you should get for this challenge when you start the program.

00:24.440 --> 00:27.380
As you can see here, all the LEDs are powered off.

00:27.410 --> 00:34.190
Then when you press the button here, you can see the first LED is powered on and all the other ones

00:34.190 --> 00:35.570
are powered off.

00:35.600 --> 00:39.140
If I press again now, I switch to the second LED.

00:39.290 --> 00:44.000
So I turn off the first one and all the other ones and I turn on that one.

00:44.000 --> 00:47.780
I press again, we go to the third led and then I press again.

00:47.810 --> 00:50.240
We go back to the first, etc..

00:50.330 --> 00:50.990
Okay.

00:50.990 --> 00:57.080
And one important thing here is that this action is only when I press on the button, okay?

00:57.110 --> 00:59.480
When I release, it doesn't do anything.

00:59.660 --> 01:03.500
Uh, it doesn't matter how long I press on the button, okay?

01:03.530 --> 01:07.140
It's just when I press on the button that we switch the LED.

01:08.460 --> 01:10.650
So that is your challenge.

01:10.650 --> 01:15.030
Now I'm going to give you a few tips, a few help to get started.

01:15.030 --> 01:22.140
So first of all, if I go back to the previous activity, you see we have this button that when pressed

01:22.170 --> 01:28.830
okay, this is going to register a function to call when the button is actually pressed okay.

01:28.860 --> 01:31.170
So you will follow a similar structure here.

01:31.170 --> 01:33.510
You will need to use the pose to pause the program.

01:33.510 --> 01:38.250
And before that you will register a function to call when the button is pressed.

01:38.250 --> 01:39.900
So for example you will have.

01:39.900 --> 01:42.870
So you will have to create the button and everything.

01:42.870 --> 01:50.400
And then you will do button when pressed is equal to and you will put a function.

01:50.460 --> 01:56.220
So for example here I'm just going to create a function named switch LED.

01:56.850 --> 02:01.140
Just like that I will write code in this later.

02:01.140 --> 02:04.560
And then you can write switch LED okay.

02:04.590 --> 02:06.780
And you don't put any parentheses.

02:06.780 --> 02:11.930
Once again you don't put the parentheses because otherwise this would call the function.

02:11.960 --> 02:15.290
Here you just want to give the name of the function.

02:15.320 --> 02:15.800
All right.

02:15.800 --> 02:22.730
And one more thing is that to keep track of which LED we are actually turning on, and the other ones

02:22.730 --> 02:23.690
will be turned off.

02:23.690 --> 02:30.230
We will add here I'm going to create a variable named LED index okay.

02:30.260 --> 02:32.480
That starts at zero.

02:32.480 --> 02:37.640
So you can use this variable to keep track of which LED is turned on okay.

02:37.670 --> 02:40.190
So for now we start and we start at zero.

02:40.190 --> 02:44.360
Because in Python as you already know, we start to count from zero.

02:44.360 --> 02:48.950
And in this function in the switch LED that's called whenever you press the button.

02:48.950 --> 02:54.590
What you can do is you can turn on whichever LED corresponds to the current index.

02:54.590 --> 02:57.140
And then you can increment this index.

02:57.140 --> 03:02.390
So you can add plus one for example, so that the next time the function is called again you can decide

03:02.390 --> 03:04.220
to turn on the next LED.

03:04.250 --> 03:04.580
All right.

03:04.580 --> 03:08.720
So with this tip you can start thinking about designing a solution.

03:08.720 --> 03:12.110
And just to finish here there's one thing you will need to do.

03:12.110 --> 03:18.150
So previously I have told you that you can use a variable here, a global variable inside a function.

03:18.150 --> 03:24.000
So you can definitely read this variable, meaning that you can print it, you can read it, you can

03:24.000 --> 03:24.510
use it.

03:24.510 --> 03:33.750
But if you want to modify this variable, if I want to do LED index is equal one for example this is

03:33.750 --> 03:39.420
going to give you an error okay when it reaches that line because well when you use a global variable

03:39.420 --> 03:41.160
inside a function you can read it.

03:41.160 --> 03:50.280
But in order to write to it you first have to use this global keyword plus the name of the variable.

03:50.280 --> 03:55.950
So we will start our switch LED function with global LED index.

03:55.950 --> 04:03.000
Because we will not only read this variable, we will not only use it, but we will also modify it.

04:03.030 --> 04:03.390
All right.

04:03.390 --> 04:05.280
So I'm just going to remove that.

04:05.280 --> 04:09.360
And this is basically the code that we will start from.

04:09.360 --> 04:13.860
So of course you need to do all the imports and initialize the LEDs and all that stuff.

04:13.860 --> 04:15.300
And now it's your time to practice.

04:15.300 --> 04:19.740
So make sure you spend enough time on this activity before you go to the solution, and I will see you

04:19.740 --> 04:21.030
in the next lesson.
