WEBVTT

00:00.210 --> 00:06.240
Let's not see how to use interrupts in your original program, so what I'm going to do here, I'm going

00:06.240 --> 00:13.080
to make a very simple application where when we press the button, we are going to toggle an entity

00:13.410 --> 00:15.790
that might sound familiar to you already.

00:16.050 --> 00:16.420
So what?

00:16.440 --> 00:20.250
I'm going to initialize the hardware stuff.

00:20.260 --> 00:21.380
OK, so that is done.

00:21.390 --> 00:22.880
So define thing.

00:23.160 --> 00:26.160
I'm going to use the 12 define.

00:27.290 --> 00:34.760
Button in which is being number two, and you can see that I have put the button on the pin number two

00:34.760 --> 00:35.340
and purple.

00:35.390 --> 00:41.300
OK, at the beginning of this course, because if you remember here, I'm using an outer window and

00:41.300 --> 00:46.350
only digital pins you can use for interrupts our pin number two and pin number three.

00:46.520 --> 00:52.790
So if you have put your pushbutton in a different pin, well, make sure that you look at the table

00:52.820 --> 00:54.740
I have provided in the previous lesson.

00:54.740 --> 01:01.760
And that corresponding to your order, would you connect the push-Button to a pin that can support interrupts?

01:03.100 --> 01:04.460
OK, so in the setup.

01:05.590 --> 01:07.570
In mode and the.

01:09.530 --> 01:12.410
We upwards and then being.

01:15.030 --> 01:16.950
But being weak.

01:17.940 --> 01:24.140
So if you're going to use an interrupt on a digital Benwell, nothing changes for the initialization,

01:24.150 --> 01:24.370
OK?

01:24.390 --> 01:27.130
You just set the scene as inputs.

01:27.180 --> 01:28.720
OK, that is the same.

01:29.400 --> 01:31.540
And now let's create an interactive.

01:31.920 --> 01:37.110
So to create an interrupt in your Uninor program, you have to do two things.

01:37.470 --> 01:39.430
First, create a function.

01:39.840 --> 01:40.950
I'm going to call it.

01:42.430 --> 01:46.060
Button released direct.

01:48.630 --> 01:51.450
And then use the attach.

01:52.900 --> 01:53.560
Interrupts.

01:54.910 --> 01:55.700
Function, OK?

01:55.720 --> 02:01.990
You can use it in your setup, for example, or anywhere in your code, that usually that's what we're

02:01.990 --> 02:04.940
going to do in setup after we initialize the pin.

02:05.590 --> 02:07.850
And here we are going to choose the mode fully.

02:07.910 --> 02:14.590
OK, so following if your Roomba is when the signal goes from high to low, in other words, without

02:14.590 --> 02:17.780
secret falling is when we release the button.

02:17.800 --> 02:23.260
OK, because when we press the button, the signal will go to high and when we release the button,

02:23.260 --> 02:24.790
the signal will go to low.

02:25.000 --> 02:28.360
So we can use fulling and because we use fulling.

02:28.360 --> 02:33.110
I have named the function here button released into it.

02:33.280 --> 02:40.780
OK, if you were using, for example, rising, you could name the function button pressed into it.

02:41.020 --> 02:43.420
OK, make sure you give meaningful names.

02:43.450 --> 02:49.240
So in this attachment or a function, I need to provide three argument.

02:49.540 --> 02:50.620
First the thing.

02:50.650 --> 02:55.010
So the button pin, then what I'm getting here, the callback function.

02:55.020 --> 03:00.180
So the function to call when to interrupt is triggered and there is the mode.

03:01.030 --> 03:03.830
So let's start to set up the PIN number.

03:03.850 --> 03:06.400
We are not just going to button up like this.

03:06.400 --> 03:11.650
We're going to need to use another function which is called digital pin to.

03:12.680 --> 03:13.400
Interact.

03:13.860 --> 03:15.530
OK, you can see it turn.

03:17.070 --> 03:17.390
OK.

03:18.090 --> 03:25.970
OK, button that we use digital ping to interact with button ping that we put as the first argument

03:25.970 --> 03:27.970
in the attach interact.

03:29.220 --> 03:29.630
OK.

03:29.690 --> 03:32.000
And then the second argument will be.

03:33.980 --> 03:41.360
Dysfunction, as you can see, the line is quite long, so what I'm going to do here and I encourage

03:41.360 --> 03:47.510
you to do when you have a function with many parameters, is go back to a new line supercenter and then

