WEBVTT

00:00.110 --> 00:06.740
This is the solution for the activity nine where we will start from this blink LED code.

00:06.740 --> 00:08.570
We're going to create a new Python file.

00:08.570 --> 00:10.310
From that we're going to execute it.

00:10.340 --> 00:13.520
Notice the difference between Tony and the terminal.

00:13.520 --> 00:16.820
And then improve the code with a try except block.

00:16.820 --> 00:19.640
So let's start with the first step.

00:19.640 --> 00:22.040
I already have the code here to make the LED blink.

00:22.040 --> 00:29.570
What I will do is I will use the command CP to copy the LED dot pi into activity.

00:29.570 --> 00:32.570
So for pi I could use the Autocompletion here.

00:32.570 --> 00:35.000
I can't because it's a new file name.

00:35.000 --> 00:38.660
So activity let's call it nine dot pi.

00:39.380 --> 00:47.420
Okay so I have my activity nine dot pi that contains activity nine that contains the same contents.

00:47.450 --> 00:48.350
No surprise.

00:48.350 --> 01:01.880
And I'm going to now go to step two okay I'm going to open this from Sony Open Activity nine okay.

01:02.960 --> 01:09.170
And just to make sure that we have enough time to stop the program, I'm just going to put a two seconds

01:09.170 --> 01:09.740
here.

01:10.940 --> 01:11.090
Okay.

01:11.090 --> 01:15.500
So the LED will be on for two seconds and then off for one second.

01:15.530 --> 01:18.680
So we have more time to stop the program and see what happens.

01:18.680 --> 01:22.400
This is specifically in this part that I want to make experiments here.

01:22.400 --> 01:24.800
So let's run the program.

01:26.150 --> 01:30.590
And as expected it's on for two seconds off for one second.

01:30.650 --> 01:38.690
Now if I stop it, you see, if I stop it here when the LED here is still on, then the LED stays on,

01:38.690 --> 01:41.990
which means that the Gpio was not correctly cleaned up.

01:41.990 --> 01:50.780
Now let's go to the terminal and let's run Python three activity nine okay.

01:51.080 --> 01:51.860
So what.

01:51.860 --> 01:54.860
It's going to do the same thing on and then off.

01:56.150 --> 01:58.520
And I'm going to do control C here.

01:58.640 --> 02:00.290
Actually I think it was off.

02:00.290 --> 02:04.490
So I'm going to try again on off and then on.

02:04.490 --> 02:05.450
And I press Ctrl C.

02:05.480 --> 02:10.220
And you can see right now here that the LED was turned off okay.

02:10.250 --> 02:12.320
So this is the same case as before.

02:12.320 --> 02:18.530
We were in the part of the code where the LED was turned on, and then we were waiting for two seconds.

02:18.530 --> 02:22.190
But when I stopped it from this button here, it stayed on.

02:22.190 --> 02:27.230
Which means that as I told you, the Gpio was not correctly cleaned up here from the terminal, and

02:27.230 --> 02:33.500
probably if you also run it from the shell here when I stop the program with control C, even if the

02:33.500 --> 02:39.920
LED is on, you can see that, then the LED is going to be turned off, which means that the Gpio was

02:39.920 --> 02:40.790
cleaned up.

02:40.820 --> 02:41.090
Okay.

02:41.120 --> 02:43.610
So as you can see we will try.

02:43.640 --> 02:47.930
Not later on we will try not to run the code.

02:47.960 --> 02:55.820
So any code that uses Gpio using display and the stop button here we will try to run it either maybe

02:55.850 --> 02:59.720
in a shell or in the terminal which might work better.

02:59.720 --> 03:02.660
And now I'm going to go to the last part of this activity.

03:02.660 --> 03:07.880
So you can see when we do control C we have an exception here and a keyboard interrupt.

03:07.880 --> 03:11.270
So let's try to exit in a smooth way.

03:11.270 --> 03:13.910
So I'm going to write the code here directly from Thonny.

03:13.940 --> 03:16.220
Since the program is open here.

03:16.340 --> 03:18.080
And I'm going to do a try.

03:18.110 --> 03:21.600
And I'm going to put all that inside the to try.

03:22.890 --> 03:23.160
Okay.

03:23.190 --> 03:26.730
And after this, I go back to the same indentation as the try.

03:26.730 --> 03:33.420
And I do accept keyboard interrupt with a colon.

03:33.420 --> 03:36.090
I go back to a new line and do I want to do something?

03:36.120 --> 03:38.700
Well, if you want to do something, you can do it.

03:38.700 --> 03:40.650
You can write something with a print.

03:40.680 --> 03:42.060
Run some other comments here.

03:42.060 --> 03:43.500
I'm just going to do pass.

03:43.500 --> 03:46.080
So I'm going to save that.

03:46.140 --> 03:49.530
You can note it's important if you're going to run the script from the terminal.

03:49.530 --> 03:55.800
It's important that first you save from Tony here, because in Tony, when you run the script, anytime

03:55.800 --> 03:57.870
you run the script, it's going to save it anyway.

03:57.870 --> 03:59.010
So save and run.

03:59.010 --> 04:02.250
But from the terminal, it's not going to save the file for you.

04:02.250 --> 04:08.610
So you need to make sure that you save with Ctrl S or also file and save here.

04:08.610 --> 04:12.510
So now that this file is saved, let's run it again.

04:12.510 --> 04:15.360
And then I'm just going to press Ctrl C whenever.

04:15.360 --> 04:18.630
And you can see now we don't have this message here.

04:18.660 --> 04:21.000
This exception message.

04:21.270 --> 04:21.720
Great.

04:21.720 --> 04:23.490
And that's the end of this activity.

04:23.490 --> 04:28.470
And as you can see now you can do quite a lot of things with Python and the terminal.
