Here is the solution for the activity number five.
So I'm first going to do the solution with the code that we have seen previously.
And then I'm going to give you two additional things that are very important.
First is a way to improve the program and make it more efficient.
And the second is well we're going to use a different way to write the program and that we will use
directly for the next activity of this course.
So let's get started.
The first thing we need to do is to import stuff.
So from Gpio zero import.
So we need to import the LED and the button because we're going to use both so LED maybe you have written
this from Gpio zero import like this import button.
And that is correct okay.
It's going to import the LED and the button.
But what we can also do is to add a comma.
So I showed you that before we can add a comma and just do button.
Okay.
So from library import functionality, one comma functionality two comma etc..
Great.
So now I will initialize my LED just like that.
So LED I put the Gpio number 17 and then button I put button and the Gpio number 26.
So first things first we initialize the Gpio 17 and we initialize the Gpio 26.
And then we want to detect.
So when the button is pressed we power on the LED.
If it's not pressed while the LED is going to be turned off.
And I don't want to just do that one time here when we run the program, I want to do it continuously.
So I'm also going to use this while true loop.
So an infinite loop.
And I'm going to say simply if button dot is pressed I'm going to put a colon.
And if it's true.
Well, we do led dot on.
If it is not true then I'm going to go back here.
So if it's not true it can only be false.
So I'm going to put else and then led dot of okay.
And pay attention to the indentation here.
The whole if is with one indentation because it's inside the while.
But then for example that instruction has one indentation of four spaces plus an extra one.
And now I can test my program.
So let's just run it okay.
It's running.
So there's nothing printed here but it's running.
And I can just press on the push button.
You see the LED is on now I release okay.
And you can see when it pressed it's on.
When it's released it's off on off on off etc..
So that's working.
And if you have written this code, well you have solved the activity, but now I'm going to show you
something additional.
So actually two things.
Let's start with the first one.
And the first one is about optimizing the program a bit.
And why do I care about optimizing here.
Because I'm going to show you something.
So while the program is running you can go here and search for it's going to be the task manager here.
Okay.
And let's wait that it initializes okay.
And you can see we have here a command Python three.
So that's basically the program that's running.
You can ignore the rest.
And you can see that this Python three command is taking 25% of the CPU.
And what does that mean.
So actually on the Raspberry Pi here on the Raspberry Pi five I have four CPUs.
And when you run a Python script it's going to run using one CPU unless you have a multi-threaded application.
But that's way more complex.
And for now it's just using one CPU, so when you see 25%, it means that the program is actually using
the whole CPU.
So a program as simple as this is using the whole CPU.
Why is that?
Because well, we have an infinite loop that just runs at full speed.
And if it runs at full speed, it's basically going to run as fast as it can and use 100% of the resources
of one CPU.
So we want to avoid that.
For what?
For a few reasons.
First, it's going to make your Raspberry Pi heat more.
So if you don't have any fan or anything to protect from the heat, well, it's going to heat quite
a lot.
Not necessarily a problem, but well, you don't want to have your Raspberry Pi too hot and then while
you are using one full CPU.
So anything else that you run is going to be slower.
And if there is an easy way to solve that, then we're going to do it.
So instead of running this full speed, we don't need to run this full speed.
We could add a small delay actually between each iteration of the while loop.
I'm going to add here I'm going to import time.
And what I can do is after I read the button, I'm going to add.
So I go back to this indentation.
Okay.
After the if time dot sleep.
And let's actually pause the program.
But for a very short time for example 0.01 second.
So that's very very tiny.
That's basically ten milliseconds.
So you will not see that okay.
When you run the program you will not see any difference because we as human, we well, we can't see
a ten millisecond difference in the program.
So it's going to do the exact same thing.
But this loop is going to be running at a much, much slower speed, which is still high enough for
this application.
So now let's stop and let's run this again.
Now you can well, you can press the push button again.
This is going to be exactly the same thing.
But if we have a look at our task manager again.
So let's wait a few seconds for that initialization.
And Python three is here.
And now it's using zero.
So not 0% but zero point something percent.
So you see we went from 100% of one CPU to something that's very very very tiny okay.
So this improvement is actually quite important.
If you run an infinite loop like this for a program that you run on Raspberry Pi, it's better if this
loop is going to run at full speed.
So the full CPU speed is better to slow it down a bit by adding here a time.sleep with a small amount.
And basically this is ten milliseconds, so it's 0.01 second.
That means that you are going to run this at about 100 times per second.
Okay.
So that's still a lot.
And we are talking here about.
So if it's 100 times per second.
We also talk about a loop that runs at 100Hz.
So that's for the technical terms.
And one thing is maybe I'm gonna just put that at the beginning of the loop.
It doesn't really change anything okay.
I just put it at the beginning so that I don't forget it later if I have more code.
And also well, as I add code, maybe I'm going to mess up the indentation for that.
So here I make sure that it's at the beginning of the loop and it's going to slow the loop.
So we run it at 100 times per second.
Great.
Let me stop that.
And now I want to show you something else.
So that's not related to optimization or anything.
It's just that we are going to use another functionality from the Gpio library to do actually the exact
same thing, but the code is going to be much smaller.
And that's the code we are going to use also for the next activity.
So first, that was actually a very good practice for you to work on loops and conditions.
Okay, so that's why I kept it like this.
And now let's see how we can write this code differently.
So what I'm going to do is I'm going to comment that like this comment.
So we don't execute it.
And there is in fact inside this button functionality.
What we can do is just write button.
So using this button here dot when pressed is equal to.
And then just give a function here.
So for example led dot on.
And note that we don't provide any parentheses.
Okay I'm gonna explain that just in one minute.
So we can say that when the button is pressed when pressed we're going to turn the LED on.
And then we can also do button dot when released LED dot off.
And so this line is actually not going to stop.
It's just going to register here this this function and this one as well.
So if we just run the program like this, well it's going to run and then it's going to exit.
So it's not going to do anything.
So we need to somehow pause the program.
We need to find a way to pause the execution of the program.
And for this we can do from signal.
So that's another library you can use import pause.
You have the pause function from the signal library that we can just use at the end of our program here.
Pause like that with parenthesis.
All right.
So what do we do here is we initialize the LED the button.
And then we say that when the button is pressed we're going to turn on the LED.
When the button is released we turn off the LED.
And then we pause the program.
And just to give you more details about this, well, this is what we can call a callback function.
So to use a real life analogy, let's say that you need some information from somebody and they will
tell you, okay, I will call you back when I have this information.
So you give them your, for example, your phone number or your email.
All right.
So you kind of we're going to talk about registering your phone number.
So you register your phone number so that when they have this information they can call you back using
this phone number.
Okay.
So here there are two things.
One is that there is an event that must happen and that is external.
You don't really know when it's going to happen.
It might happen at some point.
And when this event is happening you want to be called back.
So you want to be notified of this.
And that's exactly what's happening here.
You see we have this dot when pressed.
So this is the external event when the button is pressed.
But you don't know when the button is going to be pressed from the program.
Okay.
That's an external event.
And so you register a callback and the callback is led1.
So when the button is pressed here is what to do, what to call.
And we're going to call this function led1.
And just note it's one particular thing about callbacks is that you don't provide the parentheses,
because if you provide a parentheses, you are calling the function right.
Now here you are registering the function so it can be called later.
So basically with this you just register those two functions that they can be called when this event
or that event is happening.
And then you make sure you pause the program.
Because if you don't pause the program well it's just going to exit and nothing is going to happen.
And now you can try to run this and it's going to be the exact same behavior as what we had before.
All right.
And you can see the code is much smaller actually.
We don't even need the time library anymore.
And if you look at if we go back to the task manager here, you will see that.
So this is running and uh, well this is not using much CPU as well.
Okay.
So this code is already pretty much optimized.
We don't need to add any time.sleep here.
All right.
And I will come back to this for the next activity of this course.