WEBVTT

00:00.210 --> 00:06.810
Let's now face the problem that we had with the false information when we get the data from the ultrasonic

00:06.810 --> 00:12.960
sensor, because this function will take more or less time depending on the distance measured by the

00:12.960 --> 00:13.480
sensor.

00:14.040 --> 00:21.030
But if the distance is quite big, then this will really take a huge amount of time and that is not

00:21.030 --> 00:22.920
acceptable for the program.

00:23.190 --> 00:24.870
So here we are going to use interrupts.

00:25.080 --> 00:29.430
I'm going to explain to you first the principle and then we are going to write the code.

00:29.790 --> 00:32.000
Not that this is a little bit more advanced.

00:32.250 --> 00:37.180
We arrived at some point where it's really not but beginning to live it anymore.

00:37.500 --> 00:43.200
This isn't this specifically is a little bit more of an intermediate or advanced level.

00:43.560 --> 00:48.540
But I guess you should be fine because you have already seen and practiced with everything you need

00:48.540 --> 00:50.250
to know to understand this.

00:50.610 --> 00:52.630
So first, let's see how to solve the problem.

00:53.930 --> 01:01.900
So here is the classic place in that we have seen so far, so you goldbugs in that is the first thing,

01:01.910 --> 01:08.570
then we wait, OK, as long as the signal is low and then the signal goes to high.

01:08.600 --> 01:14.540
So this is the pulsing with the high mode both in we start measuring and then when the signal goes to

01:14.540 --> 01:18.620
localising, we stop measuring and return the pulse duration.

01:19.130 --> 01:24.320
So between them, the number one and number four, everything here will block your program.

01:24.890 --> 01:31.760
So if, for example, the wait here is the signal stays low for a long time, well, that's more time

01:31.760 --> 01:32.750
to plug the program.

01:32.960 --> 01:35.600
And then the longer the pulse, the longer the pulse.

01:35.600 --> 01:37.600
It will take time to measure the pulse.

01:37.610 --> 01:39.160
And also we block your program.

01:39.410 --> 01:46.060
So our sensor, if you have a greater distance, this will be bigger and then you will wait more.

01:46.310 --> 01:50.040
So you have two times here where you are just going to wait.

01:50.060 --> 01:52.610
And the thing is that you actually just need two things.

01:52.610 --> 01:58.850
You need to start measuring this point here and measure this point here and make the difference so the

01:58.850 --> 02:02.300
person does the job OK, does the job already correctly.

02:02.300 --> 02:09.200
We have the correct value every time, but the time between the point one, point four is really too

02:09.200 --> 02:10.880
much can really be too much.

02:11.240 --> 02:18.110
So what we're going to do, we are going to do the same thing, which is basically to measure this duration,

02:18.530 --> 02:21.530
but using interrupts and how are we going to do this?

02:21.750 --> 02:23.420
So here is the person.

02:24.260 --> 02:27.200
We are going to do this in three steps.

02:27.740 --> 02:29.620
The first step we're going to set.

02:29.640 --> 02:37.580
So an interrupt when the signal changes and then when the signal goes from low to high, we are going

02:37.580 --> 02:39.170
simply to get the time.

02:39.350 --> 02:41.840
We are going to store the time in global view.

02:42.110 --> 02:48.560
So, for example, time begins and then when our program continues to execute and we have another interruptive

02:49.100 --> 02:52.000
OK, using still the change mode.

02:52.010 --> 02:55.460
So the change is the rising mode and the following mode.

02:55.760 --> 03:00.140
So when the signal is falling, we are simply going to also get the time.

03:00.860 --> 03:04.270
So we get the time at the beginning, we get the time at the end.

03:04.490 --> 03:07.970
And then here we set a flyer, we set the global flag.

03:08.300 --> 03:11.140
And the third step is, look what we do.

03:11.150 --> 03:12.920
We simply check for that flag.

03:13.370 --> 03:17.000
And then once we have this flag, we compute the distance.

03:17.150 --> 03:18.590
How do we conclude the distance?

03:18.780 --> 03:21.580
Well, we have the time and we have the time begin.

