Hi and welcome to a new look changes section.
In the previous lecture, you learned how to make this link so disconnected to your 21 and you make
that work by using the while loop.
You've got a while true loop here that never ends and it will just turn on the on and off and each time
it'll keep its state for half a second.
In this lecture, I want to show you an alternative way of making this ability to blink, and that is
by using a hardware timer instead of a loop.
So I've got this script right here.
It's five twenty and you can see it's about the same size as the blink with the wild true loop.
But now we are using a timer.
You can see that I'm setting the timer down here.
This timer has an I as are assigned.
This is an interrupt service routine assigned, which is a simple function that inside that function,
all it does is that they will check the current value of the ability by using the value function.
And if it is false, therefore the elite is turned off and it will turn the lights on and if it's not,
it will turn it off.
So the logic here is that we don't have an infinite loop.
We just have a hardware timer which is connected to a routine that contains the functionality that we
want, whether in this case it is to blink an on and off or to do something else periodically.
The new thing to learn here is the use and the configuration of the hardware timer.
And you can see that here I've got a Eurail, a link to the documentation where you can look it up and
learn more about it.
But as you can see, it's quite simple.
I'm looking inside the quick reference for E.S.P 32 times instead of this paragraph here, which contains
the way to use a hardware timer.
So first, we need to start by importing the time a module from the machine module here, we create
the timer object to give it a day.
It doesn't need to be a negative number.
You can see here that I just said timer parentheses one for the ID and then you can set that time up
to whichever interval you want.
There's the init or initialization function and it takes these parameters.
First you've got the period.
So how often which it like this time to call your function then the mode it's going to be a one shot,
which means the time it's going to trigger once and then it will stop or periodic, which is what we
are doing in this example, which means that the time I was going to call your you will retain your
function every time that the period elapses.
And then we've got a call back here.
The callback is a Lambda Lambda is a python feature that allows you to define a very small function
in line.
So instead of defining the function as we are doing here, right here, somewhere else in your program
with the lambda, you define your function in line with the callback declaration.
And this case is just going to print out one which in this case, the lung function printshop out to
also want to show you the full class time on module that comes with the macro python firmware.
And you can see how it works here.
A little bit more information about it.
You can see that the mode can be that this one short or periodic, and you can also use the Internet
to initialize it time and basically stop it.
Disable it if needed properly, can enable it by using in it and then you can disable it by using the
Internet.
We are just enabling it by using init in this example.
There's no reason to initialize it.
And there's the constants.
And it's a very simple way to take advantage of the hardware time in the E.S.P three to.
So the next thing to do here is to upload this script to the ISP 3-2, so I'm going to use the save
a copy method so they will go to the market python device.
OK, so I'll do this again to see what happens.
So I tried to make a copy to make a Python device, but as you can see, the device is busy right now
because it still running the sketch from the previous lecture.
So I first have to stop the execution of the script to interrupt it and then copy the new script along.
You can do that a couple of ways.
The first one is to hit control, see, and that will stop the execution of the script.
Or you can go to run and choose to interrupt execution option, which is, as you can see, control
see from the menu.
So I'll do that via the menu this time and from now on, which I'll be using my keyboard to do a control
see and interrupt execution.
All right.
So let's try again our fail safe copy Micro Python, and I'm going to give you the same name ality link
timer here.
Why I got the twenty one for the GPO.
Right, so here it is.
Double click to bring it up.
All right, so now I'm going to make sure that I've selected the blink time.
I've got to wonder why a script with the square brackets that is the one that is stored on the 32 and
with that selected or pressed the play button, the green play button, or go to run and click on run
current script.
And that will have the same effect.
So click on that and.
You can see that the effect is exactly the same as before.
So we've got the deep thinking I can change the hardware time to make it blink a little faster.
So let's make this two hundred and fifty milliseconds just to see the difference between description
and ensure that this is the one that is really being executed.
So I made a small change here to the period and then click on Save to save it to the device and then
click a. on the green play button.
And you can see that the elite is now blinking faster.
The other thing that is interesting is that I can make changes and save them or make them say two hundred
milliseconds and I'm going to save it.
And you can see that there was no complaint from Sony to complain that the device is busy, like there
was a complaint earlier when we were running the ality blink with the while loop here.
And that is because when we were using the wire loop, the device was really engaged, either making
the little blink or sleeping.
So the device was engaged.
We couldn't save on.
I can make any changes, but when we are running the script with the interrupt, the device is only
busy when the timer is actually calling the isobutane and only when the security is executing these
highlighted lines and all other times it's not busy.
So it can be ready to receive, for example, a new file update from Sony without complaining about
it being busy.
So that's another positive effect from this.
You can see that the use of the U.S. military to hardware is more efficient when we use the hardware
time versus the while troop.
All right, so let's move on and do one more experiment that involves the energy on Tiberio 21, which
is learning how to make it fate using copes with modulation.