WEBVTT

00:00.360 --> 00:07.100
This is the solution of the previous activity where you need to complete the duration of this line,

00:07.110 --> 00:14.230
so the line with the false information, you need to know basically how long this line takes to execute.

00:14.910 --> 00:23.790
So you can see here first we call this function already here in the void loop every one hundred milliseconds.

00:23.790 --> 00:30.020
So every 100 meters that we trigger the ultrasonic sensor and then we call the function to get the distance.

00:30.390 --> 00:32.490
And in that function, we call those in.

00:32.520 --> 00:36.660
So that means we can directly measure the time right here.

00:37.020 --> 00:47.300
So what I'm going to do and we unsigned long time making, which is I couldn't use means I can use micros,

00:47.310 --> 00:49.920
OK, if I want to be more precise.

00:50.400 --> 00:53.240
So I get the time before the action.

00:53.670 --> 00:54.600
I do the action.

00:54.990 --> 00:55.890
I get the time.

00:58.660 --> 01:05.530
The actual time and make sure if you use macros that you use micros here and if you use minutes you

01:05.530 --> 01:09.280
use Moelis, you want to compare things that can be compared.

01:09.520 --> 01:15.040
Now, I'm going to sign long that's called it's called duration.

01:15.940 --> 01:19.300
And that would be time and minus time.

01:21.040 --> 01:26.680
So I'm just making the subtraction between the time after the action and the time before the action,

01:26.680 --> 01:29.480
which gives the duration of the action basically.

01:30.100 --> 01:33.250
And now what I can do is say that let's do say a print.

01:34.530 --> 01:35.250
Duration.

01:37.120 --> 01:43.090
And say of print and then with code duration viable.

01:45.120 --> 01:53.200
So every time we go to a new line to what will happen here is that first, so we call the function that

01:53.250 --> 02:00.360
we execute the action, the functionality doesn't change here, that we add a new piece of code, that

02:00.360 --> 02:02.580
we complete the duration in print the direction.

02:02.880 --> 02:06.830
Then we return the distance and we print the distance, we say of the printing.

02:06.900 --> 02:09.880
So we will first get the duration and then the distance.

02:10.200 --> 02:10.610
All right.

02:10.620 --> 02:11.660
And now let's.

02:12.390 --> 02:13.710
So it's the fire code.

02:13.890 --> 02:16.800
I have named this activity in 14.

02:16.950 --> 02:22.050
OK, and let's upload the code to Jeronimo.

02:23.800 --> 02:28.990
But make sure they are connected and know I am going to open the sale Moniteau.

02:30.430 --> 02:32.080
And let's see what we get.

02:32.280 --> 02:40.630
OK, you can see we still have the distance in centimeters, so I have here one hundred thirty seven,

02:40.630 --> 02:45.150
eight centimeters and the duration of the pulse in function.

02:45.160 --> 02:52.630
So the total duration of this line of code, which is here eight thousand five hundred, which means

02:52.630 --> 03:01.960
that here, between this point and this point, you have eight point five milliseconds that we're used

03:01.960 --> 03:05.340
by this function, which is actually quite a lot.

03:05.350 --> 03:05.640
No.

03:06.430 --> 03:08.030
OK, let's make some experiments.

03:08.230 --> 03:13.900
Now I'm going to put my hand in front of the sensor so you can see we have 10 but 10 centimeters.

03:15.070 --> 03:19.780
So the shorter the distance, well, the shorter also the duration.

03:19.830 --> 03:27.490
OK, you can see now if I increase, the distance increases and the duration also increases and I remove

03:27.490 --> 03:27.940
my hand.

03:27.970 --> 03:29.200
This is the wall behind me.

03:29.720 --> 03:31.810
So I have eight point five thousand.

03:32.080 --> 03:36.800
I'm going to move this a little bit so I can maybe increase the distance here.

03:36.880 --> 03:47.560
I have 220 centimeters OK here and I got about 13000 microseconds.

03:48.070 --> 03:54.270
OK, which means that for 200 centimeters, dysfunction will take 13 milliseconds.

03:54.280 --> 03:55.990
And this is not even the maximum.

03:55.990 --> 04:01.570
The maximum is four hundred centimeters, which means that I don't know exactly, but this will take

04:01.570 --> 04:04.760
between 20 and 25 milliseconds.

04:05.230 --> 04:09.630
OK, the person we rate for 20 to 25 milliseconds.

04:10.000 --> 04:11.220
So what is the problem here?

04:11.230 --> 04:17.970
The problem is that, well, previously with the time functionalities, we have removed all of the delays.

04:17.990 --> 04:23.050
OK, so the problem is not stuck and we can make multiple actions run in parallel.

04:23.200 --> 04:27.050
And we also don't miss any data and we can have a more efficient code.

04:27.370 --> 04:33.370
Well, as you can see here with the both in function, it's just like we've added the delay back in

04:33.370 --> 04:40.630
our code, because if there is not oxygen, the person will take 25 milliseconds to execute between

04:40.630 --> 04:47.500
20 and 25, which is if you compare to how frequently we call that here, we call that every one hundred

04:47.500 --> 04:48.220
milliseconds.

04:48.730 --> 04:56.400
So every 100 milliseconds we are going to spend up to twenty five milliseconds just with this line.

04:56.800 --> 05:03.160
So we are going to wait 25 percent of the computing time of the original, which is well, actually

05:03.160 --> 05:09.310
quite a lot because now imagine that you don't have one since like this, but you have four sensors

05:09.310 --> 05:12.830
like this, which is actually a real case in real robots.

05:13.420 --> 05:18.130
So if you have false insults and you wait for twenty five milliseconds each, well, you're going to

05:18.130 --> 05:23.980
have some problems because all the time will be spent waiting in the person.

05:24.640 --> 05:31.150
So you can see this is really, really useful to measure the duration with this technique.

05:31.200 --> 05:32.920
OK, you can easily spot.

05:32.930 --> 05:38.500
So if you have a dog here, I have of course given you the exact line to check.

05:38.650 --> 05:45.640
But if you have a doubt, you can just do this and maybe maybe your dog is unfounded and and you have

05:45.640 --> 05:50.680
a problem somewhere else, but maybe your dog is funded and you find that one line is just taking so

05:50.680 --> 05:54.230
much time to execute and that is a burden for your program.

05:54.820 --> 05:59.860
So now that we know this takes too long to execute well, what can we do about it?

06:00.220 --> 06:05.830
In the next lesson, I'm going to show you simply how to get the same functionality that without the

06:06.040 --> 06:09.520
same function and using Interent.