03:21.860 --> 03:24.010
We do timing minus time being.

03:24.440 --> 03:31.760
This gives us the duration of the inputs and from the duration we have the formula to convert this into

03:31.910 --> 03:32.630
a distance.

03:33.530 --> 03:36.140
So that's actually pretty simple.

03:36.650 --> 03:41.300
And the program is not blocked anymore because here you still continue the execution.

03:41.540 --> 03:43.220
You just have to interrupt.

03:43.940 --> 03:47.060
That is fired twice where you simply get the time.

03:47.070 --> 03:51.020
So this is very quick and then you simply take a flag inside the envelope.

03:51.020 --> 03:53.380
So this will take almost no time.

03:54.630 --> 04:00.270
And back to the code, let's just use the Internet, just like I have explained with the diagram.

04:00.630 --> 04:06.330
So I'm going to start by setting up the interrupt on the equal number three.

04:06.510 --> 04:07.750
And again, this is important.

04:07.800 --> 04:12.300
Make sure that the equipment is connected on an interactive podiatric.

04:13.080 --> 04:20.510
So I'm going to create a function here that's that's void because this is an indirect we don't have

04:20.510 --> 04:21.030
the choice.

04:21.540 --> 04:24.810
Let's call it equal to interrupt.

04:27.220 --> 04:33.660
And no parameter because we don't have the choice either voit no idea what I'm going to do.

04:33.670 --> 04:41.140
I'm going to do a Dutch interrupt with digital bean to.

04:42.390 --> 04:46.240
Internet and digital ping is the echo here.

04:47.370 --> 04:55.640
OK, I appreciate Cooma, I press enter, just make it nicer to read, so the function here, AECO been

04:55.650 --> 04:56.170
interrupted.

04:57.220 --> 05:04.240
And then the mode, so the mode we need, the mode is change, OK, because we need to know when the

05:04.240 --> 05:10.390
signal is arising, which is going from low to high and when the signal is falling, which is going

05:10.390 --> 05:14.620
from high to low and change will give us both.

05:14.920 --> 05:22.510
So now that we have done that, this will be triggered every time we have a change in the ECOFIN signal.

05:22.960 --> 05:28.990
And now when we are in this interrupt, when actually because this is a change mode, we don't necessarily

05:28.990 --> 05:33.870
know if the signal is rising or if the snow is falling.

05:34.150 --> 05:39.620
So because of this, when you use change, you need to check again the signal.

05:39.640 --> 05:44.770
So if digital read Iku in.

05:45.830 --> 05:58.460
Is I OK and then to do something else, so if the signal is low, but if you use the following mode,

05:58.460 --> 06:01.370
you know the signal is low because it's going to low.

06:01.580 --> 06:04.340
If you use the rising mode, you know, the signal is high.

06:04.760 --> 06:09.620
But if you use change, you just know that the signal went from low to high or high to low, but you

06:09.620 --> 06:10.620
don't know which one.

06:10.940 --> 06:12.870
So you need to check with digital radio.

06:13.310 --> 06:18.380
OK, so usually when I've told you is to discourage you to use digital right.

06:18.380 --> 06:23.330
Or other functions in the interactivity, you don't have the choice you need to use digital, which

06:23.330 --> 06:25.010
will be quite fast.

06:25.640 --> 06:29.730
So here we know the signal has changed and we've high.

06:29.760 --> 06:31.610
We know the signal is rising.

06:33.240 --> 06:42.160
And as the signal is falling, the when the signal is rising, what do we do when we start measuring

06:42.160 --> 06:47.250
it and when the signal is fully, we would stop measuring.

06:47.940 --> 06:49.530
So how do we start measuring?

06:49.590 --> 06:53.610
Well, we are going to create two new variables here.

06:57.350 --> 06:59.030
Unsigned, long.

07:00.130 --> 07:01.030
Both in.

07:02.610 --> 07:03.840
Time Biggart.

07:06.140 --> 07:10.960
Can not going to put any value in this because we are going to initialize them or just said the value

07:10.970 --> 07:18.680
directly in the Internet and then unsigned long just in time.