03:47.510 --> 03:51.980
I'm going to align in the beginning of the disease.

03:52.370 --> 03:56.420
So you press enter after the coma, OK?

03:57.610 --> 04:02.650
OK, I have a new argument, so I'm going to press enter here after the command, not everything is

04:02.650 --> 04:03.590
aligned, so that's good.

04:04.030 --> 04:06.940
So I have digital ping to interact with the button ping.

04:08.180 --> 04:13.100
The name of the function without parents is without anything, just the name of the function.

04:13.130 --> 04:14.930
OK, but don't really interrupt.

04:15.140 --> 04:16.040
And then the mode.

04:16.250 --> 04:17.300
So the mode is.

04:18.600 --> 04:22.140
Falling and as you can see, falling turns blue.

04:22.440 --> 04:29.730
OK, this is a key word, if you were using rising, for example, would turn blue also and change also.

04:29.940 --> 04:30.310
OK.

04:32.170 --> 04:35.970
Fully, and then I close the parentheses and semicolon.

04:36.880 --> 04:45.220
OK, so what this will do is to attach and interact on this pin, OK, for the following mode and calling

04:45.220 --> 04:46.570
the button interrupts.

04:46.570 --> 04:55.090
So now from this point after the line 12, whenever the signal on the PIN number two goes from high

04:55.090 --> 04:57.920
to low, this function will be called.

04:58.240 --> 05:04.690
So wherever we are in the program, the program will stop, the execution will stop, and this function

05:04.690 --> 05:05.350
will be called.

05:05.560 --> 05:11.510
After this function has finished, the program will resume wherever it is.

05:11.860 --> 05:18.800
OK, and one important thing here for the interim function, you should have avoid that type, OK?

05:18.830 --> 05:22.970
You should not return anything and you should not have any parameter.

05:23.190 --> 05:28.840
OK, that's the two conditions or interim function void and no parameter.

05:29.560 --> 05:30.310
What we want to do.

05:30.340 --> 05:34.160
We want to toggle the entity when we release the button.

05:34.660 --> 05:38.560
What I'm going to do, I'm going to create another function.

05:39.040 --> 05:40.530
Toggle edit OK.

05:42.350 --> 05:50.990
And in this function, so I'm going to create vitality, the rate is low, initialized to low.

05:51.200 --> 05:54.110
So this we already have done it a few times before.

05:54.140 --> 05:57.790
OK, I'm going to go quite fast if you.

05:59.070 --> 06:01.390
They is low.

06:01.590 --> 06:04.320
I would just invest.

06:05.230 --> 06:09.760
Every state becomes I, it ends.

06:13.670 --> 06:20.150
State becomes law, and after we have changed the state, we do digital.

06:22.850 --> 06:25.220
And the thing with the.

06:28.860 --> 06:34.890
OK, so we keep the state of the 80s, so whenever we enter the target area, we check what was the

06:34.890 --> 06:41.040
current state would change it to the other one because the only two states and then we use digital rights

06:41.040 --> 06:43.730
to apply the new state to the atypia.

06:44.040 --> 06:46.850
So then by using this, it's nice and clean.

06:46.860 --> 06:52.640
We have a function that does just what we wanted to do so we can just go ahead.

06:52.710 --> 06:59.670
We don't need to repeat that code anywhere else in the program and know what we could do is to call

06:59.670 --> 07:05.610
the toggle and inside the button, really interrupt, OK, because that's what we want to do.

07:05.940 --> 07:09.000
But that's not what we are going to do, actually.

07:09.540 --> 07:10.260
Why is that?

07:10.620 --> 07:17.100
Because the interim function will block your program, which stopped your program, and it's also going

07:17.100 --> 07:20.180
to disable every other interrupt.

07:20.330 --> 07:25.140
OK, so you have to know that in order, you know, you can create interrupts like this.

07:25.290 --> 07:30.900
But there are also other interrupts that run in the background that you may not be aware of.

07:31.050 --> 07:36.630
But the thing is that when you create and when you are in an interim function, every other interrupt

07:36.630 --> 07:41.960
is disabled and your main program is completely stopped looking.

07:41.960 --> 07:48.210
Hear what's wrong with that is that we use digital right to write is a function to do an action and

07:48.210 --> 07:51.060
this will take a few microseconds.

07:51.370 --> 07:54.560
OK, and this is more than just changing the state.

07:54.570 --> 07:56.280
OK, changing variable.

07:56.580 --> 08:02.970
So a best practice for an interim function is just to change some variables.

