This is the solution for the activity nine where we will start from this blink LED code.
We're going to create a new Python file.
From that we're going to execute it.
Notice the difference between Tony and the terminal.
And then improve the code with a try except block.
So let's start with the first step.
I already have the code here to make the LED blink.
What I will do is I will use the command CP to copy the LED dot pi into activity.
So for pi I could use the Autocompletion here.
I can't because it's a new file name.
So activity let's call it nine dot pi.
Okay so I have my activity nine dot pi that contains activity nine that contains the same contents.
No surprise.
And I'm going to now go to step two okay I'm going to open this from Sony Open Activity nine okay.
And just to make sure that we have enough time to stop the program, I'm just going to put a two seconds
here.
Okay.
So the LED will be on for two seconds and then off for one second.
So we have more time to stop the program and see what happens.
This is specifically in this part that I want to make experiments here.
So let's run the program.
And as expected it's on for two seconds off for one second.
Now if I stop it, you see, if I stop it here when the LED here is still on, then the LED stays on,
which means that the Gpio was not correctly cleaned up.
Now let's go to the terminal and let's run Python three activity nine okay.
So what.
It's going to do the same thing on and then off.
And I'm going to do control C here.
Actually I think it was off.
So I'm going to try again on off and then on.
And I press Ctrl C.
And you can see right now here that the LED was turned off okay.
So this is the same case as before.
We were in the part of the code where the LED was turned on, and then we were waiting for two seconds.
But when I stopped it from this button here, it stayed on.
Which means that as I told you, the Gpio was not correctly cleaned up here from the terminal, and
probably if you also run it from the shell here when I stop the program with control C, even if the
LED is on, you can see that, then the LED is going to be turned off, which means that the Gpio was
cleaned up.
Okay.
So as you can see we will try.
Not later on we will try not to run the code.
So any code that uses Gpio using display and the stop button here we will try to run it either maybe
in a shell or in the terminal which might work better.
And now I'm going to go to the last part of this activity.
So you can see when we do control C we have an exception here and a keyboard interrupt.
So let's try to exit in a smooth way.
So I'm going to write the code here directly from Thonny.
Since the program is open here.
And I'm going to do a try.
And I'm going to put all that inside the to try.
Okay.
And after this, I go back to the same indentation as the try.
And I do accept keyboard interrupt with a colon.
I go back to a new line and do I want to do something?
Well, if you want to do something, you can do it.
You can write something with a print.
Run some other comments here.
I'm just going to do pass.
So I'm going to save that.
You can note it's important if you're going to run the script from the terminal.
It's important that first you save from Tony here, because in Tony, when you run the script, anytime
you run the script, it's going to save it anyway.
So save and run.
But from the terminal, it's not going to save the file for you.
So you need to make sure that you save with Ctrl S or also file and save here.
So now that this file is saved, let's run it again.
And then I'm just going to press Ctrl C whenever.
And you can see now we don't have this message here.
This exception message.
Great.
And that's the end of this activity.
And as you can see now you can do quite a lot of things with Python and the terminal.