WEBVTT

00:00.330 --> 00:06.870
Let's not use what we've learned in the previous lesson to make some entities blink at different rates.

00:06.900 --> 00:11.680
OK, so I'm going to use two ladies and make them blink at two different frequencies.

00:12.330 --> 00:15.410
First, let's handle the first energy, OK?

00:16.110 --> 00:19.950
So define that is different.

00:20.610 --> 00:22.560
So I'm just going to add a new one.

00:22.560 --> 00:33.510
OK, in let's use spin room 12 in the set up in mode energy one in a Puzzo and went quite fast because

00:33.510 --> 00:34.460
you already know that.

00:35.070 --> 00:39.760
And then within the loop let's make this a living without the delay.

00:40.230 --> 00:51.840
So first thing I'm going to create and sang long previous time energy one that's new one like.

00:52.770 --> 01:04.410
This is initialised to me and then I need to add also time, long time interval, if you were.

01:07.030 --> 01:15.130
Which is to say, we want to make blink every 400, let's say four hundred and seventy millisecond.

01:15.160 --> 01:25.380
OK, so what we do in the loop first we do unsigned, long time, now is minutes.

01:25.390 --> 01:29.070
So we get the time, we get the current time in the loop function.

01:29.650 --> 01:37.530
And then we compare time now with the previous time we have made the break.

01:38.640 --> 01:41.310
And if this is greater than the time interval.

01:43.010 --> 01:50.000
Then we enter this book of code, the way we do is we are going to to your lady, OK?

01:51.480 --> 01:57.540
And just after all, before, it doesn't really matter, you can do previous time.

02:00.320 --> 02:02.870
Plus, equal time interval.

02:04.710 --> 02:06.620
And so we have Destructo.

02:08.030 --> 02:13.670
And then to talk about the idea what we're going to do, because here what we can just do digital,

02:13.670 --> 02:18.960
right, high end digital right now, we need to keep the state of the art.

02:19.310 --> 02:22.860
So I'm going to add a new value here into.

02:23.690 --> 02:29.150
The one eight, which starts at, say, low.

02:31.070 --> 02:37.190
And then I'm going to do this, but we have already done that in a previous activity, if the state.

02:39.880 --> 02:42.160
Is low with two equal sign.

02:43.390 --> 02:49.930
Then we do the one that is equal to high and then else.

02:52.380 --> 03:01.470
One state is equal to low, so if it's law, we said it's too high, if it's high, we said it too low

03:01.740 --> 03:03.690
and then we do digital.

03:03.780 --> 03:04.260
Right.

03:04.890 --> 03:09.930
Any one thing, we want it.

03:10.590 --> 03:10.980
All right.

03:10.990 --> 03:12.770
So what happened here in the loop?

03:12.810 --> 03:14.130
We get the Turrentine.

03:14.160 --> 03:20.570
We change the current time for tango, minus the previous time we linked the energy one.

03:21.180 --> 03:29.910
If this is greater than the interval, the time interval, we go inside that log of code that we execute

03:29.910 --> 03:34.560
the action and then we add a timetable to the previous time.

03:34.980 --> 03:41.040
So we make sure that we don't enter if just after, but we make sure that we are going to wait, at

03:41.040 --> 03:48.600
least for the time interval to enter the if again and to do the action we need to store the red states

03:48.630 --> 03:51.900
outside of this loop function.

03:51.910 --> 03:57.680
OK, because every time we enter the loop function and then if here we are going to toggle the added

03:57.780 --> 04:00.420
state, we need to know what was the previous date.

04:00.840 --> 04:09.530
So we initially that date to lock as an arbitrary choice and then we compare it to know if it's law,

04:09.540 --> 04:16.260
we set it too high, if we set it to low, and then we simply do digital right at any point with the

04:16.260 --> 04:17.820
state that we have just modified.

04:18.600 --> 04:20.880
And let's upload the program.

04:20.890 --> 04:23.580
So it's first verify the.

04:24.740 --> 04:28.260
Listening to it is because I mean to another one, just Attaran.