07:18.960 --> 07:25.910
And so I just named it both in time being OK, because this is the time when the police in Beijing,

07:25.910 --> 07:32.660
basically when the signal goes to I in here, when the signal goes to low end because we are going to

07:32.660 --> 07:38.830
modify them inside the interrupts, I'm going to put volatile it and also keep.

07:41.540 --> 07:44.900
So not what we do is quite simple, we do.

07:46.220 --> 07:50.150
I'm going to take place in time.

07:51.220 --> 07:53.200
Begin is equal to.

07:54.810 --> 07:55.400
Microbes.

07:55.590 --> 08:01.880
OK, I use microbes because we have the pulse in function, we were getting a duration in microseconds,

08:02.070 --> 08:03.300
so I'm going to keep that.

08:03.300 --> 08:06.210
I'm going to also consider duration in microseconds.

08:06.570 --> 08:15.860
And then when the signal is falling, we do those in time and which is equal to microbes.

08:16.530 --> 08:22.570
So we have stored the two times that are important, which is when the signal went high.

08:23.040 --> 08:27.120
So when the pulse begins and when the signal went low.

08:27.150 --> 08:30.750
So when the pulse ends, I know how to conclude this.

08:30.960 --> 08:38.790
Well, I'm going to add in a new thing, which is I'm going to create so volatile.

08:41.420 --> 08:43.220
Bu new.

08:44.590 --> 08:45.250
Dist..

08:47.100 --> 08:47.740
Available.

08:48.880 --> 08:54.630
And let's initialize this to faults, so I'm going to create a boolean, which I can also call flag,

08:54.670 --> 08:57.530
can be volatile, new distance available.

08:58.240 --> 08:59.700
So this is false.

08:59.890 --> 09:04.230
And here when we stop, the measurement are when we just get the time.

09:04.540 --> 09:07.120
In the end, I'm going to do so.

09:07.150 --> 09:08.290
The name is new.

09:10.310 --> 09:14.030
Distance available is true.

09:14.780 --> 09:19.970
OK, so just with the interrupt running, so we have to interrupt, just with the interrupt running,

09:19.970 --> 09:27.320
we will automatically get the three variables updated with the time, but the beginning of the birth,

09:27.330 --> 09:32.780
the time for the end of the post and when also we update this, we update this boolean to true.

09:33.350 --> 09:37.460
So now what we need to do is to check in the loop for this billion.

09:38.270 --> 09:39.870
So I'm going to run that line here.

09:40.970 --> 09:43.760
We're not going to get the distance in this.

09:44.180 --> 09:52.220
We are going simply to keep this structure here to simply trigger the ultrasonic sensor every 100 million

09:52.970 --> 09:53.690
signal here.

09:55.740 --> 09:59.280
But we are simply going to tringale and then I'm going to do if.

10:00.450 --> 10:00.840
New.

10:02.230 --> 10:04.320
Distance available.

10:05.650 --> 10:09.560
So is the flag that we have set in the interrupt is true.

10:10.120 --> 10:12.760
So what I'm going to do is first say to false.

10:16.730 --> 10:23.570
So we do not enter this another time, we enter this only another time, if we have also put the flag

10:23.570 --> 10:28.350
to true again in the interim, and then I do the action and the action is.

10:28.370 --> 10:35.600
So let's say double distance is equal to get ultrasonics.

10:37.310 --> 10:44.960
Distance and I'm going also to modify the jet ultrasonic distance, so to get the duration of the pulse

10:44.960 --> 10:46.050
in microseconds.

10:46.400 --> 10:49.930
Now I'm going to remove the person we are not going to use in.

10:50.150 --> 10:55.320
I'm simply going to compute the duration with the time that we already got in the program.

10:55.640 --> 11:04.910
So both in time and minus, but in time begin and.

11:04.910 --> 11:05.980
Well, that's it.

11:06.800 --> 11:12.440
Will this we get the duration of the birds and then when the competition is exactly the same.

11:12.950 --> 11:14.900
OK, so we get the distance.

11:14.900 --> 11:21.080
So I could just print these, but I'm going to do to get violent and do sale of prints.

11:21.110 --> 11:21.620
Ellen.

