WEBVTT

00:00.150 --> 00:07.230
And now here is the solution to avoid using delays, so you have seen how to use the minis and macros

00:07.230 --> 00:13.200
function to get the time, you have also discovered how to compute the duration of connection with those

00:13.200 --> 00:13.680
functions.

00:14.040 --> 00:19.620
And now we are going to use all of this knowledge in order to reproduce the behavior we had with delay,

00:19.630 --> 00:21.180
but without using delay.

00:21.450 --> 00:24.260
So for this, I'm going to make a simple test.

00:25.050 --> 00:31.290
I want to print something every 500 musician in the loop function so they'll begin.

00:34.590 --> 00:35.050
OK.

00:35.070 --> 00:45.240
And in the function, I'm going to do that to bring in a load, and so we have to delay 500, that would

00:45.240 --> 00:47.210
be with the delay function.

00:47.230 --> 00:53.520
OK, so gate to basic program, we bring Telo every five hundred milliseconds and now let's remove the

00:53.520 --> 00:56.740
delay and let's see how we can do without it.

00:57.060 --> 01:02.370
So of course if we just applaud the program like this, we don't print hello every 500 milliseconds.

01:02.370 --> 01:04.770
We print at full speed.

01:05.040 --> 01:07.830
So the full speed of the original execution.

01:08.370 --> 01:15.090
OK, so to be able to print hello every five hundred milliseconds, we're going to need to check the

01:15.090 --> 01:20.720
duration between the current time here in the Loop and the last time.

01:20.730 --> 01:24.990
So the previous time we have printed on the same monitor.

01:26.040 --> 01:32.460
And if the duration so the difference between the current time and the last time we have done the action

01:32.760 --> 01:37.560
is greater than 500 milliseconds, then we can execute the action.

01:37.920 --> 01:39.960
So we would need to store the previous time.

01:40.590 --> 01:43.170
I'm going to create a global viable here.

01:44.810 --> 01:54.440
And signed long and name its previous time, not just previous time, OK, but previous time still rates.

01:54.470 --> 02:00.220
So we know it's the previous time we have used the sale print function that we are going to use here

02:00.710 --> 02:01.180
and this.

02:01.190 --> 02:05.060
So I could just initialize it to zero to merely.

02:05.060 --> 02:10.410
So I'm just going to use Milli's because when the program starts, this would be zero.

02:10.430 --> 02:12.800
So this would just be executed.

02:12.830 --> 02:18.050
So this value is declared and defined with this value.

02:18.400 --> 02:23.930
So now in the loop function, what I'm going to do is I'm going to unsigned long.

02:25.340 --> 02:26.070
Crunch time.

02:26.380 --> 02:29.090
All I can right now.

02:30.180 --> 02:31.700
Equal to miss.

02:33.340 --> 02:37.440
So every time we enter the loop, we check what is the current time?

02:38.860 --> 02:44.190
And then I'm going to do if the condition, if the time No.

02:45.390 --> 02:55.320
Minus the previous time the sale printed, something here is greater than we want five hundred, I'm

02:55.320 --> 03:03.630
writing 500, then I execute this book of code and I put the sale inside the block of code.

03:05.680 --> 03:11.950
OK, so quick recap of what happened here, we create a viable to stop the previous time we have printed

03:11.950 --> 03:12.570
on the sale.

03:12.700 --> 03:19.390
OK, we initiate easy to zero at the beginning, of course, and then you loop it going in a loop.

03:19.900 --> 03:23.770
We take the time now, which is the time we meet.

03:24.040 --> 03:30.100
OK, so every time we go in the loop, we check what is occurring and then we make the difference between

03:30.100 --> 03:35.360
the current time, time now and the previous time we have printed on the same.

03:35.560 --> 03:42.290
OK, so if you're running from the last lesson we do this, you are just creating a duration.

03:42.310 --> 03:43.930
OK, time now.

03:43.930 --> 03:46.750
Minus time in the past is the duration.

