Welcome back.
In the previous lecture, you learned how to read the state of the button in the simplest possible way.
As you can see here, we just use a while, true infinite loop.
And in it we just take a reading of the button, fairly painful, and then act accordingly.
The problem with this method, of course, is that you've got an infinite loop here that looks at the
execution of the program in it.
It's not a very efficient way to use your hardware.
In this lecture, I want to show you an alternative which is more efficient and allows us to read the
state of the button using a hardware interrupt.
So in this line here, line 58, I have to find an IQ and interrupt request on button painful and have
configured it in a way that is very efficient.
So what I'm saying here to the trigger is the falling edge of the signal that is produced by the button.
So when you press the button, the voltage on your full force from high to low, and that is the IQ
falling edge which is detected in triggers this interrupt.
And when this edge, the falling edge is detected, then they interrupt.
Is calling this routine here a function, the button and this compressed underscore ESR interrupted
service request or retain his ISO, which branches the program inside this function and again act accordingly.
Will get to this in a minute in order to display the ality lighting up.
When I press the button, I'm still using a while loop here.
It's just the easiest way just to demonstrate the interaction between an eyesore and then a different
part of your program.
And I'm also demonstrating the ability of Intisar and other parts of your program to communicate using
global variables in this case.
Now let's have a look at some of the details.
You can see up here in line 36, I am importing the PIN function, disable IQ and enable IQ functions
from the machine module.
And again, the slip in there from the time module.
I'm starting by creating the object as usual, and then the pattern object, which is exactly what we
did in the previous example.
Same thing here.
Nothing has changed.
Then I'm declaring a couple of variables.
This is the button pressed portable.
It allows the IQ routine of the ISIS recorded here, retained to communicate with other parts of my
program.
So when a button is pressed, then I update this variable here.
You can see it is updated here and then used here to determine whether they should be turned on or off.
They also have integer like a numerical variable here that keeps track of how many times have pressed
the button before it's reset.
So you can play around with those variables as well.
Now, inside the definition of the routine that we will be called by the IQ, you can see that it requires
one parameter.
And this is the object that has caused the IQ and this is passed by the IQ routine here as well.
And I'm using this object here to print out some information about what is it that has caused the IQ
to see I'm taking PIN and exactly what it is and passing it into the button pin global Viterbo, which
is then printed out down here so I can get some information about the object that caused the IQ.
A couple of other interesting things that are happening in here is, first, that I am calling the disabled
IQ routine, which, as you can probably guess, will disable further excuse.
So when I press the button, the first thing that happens with disability IQ, while the Isar function
is busy, any further button presses will just be ignored until I re-enable the IQ right at the bottom
of the isobutane.
So when I'm done doing whatever needs to be done to deal with the existing IQ, then I will re-enable
it so that the 32 can detect the next button.
Press a couple of other interesting things here that are perhaps a bit unusual for you is that I'm using
the global keyword here so you can see what's happening, of course, available, such as Button Pressed,
which I have already declared at the header of my program.
So you would expect that this variable would be global already just by the fact that it's been declared
the header of the program, but in fact, it isn't.
I won't be able to make any changes to this variable from inside this context before I use the global
keyword to convert it into a variable that I can make changes to.
I've got a link for more information about this here.
If you're curious about how this works, if you don't use this keyword, then you are going to get a
syntax error or you're going to get an interim message on line 53 when you try to make a change to the
value stored in this variable.
So do the same thing with the other two variables that I'm using across different sections of the program.
So button spin and press count, you can see that all of those have been declared up there.
And I still need to use a group of able to be able to make changes to them.
All right, so then I just store to the button, pressed as to the object that is causing the IKEA into
button pin and then I increment the press counter to what?
And then I close the cube in here.
You've got the infinite loop, which is similar to the loop function in the Adreno is just constantly
going around executing whatever codes you have in it.
And in this case, it's constantly checking for the value stored inside the button pressed variable.
And when it's true, you will go inside, change it into force, turn on the ality, print out the two
messages zero.
The counter will reset the counter and keep the lady on for half a second.
If it's not true, then it will turn off the reality and that's about it.
So I am going to save a copy of this program to make replacing it with PCs and it's going to hit control.
See you stop the execution.
All right.
And then try again, save a copy of Python and you can call this.
I tend food interrupt why?
All right, how close these two and not that one, I need to open up the interruptive position, but
program this one right here.
And now that I've got the program opened on the target market Python device, I will play it.
And.
Press the button and works.
Stays on for half a second as well.
Right.
And one thing to notice here is that in the message that starts with Button pressed at it right here,
you can see that the output of button pin, which is this variable here, which contains the object
that has caused the interrupt, it says painful that way.
If you have multiple interrupts from different areas, then you can always differentiate as to which
button or which interrupt is the one that has triggered your interrupt service routine.
OK, so that's about it with the bat, an example using the hardware interrupter.
I want to show you one more variation of the same in the next lecture, which involves this time using
a timer interrupt this ticket out.