Let's now face the problem that we had with the false information when we get the data from the ultrasonic
sensor, because this function will take more or less time depending on the distance measured by the
sensor.
But if the distance is quite big, then this will really take a huge amount of time and that is not
acceptable for the program.
So here we are going to use interrupts.
I'm going to explain to you first the principle and then we are going to write the code.
Not that this is a little bit more advanced.
We arrived at some point where it's really not but beginning to live it anymore.
This isn't this specifically is a little bit more of an intermediate or advanced level.
But I guess you should be fine because you have already seen and practiced with everything you need
to know to understand this.
So first, let's see how to solve the problem.
So here is the classic place in that we have seen so far, so you goldbugs in that is the first thing,
then we wait, OK, as long as the signal is low and then the signal goes to high.
So this is the pulsing with the high mode both in we start measuring and then when the signal goes to
localising, we stop measuring and return the pulse duration.
So between them, the number one and number four, everything here will block your program.
So if, for example, the wait here is the signal stays low for a long time, well, that's more time
to plug the program.
And then the longer the pulse, the longer the pulse.
It will take time to measure the pulse.
And also we block your program.
So our sensor, if you have a greater distance, this will be bigger and then you will wait more.
So you have two times here where you are just going to wait.
And the thing is that you actually just need two things.
You need to start measuring this point here and measure this point here and make the difference so the
person does the job OK, does the job already correctly.
We have the correct value every time, but the time between the point one, point four is really too
much can really be too much.
So what we're going to do, we are going to do the same thing, which is basically to measure this duration,
but using interrupts and how are we going to do this?
So here is the person.
We are going to do this in three steps.
The first step we're going to set.
So an interrupt when the signal changes and then when the signal goes from low to high, we are going
simply to get the time.
We are going to store the time in global view.
So, for example, time begins and then when our program continues to execute and we have another interruptive
OK, using still the change mode.
So the change is the rising mode and the following mode.
So when the signal is falling, we are simply going to also get the time.
So we get the time at the beginning, we get the time at the end.
And then here we set a flyer, we set the global flag.
And the third step is, look what we do.
We simply check for that flag.
And then once we have this flag, we compute the distance.
How do we conclude the distance?
Well, we have the time and we have the time begin.
We do timing minus time being.
This gives us the duration of the inputs and from the duration we have the formula to convert this into
a distance.
So that's actually pretty simple.
And the program is not blocked anymore because here you still continue the execution.
You just have to interrupt.
That is fired twice where you simply get the time.
So this is very quick and then you simply take a flag inside the envelope.
So this will take almost no time.
And back to the code, let's just use the Internet, just like I have explained with the diagram.
So I'm going to start by setting up the interrupt on the equal number three.
And again, this is important.
Make sure that the equipment is connected on an interactive podiatric.
So I'm going to create a function here that's that's void because this is an indirect we don't have
the choice.
Let's call it equal to interrupt.
And no parameter because we don't have the choice either voit no idea what I'm going to do.
I'm going to do a Dutch interrupt with digital bean to.
Internet and digital ping is the echo here.
OK, I appreciate Cooma, I press enter, just make it nicer to read, so the function here, AECO been
interrupted.
And then the mode, so the mode we need, the mode is change, OK, because we need to know when the
signal is arising, which is going from low to high and when the signal is falling, which is going
from high to low and change will give us both.
So now that we have done that, this will be triggered every time we have a change in the ECOFIN signal.
And now when we are in this interrupt, when actually because this is a change mode, we don't necessarily
know if the signal is rising or if the snow is falling.
So because of this, when you use change, you need to check again the signal.
So if digital read Iku in.
Is I OK and then to do something else, so if the signal is low, but if you use the following mode,
you know the signal is low because it's going to low.
If you use the rising mode, you know, the signal is high.
But if you use change, you just know that the signal went from low to high or high to low, but you
don't know which one.
So you need to check with digital radio.
OK, so usually when I've told you is to discourage you to use digital right.
Or other functions in the interactivity, you don't have the choice you need to use digital, which
will be quite fast.
So here we know the signal has changed and we've high.
We know the signal is rising.
And as the signal is falling, the when the signal is rising, what do we do when we start measuring
it and when the signal is fully, we would stop measuring.
So how do we start measuring?
Well, we are going to create two new variables here.
Unsigned, long.
Both in.
Time Biggart.
Can not going to put any value in this because we are going to initialize them or just said the value
directly in the Internet and then unsigned long just in time.
And so I just named it both in time being OK, because this is the time when the police in Beijing,
basically when the signal goes to I in here, when the signal goes to low end because we are going to
modify them inside the interrupts, I'm going to put volatile it and also keep.
So not what we do is quite simple, we do.
I'm going to take place in time.
Begin is equal to.
Microbes.
OK, I use microbes because we have the pulse in function, we were getting a duration in microseconds,
so I'm going to keep that.
I'm going to also consider duration in microseconds.
And then when the signal is falling, we do those in time and which is equal to microbes.
So we have stored the two times that are important, which is when the signal went high.
So when the pulse begins and when the signal went low.
So when the pulse ends, I know how to conclude this.
Well, I'm going to add in a new thing, which is I'm going to create so volatile.
Bu new.
Dist..
Available.
And let's initialize this to faults, so I'm going to create a boolean, which I can also call flag,
can be volatile, new distance available.
So this is false.
And here when we stop, the measurement are when we just get the time.
In the end, I'm going to do so.
The name is new.
Distance available is true.
OK, so just with the interrupt running, so we have to interrupt, just with the interrupt running,
we will automatically get the three variables updated with the time, but the beginning of the birth,
the time for the end of the post and when also we update this, we update this boolean to true.
So now what we need to do is to check in the loop for this billion.
So I'm going to run that line here.
We're not going to get the distance in this.
We are going simply to keep this structure here to simply trigger the ultrasonic sensor every 100 million
signal here.
But we are simply going to tringale and then I'm going to do if.
New.
Distance available.
So is the flag that we have set in the interrupt is true.
So what I'm going to do is first say to false.
So we do not enter this another time, we enter this only another time, if we have also put the flag
to true again in the interim, and then I do the action and the action is.
So let's say double distance is equal to get ultrasonics.
Distance and I'm going also to modify the jet ultrasonic distance, so to get the duration of the pulse
in microseconds.
Now I'm going to remove the person we are not going to use in.
I'm simply going to compute the duration with the time that we already got in the program.
So both in time and minus, but in time begin and.
Well, that's it.
Will this we get the duration of the birds and then when the competition is exactly the same.
OK, so we get the distance.
So I could just print these, but I'm going to do to get violent and do sale of prints.
Ellen.
With the distance, let's actually verify the code, so I have a trussoni project, not the activity,
and let's upload the code.
Let's see if it works.
And let's open the science monitor and you can see here, well, we have the exact same values as we
had before.
So the functionality, I'm going to put my hand closer, OK?
You can see the functionality is exactly the same as before.
But no, we are not stuck with the bills in function.
So to quickly recap, when I do the setup, I also attach and interrupt or coping with the change mode.
OK, then in the loop, what I do is every one hundred milliseconds I trigger the ultrasonic sensor,
so I send you a high signal for 10 microseconds and then they exit and then, well, the program can
continue and then everything will happen in the background.
So the ultrasonic sensor will send back some data on the equipping.
And what we need to do is to measure the pulse.
So we have the interrupt, which will be triggered when the signal is rising.
This time we initialize the pulse in time.
Begin with the time right now when the interrupt is fired and then interrupt exits.
So the program continues to execute with all the other actions in the code.
OK, no problem with that.
And then when the signal on the equipment goes to low, well, we also have the trigger of this interrupt
the same.
And now we know that we need to update the time and OK and set the flag to true.
So then what happens is in the loop function.
We can check the fly.
OK, so we take the flag and when this is true, well, so we set it to faults of course.
And well we can do the functionality which is get the distance and we get the distance simply by subtracting
the time and the time beginning that we have set in to interrupt and then when we complete the distance
from the duration of this computation.
So this call now is pretty good, pretty efficient, and we are not stuck any time in the program.
Note again that here the delay microseconds are so small compared to what we had here.
It's ten microseconds.
It was more about twenty five thousand microseconds.
OK, so depending on the number, if it's a very small number like this, you can use it in your code
with the microseconds.
If it's too big, then it's better to avoid it.
And one thing to note here is that frequency here.
So we have every one hundred milliseconds.
It's best to keep a minimum of 60.
OK, this is recommended for these components.
Were having to put one hundred, but tried to use 60 and not below 60.
OK, this is recommended by the supplier of the component.
And also if you go to low with this value, let's say you.
But then when the thing is that you are going to trigger the trigger being very, very fast and then
the sensor will not be able to keep up with the data to send on the A.P. and the data you will receive
interest might be not correct.
OK, so 100 is good.
Try to go at least 60 or 100 or more and everything will be fine with this program.