11:22.860 --> 11:32.340
With the distance, let's actually verify the code, so I have a trussoni project, not the activity,

11:32.730 --> 11:34.590
and let's upload the code.

11:35.010 --> 11:36.200
Let's see if it works.

11:37.960 --> 11:45.250
And let's open the science monitor and you can see here, well, we have the exact same values as we

11:45.250 --> 11:45.830
had before.

11:45.850 --> 11:49.020
So the functionality, I'm going to put my hand closer, OK?

11:49.060 --> 11:52.530
You can see the functionality is exactly the same as before.

11:52.930 --> 11:57.460
But no, we are not stuck with the bills in function.

11:57.850 --> 12:05.770
So to quickly recap, when I do the setup, I also attach and interrupt or coping with the change mode.

12:06.230 --> 12:13.320
OK, then in the loop, what I do is every one hundred milliseconds I trigger the ultrasonic sensor,

12:13.330 --> 12:21.010
so I send you a high signal for 10 microseconds and then they exit and then, well, the program can

12:21.010 --> 12:24.600
continue and then everything will happen in the background.

12:24.610 --> 12:30.390
So the ultrasonic sensor will send back some data on the equipping.

12:30.670 --> 12:32.730
And what we need to do is to measure the pulse.

12:33.040 --> 12:38.310
So we have the interrupt, which will be triggered when the signal is rising.

12:38.650 --> 12:41.470
This time we initialize the pulse in time.

12:41.470 --> 12:46.420
Begin with the time right now when the interrupt is fired and then interrupt exits.

12:46.750 --> 12:52.010
So the program continues to execute with all the other actions in the code.

12:52.030 --> 12:53.400
OK, no problem with that.

12:53.830 --> 13:00.520
And then when the signal on the equipment goes to low, well, we also have the trigger of this interrupt

13:00.520 --> 13:01.060
the same.

13:01.570 --> 13:07.050
And now we know that we need to update the time and OK and set the flag to true.

13:07.810 --> 13:10.900
So then what happens is in the loop function.

13:11.260 --> 13:12.640
We can check the fly.

13:12.700 --> 13:17.470
OK, so we take the flag and when this is true, well, so we set it to faults of course.

13:17.830 --> 13:25.750
And well we can do the functionality which is get the distance and we get the distance simply by subtracting

13:25.750 --> 13:33.070
the time and the time beginning that we have set in to interrupt and then when we complete the distance

13:33.070 --> 13:35.930
from the duration of this computation.

13:36.280 --> 13:42.130
So this call now is pretty good, pretty efficient, and we are not stuck any time in the program.

13:42.640 --> 13:48.910
Note again that here the delay microseconds are so small compared to what we had here.

13:49.390 --> 13:50.830
It's ten microseconds.

13:51.210 --> 13:55.460
It was more about twenty five thousand microseconds.

13:55.480 --> 14:01.330
OK, so depending on the number, if it's a very small number like this, you can use it in your code

14:01.330 --> 14:02.530
with the microseconds.

14:02.530 --> 14:04.610
If it's too big, then it's better to avoid it.

14:04.990 --> 14:09.360
And one thing to note here is that frequency here.

14:09.370 --> 14:12.130
So we have every one hundred milliseconds.

14:12.850 --> 14:16.360
It's best to keep a minimum of 60.

14:16.370 --> 14:19.540
OK, this is recommended for these components.

14:19.930 --> 14:25.900
Were having to put one hundred, but tried to use 60 and not below 60.

14:25.910 --> 14:29.320
OK, this is recommended by the supplier of the component.

14:29.590 --> 14:32.110
And also if you go to low with this value, let's say you.

14:32.120 --> 14:40.600
But then when the thing is that you are going to trigger the trigger being very, very fast and then

14:40.600 --> 14:45.820
the sensor will not be able to keep up with the data to send on the A.P. and the data you will receive

14:46.030 --> 14:48.640
interest might be not correct.

14:48.850 --> 14:51.510
OK, so 100 is good.

14:51.700 --> 14:57.510
Try to go at least 60 or 100 or more and everything will be fine with this program.