04:31.720 --> 04:33.070
OK, let's applaud the code.

04:35.790 --> 04:43.920
And you can see the edit is for the first lady is linking, OK, at four hundred and seventy milliseconds

04:43.920 --> 04:45.500
between each link.

04:46.350 --> 04:50.210
So we have just solved the delay problem for the editing.

04:50.460 --> 04:57.630
And now, of course, this just forward is a little bit overkill because if you just need a program

04:57.630 --> 05:04.240
with just one energy, then why would you use this instead of the simple solution with the delay?

05:04.680 --> 05:10.920
Well, because if not, you want to blink and other energy with a different rate, that would be much

05:10.950 --> 05:11.360
easier.

05:11.580 --> 05:13.720
And that's what I'm going to do right now.

05:13.950 --> 05:16.560
So let's add the other one.

05:16.740 --> 05:18.300
Define energy to.

05:19.580 --> 05:25.740
In which has been number 11 here in Moad and.

05:26.880 --> 05:29.910
To begin with output.

05:31.100 --> 05:32.930
And now I'm simply going to create.

05:34.650 --> 05:37.770
Valuables here for the LTTE to.

05:39.410 --> 05:45.210
So I create the previous time we bring the energy to, OK, this is a different one than the energy

05:45.230 --> 05:51.740
one, and the time interval here for one is five hundred seventy nine, the time interval for 80 to

05:51.740 --> 05:55.400
let's say we want to blink at one point three.

05:55.420 --> 05:57.900
Segan, the one thousand three hundred.

05:58.130 --> 05:58.430
All right.

05:58.430 --> 06:05.300
Then you can see this would have been very, very hard to make those to link for the first one with

06:05.300 --> 06:10.040
this frequency, the second one with this frequency, just with the delay function.

06:11.060 --> 06:13.940
So I'm going to also add it to.

06:15.070 --> 06:15.590
Eight.

06:17.650 --> 06:23.890
So for each new action, you are going to add the previous time, you're going to a time interval and

06:23.890 --> 06:27.710
then for this specific case, we need to start also the state.

06:29.260 --> 06:40.270
And in the loop, so first I get the current time I do my first action and then we the same current

06:40.290 --> 06:42.460
time, I can do my signature action.

06:43.180 --> 06:45.040
So I'm going to do time, No.

06:45.040 --> 06:57.670
Minus previous time to think if this is greater than the time interval to link.

06:58.750 --> 07:07.090
Then I'm going to enter this block of code, I'm going to do if anything to eat is low with two people

07:07.090 --> 07:08.980
saying we said it.

07:17.990 --> 07:18.650
We said it's.

07:20.260 --> 07:21.560
Too low.

07:22.300 --> 07:32.860
All right, then, so this is the exact same structure, digital, right, edited to deal with Eddie

07:32.860 --> 07:34.350
to the kids.

07:34.360 --> 07:39.670
So make sure you use the labels that we have created for the energy to.

07:41.500 --> 07:47.320
Make sure you don't make a typo, because if you change the state of energy, one when you are handling

07:47.320 --> 07:52.390
energy too well, this is where you can have a serious problem and then you scratch your head trying

07:52.390 --> 07:53.320
to find what happened.

07:53.680 --> 07:57.740
And this is a common area, OK, when you copy and paste stuff.

07:57.770 --> 08:02.760
OK, so sometimes it's just better to copy and paste the structure and modify the variables.

08:02.770 --> 08:05.590
But here you can see this can be a source of Beryl's.

08:06.820 --> 08:07.630
The previous.

08:09.420 --> 08:17.420
I'm ready to think less equal time interval and need to think.

08:19.020 --> 08:23.790
I'm just going to make sure I have a meet and campaign, they're all done.

08:24.870 --> 08:27.420
So here we simply have actually one.

08:27.930 --> 08:29.250
And here we simply have.

08:30.160 --> 08:30.820
Action to.

08:32.880 --> 08:40.260
We initialize some variables here for the time we assign time into it all the action, one Brustein