03:47.230 --> 03:50.380
This duration must be greater than five hundred.

03:50.410 --> 03:51.970
So we execute the action.

03:52.420 --> 03:57.880
OK, so every time we're going to look at said the beginning this would be one and then two, three,

03:57.880 --> 04:03.430
four, five, etc. So this will not be greater than five hundred.

04:03.430 --> 04:05.730
And then we don't execute this action.

04:06.370 --> 04:13.720
So not when the minutes reaches five hundred for the first time, this would be greater than 500.

04:13.730 --> 04:14.980
So maybe more.

04:14.980 --> 04:18.620
Five hundred and one, because it's really greater than that.

04:18.700 --> 04:23.830
When this is equal to five hundred and one or more, we enter the.

04:25.080 --> 04:32.360
But then we need to do something because the previous time, so you'll print will always be zero here

04:32.460 --> 04:34.220
because we have initialize it to zero.

04:34.230 --> 04:40.340
So this condition after the minutes here reaches five hundred and one will always be true.

04:40.530 --> 04:44.500
And so we will keep printing this very, very fast.

04:44.520 --> 04:49.020
So what we need to do is that whenever we enter this block of code so we can do an action.

04:49.020 --> 04:51.660
And we said the previous time.

04:54.720 --> 04:55.920
The current.

04:59.760 --> 05:07.070
So if the current time is five hundred, about 500, then we are going to enter this and then the previous

05:07.070 --> 05:14.780
time will become five hundred, which means that not when we go in the loop again, but let's say now

05:14.780 --> 05:16.550
the time is five hundred and ten.

05:17.660 --> 05:23.400
The previous time would be five hundred, which means that this is not greater than five hundred.

05:23.480 --> 05:31.640
OK, but we are only going to go back to the if when the time now is equal to 1000 or more.

05:32.390 --> 05:33.670
But let's actually try this.

05:34.550 --> 05:39.530
I'm going to upload old, let's say, without delay.

05:41.840 --> 05:50.150
And let's use the same lingo here, and you would see hello, hello, printed every five hundred milliseconds.

05:50.390 --> 05:50.920
Great.

05:51.470 --> 05:55.480
So it works and we haven't used any day here, as you can see.

05:56.240 --> 06:00.540
So now, of course, this is a little bit more code, but what is the advantage here?

06:01.010 --> 06:04.050
The advantage is that you never block the instructions.

06:04.070 --> 06:06.350
OK, this will run at full speed.

06:06.740 --> 06:09.650
OK, this takes very little time to do.

06:09.670 --> 06:10.970
This is just a condition.

06:11.300 --> 06:14.440
And this is just you set value inside of value.

06:14.460 --> 06:20.570
So this will take almost no time, which means that if you have a second action, you can do this.

06:20.840 --> 06:26.750
The second action in the loop function here using a similar structure and thus you can do many things

06:26.750 --> 06:27.570
at the same time.

06:27.590 --> 06:31.520
OK, you don't block the program anymore with the delay function.

06:32.000 --> 06:32.380
All right.

06:32.390 --> 06:33.460
And let's come back to this.

06:33.470 --> 06:35.020
I'm going just to improve a little bit.

06:35.510 --> 06:36.620
So here I have you.

06:36.710 --> 06:38.470
Five hundred, which is upgraded.

06:38.960 --> 06:40.420
We can do better than this.

06:40.430 --> 06:41.150
We can do.

06:41.640 --> 06:42.220
Let's see.

06:42.230 --> 06:45.740
Unsign long time interval.

06:47.750 --> 06:51.720
So rate right, which is five hundred.

06:52.990 --> 06:59.350
The we use an inside long as well, OK, because everything we compare here, unsane longsword, just

06:59.350 --> 07:05.290
give the same data type and then we are just defining a time interval viable here.

07:05.300 --> 07:07.810
We say Alperin so we know it's for this action.

07:08.440 --> 07:09.550
And so I can just.