08:02.970 --> 08:07.530
And then after you have changed some buyable, you will handle the action in the loop.

08:07.590 --> 08:09.510
OK, let's see how to do that.

08:09.960 --> 08:17.650
I'm not going to do add in the interrupt that would work for this example, but this is not a good practice.

08:18.210 --> 08:21.510
Instead, what I'm going to do is create a boolean.

08:22.930 --> 08:27.050
So when I create a boolean variable, I can also call it a flag.

08:27.290 --> 08:30.640
OK, like this button release.

08:32.500 --> 08:33.610
Which is false.

08:34.510 --> 08:39.360
And when we are in the interim and is going to do but then released.

08:40.490 --> 08:48.710
Is true, though, I said the fly, which means that I put the value to true inside this boolean and

08:48.710 --> 08:52.000
note that this is important when you use a valuable.

08:52.310 --> 08:59.270
So when you use a global valuable insight, an interim function, what you have to do when you're declared

08:59.270 --> 09:04.640
valuable, you have to add another keyword in front of the data type.

09:04.880 --> 09:06.170
And this keyword is.

09:10.090 --> 09:17.170
OK, so whenever you use a valuable insight, an interrupts, make the viable, volatile and by using

09:17.170 --> 09:23.440
volatile, you are just telling the world, you know, that you are going to use this valuable also

09:23.450 --> 09:27.010
interrupt so you may handle it better.

09:27.510 --> 09:35.410
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

09:35.410 --> 09:35.890
function.

09:36.950 --> 09:38.660
We are just going to check if.

09:39.530 --> 09:40.910
But then released.

09:41.820 --> 09:45.160
So when you take for a billion, you don't need to do this, OK?

09:45.450 --> 09:51.840
You don't need to check if the blame is true because the billion is already a bullion, so it's already

09:51.840 --> 09:52.540
bullion value.

09:52.560 --> 09:54.170
So this is just true.

09:54.190 --> 09:55.110
All this is false.

09:55.150 --> 10:02.250
OK, so if the button is released, then what we are going to do is we are going to do button release.

10:03.610 --> 10:07.150
Is false and Eric.

10:09.570 --> 10:17.250
So in the look on the on the action we do is to check for this flag it for this billion, if this balloon

10:17.250 --> 10:24.660
is true, we enter the if we unset the flag, so we set the boolean value to false or we don't enter

10:24.660 --> 10:25.440
it the next time.

10:25.560 --> 10:26.010
All right.

10:26.400 --> 10:28.000
And then we took the editing.

10:28.260 --> 10:31.730
So as you can see, your problem in the loop is very simple.

10:31.800 --> 10:33.150
Can you just check the flag?

10:33.420 --> 10:40.620
And then because you have attached and interact with the button and with this function whenever you

10:40.620 --> 10:47.460
release the button, OK, this will trigger the following mode, which will call this function, which

10:47.460 --> 10:54.090
will set this boolean to true OK, which means that the next time you read it in the loop, it is true

10:54.090 --> 10:55.620
and you can execute the action.

10:55.800 --> 11:01.830
And by doing this, you are sure that you don't miss the release of the button.

11:02.220 --> 11:02.630
All right.

11:02.700 --> 11:05.010
Now let's upload the program.

11:05.020 --> 11:06.150
So let's name it.

11:07.200 --> 11:07.880
Interrupts.

11:10.720 --> 11:19.330
OK, so you can see here the read, any on 12 is pulled off, I'm going to press on the button.

11:19.480 --> 11:26.320
You can see nothing happens and I Rollie's the button, the energy is put on, OK, when they release

11:26.320 --> 11:31.180
the button and then I press a release and then is put off.

11:31.180 --> 11:36.870
So the energy was just told, I'm going to go a little bit faster, OK.

11:38.050 --> 11:42.170
And as you can see, I just press another time and the energy is still on.

11:42.640 --> 11:43.300
Why is that?

11:43.570 --> 11:46.830
Well, we are coming back to the bones problem once again.

11:47.470 --> 11:54.130
So using interact with a push button in that specific situation means that you will know exactly when

11:54.130 --> 12:01.990
the state of the signal changes, but you still have the balance problem and the problem is simply a

12:01.990 --> 12:02.950
physical problem.

12:03.250 --> 12:07.730
So if we had the problem before with interrupts, we don't solve this problem.

12:08.290 --> 12:13.530
So in the next listen, we are going to see how to demand the button in the interrupt.
