This is the solution of the previous activity where you need to complete the duration of this line,
so the line with the false information, you need to know basically how long this line takes to execute.
So you can see here first we call this function already here in the void loop every one hundred milliseconds.
So every 100 meters that we trigger the ultrasonic sensor and then we call the function to get the distance.
And in that function, we call those in.
So that means we can directly measure the time right here.
So what I'm going to do and we unsigned long time making, which is I couldn't use means I can use micros,
OK, if I want to be more precise.
So I get the time before the action.
I do the action.
I get the time.
The actual time and make sure if you use macros that you use micros here and if you use minutes you
use Moelis, you want to compare things that can be compared.
Now, I'm going to sign long that's called it's called duration.
And that would be time and minus time.
So I'm just making the subtraction between the time after the action and the time before the action,
which gives the duration of the action basically.
And now what I can do is say that let's do say a print.
Duration.
And say of print and then with code duration viable.
So every time we go to a new line to what will happen here is that first, so we call the function that
we execute the action, the functionality doesn't change here, that we add a new piece of code, that
we complete the duration in print the direction.
Then we return the distance and we print the distance, we say of the printing.
So we will first get the duration and then the distance.
All right.
And now let's.
So it's the fire code.
I have named this activity in 14.
OK, and let's upload the code to Jeronimo.
But make sure they are connected and know I am going to open the sale Moniteau.
And let's see what we get.
OK, you can see we still have the distance in centimeters, so I have here one hundred thirty seven,
eight centimeters and the duration of the pulse in function.
So the total duration of this line of code, which is here eight thousand five hundred, which means
that here, between this point and this point, you have eight point five milliseconds that we're used
by this function, which is actually quite a lot.
No.
OK, let's make some experiments.
Now I'm going to put my hand in front of the sensor so you can see we have 10 but 10 centimeters.
So the shorter the distance, well, the shorter also the duration.
OK, you can see now if I increase, the distance increases and the duration also increases and I remove
my hand.
This is the wall behind me.
So I have eight point five thousand.
I'm going to move this a little bit so I can maybe increase the distance here.
I have 220 centimeters OK here and I got about 13000 microseconds.
OK, which means that for 200 centimeters, dysfunction will take 13 milliseconds.
And this is not even the maximum.
The maximum is four hundred centimeters, which means that I don't know exactly, but this will take
between 20 and 25 milliseconds.
OK, the person we rate for 20 to 25 milliseconds.
So what is the problem here?
The problem is that, well, previously with the time functionalities, we have removed all of the delays.
OK, so the problem is not stuck and we can make multiple actions run in parallel.
And we also don't miss any data and we can have a more efficient code.
Well, as you can see here with the both in function, it's just like we've added the delay back in
our code, because if there is not oxygen, the person will take 25 milliseconds to execute between
20 and 25, which is if you compare to how frequently we call that here, we call that every one hundred
milliseconds.
So every 100 milliseconds we are going to spend up to twenty five milliseconds just with this line.
So we are going to wait 25 percent of the computing time of the original, which is well, actually
quite a lot because now imagine that you don't have one since like this, but you have four sensors
like this, which is actually a real case in real robots.
So if you have false insults and you wait for twenty five milliseconds each, well, you're going to
have some problems because all the time will be spent waiting in the person.
So you can see this is really, really useful to measure the duration with this technique.
OK, you can easily spot.
So if you have a dog here, I have of course given you the exact line to check.
But if you have a doubt, you can just do this and maybe maybe your dog is unfounded and and you have
a problem somewhere else, but maybe your dog is funded and you find that one line is just taking so
much time to execute and that is a burden for your program.
So now that we know this takes too long to execute well, what can we do about it?
In the next lesson, I'm going to show you simply how to get the same functionality that without the
same function and using Interent.