Let's not see how to use interrupts in your original program, so what I'm going to do here, I'm going
to make a very simple application where when we press the button, we are going to toggle an entity
that might sound familiar to you already.
So what?
I'm going to initialize the hardware stuff.
OK, so that is done.
So define thing.
I'm going to use the 12 define.
Button in which is being number two, and you can see that I have put the button on the pin number two
and purple.
OK, at the beginning of this course, because if you remember here, I'm using an outer window and
only digital pins you can use for interrupts our pin number two and pin number three.
So if you have put your pushbutton in a different pin, well, make sure that you look at the table
I have provided in the previous lesson.
And that corresponding to your order, would you connect the push-Button to a pin that can support interrupts?
OK, so in the setup.
In mode and the.
We upwards and then being.
But being weak.
So if you're going to use an interrupt on a digital Benwell, nothing changes for the initialization,
OK?
You just set the scene as inputs.
OK, that is the same.
And now let's create an interactive.
So to create an interrupt in your Uninor program, you have to do two things.
First, create a function.
I'm going to call it.
Button released direct.
And then use the attach.
Interrupts.
Function, OK?
You can use it in your setup, for example, or anywhere in your code, that usually that's what we're
going to do in setup after we initialize the pin.
And here we are going to choose the mode fully.
OK, so following if your Roomba is when the signal goes from high to low, in other words, without
secret falling is when we release the button.
OK, because when we press the button, the signal will go to high and when we release the button,
the signal will go to low.
So we can use fulling and because we use fulling.
I have named the function here button released into it.
OK, if you were using, for example, rising, you could name the function button pressed into it.
OK, make sure you give meaningful names.
So in this attachment or a function, I need to provide three argument.
First the thing.
So the button pin, then what I'm getting here, the callback function.
So the function to call when to interrupt is triggered and there is the mode.
So let's start to set up the PIN number.
We are not just going to button up like this.
We're going to need to use another function which is called digital pin to.
Interact.
OK, you can see it turn.
OK.
OK, button that we use digital ping to interact with button ping that we put as the first argument
in the attach interact.
OK.
And then the second argument will be.
Dysfunction, as you can see, the line is quite long, so what I'm going to do here and I encourage
you to do when you have a function with many parameters, is go back to a new line supercenter and then
I'm going to align in the beginning of the disease.
So you press enter after the coma, OK?
OK, I have a new argument, so I'm going to press enter here after the command, not everything is
aligned, so that's good.
So I have digital ping to interact with the button ping.
The name of the function without parents is without anything, just the name of the function.
OK, but don't really interrupt.
And then the mode.
So the mode is.
Falling and as you can see, falling turns blue.
OK, this is a key word, if you were using rising, for example, would turn blue also and change also.
OK.
Fully, and then I close the parentheses and semicolon.
OK, so what this will do is to attach and interact on this pin, OK, for the following mode and calling
the button interrupts.
So now from this point after the line 12, whenever the signal on the PIN number two goes from high
to low, this function will be called.
So wherever we are in the program, the program will stop, the execution will stop, and this function
will be called.
After this function has finished, the program will resume wherever it is.
OK, and one important thing here for the interim function, you should have avoid that type, OK?
You should not return anything and you should not have any parameter.
OK, that's the two conditions or interim function void and no parameter.
What we want to do.
We want to toggle the entity when we release the button.
What I'm going to do, I'm going to create another function.
Toggle edit OK.
And in this function, so I'm going to create vitality, the rate is low, initialized to low.
So this we already have done it a few times before.
OK, I'm going to go quite fast if you.
They is low.
I would just invest.
Every state becomes I, it ends.
State becomes law, and after we have changed the state, we do digital.
And the thing with the.
OK, so we keep the state of the 80s, so whenever we enter the target area, we check what was the
current state would change it to the other one because the only two states and then we use digital rights
to apply the new state to the atypia.
So then by using this, it's nice and clean.
We have a function that does just what we wanted to do so we can just go ahead.
We don't need to repeat that code anywhere else in the program and know what we could do is to call
the toggle and inside the button, really interrupt, OK, because that's what we want to do.
But that's not what we are going to do, actually.
Why is that?
Because the interim function will block your program, which stopped your program, and it's also going
to disable every other interrupt.
OK, so you have to know that in order, you know, you can create interrupts like this.
But there are also other interrupts that run in the background that you may not be aware of.
But the thing is that when you create and when you are in an interim function, every other interrupt
is disabled and your main program is completely stopped looking.
Hear what's wrong with that is that we use digital right to write is a function to do an action and
this will take a few microseconds.
OK, and this is more than just changing the state.
OK, changing variable.
So a best practice for an interim function is just to change some variables.
And then after you have changed some buyable, you will handle the action in the loop.
OK, let's see how to do that.
I'm not going to do add in the interrupt that would work for this example, but this is not a good practice.
Instead, what I'm going to do is create a boolean.
So when I create a boolean variable, I can also call it a flag.
OK, like this button release.
Which is false.
And when we are in the interim and is going to do but then released.
Is true, though, I said the fly, which means that I put the value to true inside this boolean and
note that this is important when you use a valuable.
So when you use a global valuable insight, an interim function, what you have to do when you're declared
valuable, you have to add another keyword in front of the data type.
And this keyword is.
OK, so whenever you use a valuable insight, an interrupts, make the viable, volatile and by using
volatile, you are just telling the world, you know, that you are going to use this valuable also
interrupt so you may handle it better.
OK, and so now after we have set this flag so the button rose to true do what we can do is in the look
function.
We are just going to check if.
But then released.
So when you take for a billion, you don't need to do this, OK?
You don't need to check if the blame is true because the billion is already a bullion, so it's already
bullion value.
So this is just true.
All this is false.
OK, so if the button is released, then what we are going to do is we are going to do button release.
Is false and Eric.
So in the look on the on the action we do is to check for this flag it for this billion, if this balloon
is true, we enter the if we unset the flag, so we set the boolean value to false or we don't enter
it the next time.
All right.
And then we took the editing.
So as you can see, your problem in the loop is very simple.
Can you just check the flag?
And then because you have attached and interact with the button and with this function whenever you
release the button, OK, this will trigger the following mode, which will call this function, which
will set this boolean to true OK, which means that the next time you read it in the loop, it is true
and you can execute the action.
And by doing this, you are sure that you don't miss the release of the button.
All right.
Now let's upload the program.
So let's name it.
Interrupts.
OK, so you can see here the read, any on 12 is pulled off, I'm going to press on the button.
You can see nothing happens and I Rollie's the button, the energy is put on, OK, when they release
the button and then I press a release and then is put off.
So the energy was just told, I'm going to go a little bit faster, OK.
And as you can see, I just press another time and the energy is still on.
Why is that?
Well, we are coming back to the bones problem once again.
So using interact with a push button in that specific situation means that you will know exactly when
the state of the signal changes, but you still have the balance problem and the problem is simply a
physical problem.
So if we had the problem before with interrupts, we don't solve this problem.
So in the next listen, we are going to see how to demand the button in the interrupt.