08:40.290 --> 08:47.970
time interval for the action to some dates that we need for the application, initialize the edit is,

08:47.970 --> 08:50.520
of course, and then we get the time.

08:50.980 --> 08:51.310
So.

08:52.640 --> 09:00.500
The time as we get the time and then we seem to take the duration for the action one, and then we simply

09:00.500 --> 09:03.830
try to duration for the action to etc., etc..

09:03.980 --> 09:04.730
So, no, let's run.

09:04.730 --> 09:06.650
That could explode.

09:06.650 --> 09:07.100
Could.

09:09.950 --> 09:16.160
And you can see here we have the right energy, which blinks every one hundred and seventy milliseconds

09:16.640 --> 09:24.410
and the yellow energy which blinks every one point three signals, the blink rate is very different.

09:24.530 --> 09:28.090
But here it's just working perfectly.

09:29.390 --> 09:34.340
So here you have the structure that you can use for any action you want.

09:34.370 --> 09:37.680
So here the action is to make the head blink.

09:37.790 --> 09:43.760
OK, but this can be any other action, basically any action where you need to use a delay.

09:43.910 --> 09:47.060
OK, let's say you want to read data from the sun.

09:47.060 --> 09:53.630
So every five signal that you are not to use a delay of five sergant, you are going to use this structure.

09:53.990 --> 09:58.670
And now you can see we have completely removed the delay from the loop function.

09:59.090 --> 10:01.510
So, no, I have to say, don't use the delay.

10:01.520 --> 10:09.260
Actually, I'm not going to say never use delay because they are just a few specific cases where using

10:09.260 --> 10:13.800
delay is absolutely not a problem, let's say, in the setup function, let's say.

10:13.820 --> 10:18.830
So if you first you initialize the mode for the beans and then you just want to digital.

10:18.830 --> 10:19.250
Right.

10:19.580 --> 10:27.120
And what you just want to put on the pin number one, OK, for three seconds.

10:27.140 --> 10:32.930
OK, so when you start the program, you put on one for three things and then that's it.

10:32.930 --> 10:34.100
And then you enter the loop.

10:34.130 --> 10:36.560
OK, so the setup will only be executed once.

10:36.920 --> 10:41.900
So that's not really a problem if you use a delay here.

10:42.800 --> 10:43.600
Pejeta Right.

10:44.040 --> 10:47.520
That's the energy work load.

10:48.110 --> 10:52.040
So here I just want to hide for three seconds.

10:52.250 --> 10:56.510
I also want to wait for some communication to start.

10:56.520 --> 11:02.240
So let's say I'm communicating with a sensor and the sensor needs one point two seconds to start.

11:02.480 --> 11:04.960
Then I can use a delay for one point two seconds.

11:04.970 --> 11:06.760
OK, that would be more convenient.

11:07.070 --> 11:09.490
And this is because we are in the setup function.

11:09.860 --> 11:13.460
So this is like an initialization sequence.

11:13.680 --> 11:18.170
You do whatever you want and then of course you make sure you don't plug anything in the loop function

11:18.800 --> 11:20.870
and in the loop function sometimes.

11:21.710 --> 11:29.070
Well, sometimes you will need to just wait a few microseconds for a response which comes from a sensor.

11:29.360 --> 11:33.680
In that case, you can use the delay microseconds.

11:36.140 --> 11:39.880
We were a small number, for example, 10 microseconds.

11:39.920 --> 11:46.610
OK, this is a very, very small and sometimes actually that will really help you in your program to

11:46.610 --> 11:53.420
keep the program simple when you need to read data from the sensor and you just need to wait a few microseconds

11:53.420 --> 11:54.860
to get the result.

11:55.280 --> 12:01.570
So to recap, don't use delay in your code, but just you can use delay in the setup of initialization

12:01.940 --> 12:07.470
and in the loop, you can use delay microseconds with very small numbers in case you really need it.

12:08.390 --> 12:08.930
All right.

12:08.930 --> 12:14.060
And now this is time for you to practice more with the new time functionalities that you have just learned.
