Like in this example, I'm going to show you how to read the state of a button using a hardware timer.
Just remind you that in the previous two variations of the same exercise, you learned how to read the
state of a button using an infinite loop, like in this example right here.
And also how to use a hardware interact as in this example here.
The example in which I'm using the hardware timer is really a variation of the hardware in the example.
And you ought to know how to use the hardware timer from an earlier example with reality, which you
can see here in this script as we have defined the time down here and lines three, four and thirty
five.
So in this lecture, basically taking these scripts and putting them together and creating a hybrid
that looks like this.
So what's happening here is that I've got a timer which has a period of 50 milliseconds and this is
a periodic timer.
So it fires every 50 milliseconds and each time it fires, it will call the interruption of this routine
called button on this compressed and this call ISO, which is right here.
What it does is to check the state of the button.
And if it is pressed, then it will update these variables here.
At the same time, we've got the while loop infinite loop constantly checking for the value stored inside
the global pattern and this compressed variable.
And if it is true, then it will turn on the LCD and wait here for a second.
It's a very it's a very simple way of going about reading the state of a button, taking advantage.
Had we interrupt in terms of efficiency, I'm not quite sure which of these two is more efficient is
hard really to say.
I would say that the hardware interrupted is perhaps more efficient from the point of view that you
don't occupy a hardware timer to keep firing every 15 milliseconds and calling the interrupt service
routine.
In this example here with the hardware interrupt, the ISI is only called when the button is actually
pressed.
But in this example here, I thought that this our way of using the hardware timer is interesting and
worthwhile dedicating a lecture for it.
Let's have a quick look at the beginning.
There's nothing new really here.
We import the appropriate functions from the machine in time modules, define the ality, define the
button with its pull up resistor.
We've got our button pressed variable, the interrupt service routine with a global keyword so that
the button pressed and button event variables are global and then we can make changes to those variables
from inside the ISO.
Then when the ISI is called, we check for the value of the button and if it is down there, meaning
that button is pressed so the value returned by value is going to be false or zero.
Remember that of got this wire here which grounds the button value when it's pressed and therefore GPL
four will read a low value or false value in the python speak.
So Button is pressed.
In this case, you apply the variables.
We also store the event object in the event variable.
Otherwise button is false.
And notice that I have declared the button, pressed the function before I create the end, initialize
the hardware timer because I need to pass it to the timer via this callback parameter.
And if I call the ISO before it's actually declared, you're going to get an error message by the compiler
when you try to compile and run the script.
After that, we've got the while true, which constantly checks for the value stored in the button on
this compressed file and then it just turns on reality and keeps it on for one second.
Otherwise, it will turn it off.
All right.
So I'm going to make a copy of this script onto the device.
I will call it button for time.
I thought he y.
And let's run it.
OK.
And it stays on for one second and you can see the event with it, we're passing and printing out here,
starting the button and it's called event variable, which is created up here.
And it's really its origins are the event parameter that is passed by the time I had with him up to
the ISO.
And when you print out, this is what you get.
Okay, so that's about it with a button, you know, enough now to be able to use buttons in your sketches.
Now there's one more lecture in this section, which is the next one in which I'll show you how to use
a particular mirror and make use of the analog to digital converter.