07:10.680 --> 07:11.430
Do this here.

07:12.330 --> 07:18.210
OK, this is better if you can avoid hard cutting values inside the education of your code and create

07:18.210 --> 07:26.240
variables instead of constant and know to improve this, I'm going to also just show you something else.

07:28.680 --> 07:30.000
And then let's bring.

07:31.100 --> 07:32.090
Milice here.

07:33.360 --> 07:35.190
So let's see let's see what happens.

07:37.560 --> 07:43.260
So we bring hello, we also the time, so you see five hundred and one, one thousand and two, etc.,

07:43.470 --> 07:46.510
and you can see we are shifting a little bit.

07:46.580 --> 07:50.570
OK, the eight, 10, 11 Segan.

07:50.940 --> 07:51.980
But you can see it.

07:52.020 --> 07:53.500
I'm going to remove the scroll.

07:54.090 --> 07:59.460
You can see here, this is not exactly 14 Siggins, OK?

07:59.470 --> 08:05.190
We have twenty eight additional milliseconds and this is a shift that is going to increase every time.

08:05.250 --> 08:05.940
Why is that?

08:06.570 --> 08:09.750
And this is simply because, well, we executed some action.

08:10.110 --> 08:13.400
The action takes some time to execute, basically.

08:13.650 --> 08:19.530
And when you record time now, of course, there is always a little delay that will happen because you

08:19.530 --> 08:20.850
execute the actions.

08:21.270 --> 08:24.630
So what are we going to do instead of sitting the previous time?

08:24.630 --> 08:33.750
So point to time, know what we can do is simply add the time interval I'm going to do plus equal.

08:35.500 --> 08:41.860
OK, so plus equals simply means that you are going to add this to the variable on the left.

08:42.700 --> 08:46.790
So I simply add the time interval to the previous time.

08:46.870 --> 08:53.470
OK, so whenever we execute the action, I'm going to add plus five hundred, which means that all the

08:53.470 --> 08:59.800
small delays that we have added when actually executed some code will not be taken into account.

08:59.950 --> 09:05.120
But if you don't understand exactly that, let's just see the result and you would see that is better.

09:05.650 --> 09:09.730
Now you can see we have 500, so we still have a small delay for the first one.

09:09.970 --> 09:13.330
But then you can see we already have a good volume.

09:13.340 --> 09:16.500
So we don't have a shift here.

09:16.840 --> 09:24.970
You can see after 13 Segan after 14 seconds, OK, we have 14 and then zero zero one.

09:25.630 --> 09:31.960
So you can see there is exactly 500 milliseconds before each instruction.

09:32.950 --> 09:33.280
All right.

09:33.290 --> 09:39.190
So to recap, first, you create an unsigned, long previous time for the action.

09:39.970 --> 09:44.260
You initialize to zero images and you create a time interval.

09:44.380 --> 09:48.960
OK, the timetable will be basically what you want to put in your delay here.

09:49.040 --> 09:53.290
Five hundred milliseconds, then in the loop function, you get the time.

09:53.890 --> 09:55.120
Time now with Milice.

09:55.540 --> 10:02.740
And then for each action you want to do, you check the difference between the time now and the previous

10:02.740 --> 10:03.890
time you did the action.

10:03.940 --> 10:05.470
This gives you a duration.

10:06.500 --> 10:13.880
You check if the direction is greater than the time interval, you have said, if, yes, you do direction

10:14.450 --> 10:20.960
and then you add the time interval to the previous time, which means that you are only going to go

10:20.960 --> 10:26.540
in if again after the time interval has passed another time.

10:27.140 --> 10:27.520
All right.

10:27.530 --> 10:33.770
So this concept is not the easiest concept to understand, but with the following hands on lessons and

10:33.770 --> 10:39.500
with the following practice exercises, you would be able to get a better grasp and then, of course,

10:39.500 --> 10:42.170
come back to this lesson to better understand.
