WEBVTT

00:00.180 --> 00:06.590
This is the solution of the previous activity where you need to get some samples from the sensorial,

00:06.600 --> 00:13.380
photoresistor Senzo, and then compute the average of all the values that you got over a certain period

00:13.380 --> 00:13.950
of time.

00:14.230 --> 00:20.070
OK, so we have chosen one hundred samples and fifty milliseconds, which means that the total will

00:20.070 --> 00:21.370
be five seconds.

00:21.600 --> 00:25.840
So let's start and again, let's do that step by step.

00:25.860 --> 00:32.820
So the first thing we are going to do is actually to read from the sensor and using the time functionality

00:32.820 --> 00:35.840
to be able to read every 50 milliseconds.

00:35.850 --> 00:46.010
So let's initialize we define to register then, which is always a zero here.

00:46.740 --> 00:48.540
I'm going to add unsigned.

00:49.850 --> 00:52.910
Long last time.

00:53.880 --> 00:55.950
Let's say right, luminosity.

00:57.930 --> 01:04.890
Which is initialise to Moelis zero, that's the same for the beginning of the program and signed long,

01:05.640 --> 01:15.230
let's say, luminosity, sunchoke, delay and this wedding 20 to 50 milliseconds.

01:15.570 --> 01:15.880
Great.

01:15.900 --> 01:17.040
So now, Inglenook.

01:19.340 --> 01:24.240
So I don't need to initialize the pin again, this is an analog input pin in the loop.

01:24.260 --> 01:31.640
What I am going to do is assign a long time now is equal to Moelis.

01:32.480 --> 01:33.020
Right.

01:33.020 --> 01:40.280
And then so this should be quite easy for, you know, with all the practice that we got, if the time

01:40.280 --> 01:42.830
NUPE minus the last time we have.

01:42.830 --> 01:43.090
Right.

01:43.100 --> 01:49.190
The luminosity is greater than the simple dillehay we enter this.

01:49.190 --> 01:50.660
If we do.

01:50.660 --> 01:55.590
Last time we read the luminosity, we add the delay.

01:57.020 --> 02:06.080
So the next time we enter this, if will be 50 milliseconds after that and then we can read we've analog.

02:06.260 --> 02:15.630
So read the new front sensor and we are going to read that value and put this inside an array.

02:15.950 --> 02:22.580
So that's step two here is we're going to create the right to store the different samples because we

02:22.580 --> 02:25.230
just don't want to read the value and do something with it.

02:25.580 --> 02:31.100
We want to be able to store a few values to here one hundred values over time.

02:31.550 --> 02:33.250
So I'm going to create an array.

02:33.800 --> 02:35.030
It's created here.

02:36.590 --> 02:39.270
So an array of integer value.

02:39.290 --> 02:44.650
OK, because we are only going to get values between zero and one thousand twenty three.

02:45.620 --> 02:47.390
Let's say liminality simple.

02:49.590 --> 02:58.500
Impulsiveness, look, I could name it luminosities sample list, luminosities sample array, also,

02:58.500 --> 03:06.540
what you can do is simply add an S, make sure that when you name your array as easy to understand that

03:06.540 --> 03:11.740
this is a list or at least it's plural and the size of it will be one hundred.

03:11.760 --> 03:14.600
But again, I'm not going to have to code one hundred.

03:14.610 --> 03:19.050
I'm going to create define here, let's say luminosity.

03:21.880 --> 03:22.540
Sample.

03:24.250 --> 03:34.420
Sighs One hundred, this is here and then I'm going to initialize all of the values in Detroit to zero,

03:34.630 --> 03:40.340
and to do that, you can simply use Gary Braces and one zero.

03:40.780 --> 03:43.320
You don't need to repeat the zero hundred time, OK?

03:43.330 --> 03:49.520
If you just do that it with initialize all the values to zero, which is very practical and very efficient.

03:49.660 --> 03:49.870
So.

03:49.960 --> 03:55.340
Right, our luminosity sample contained one hundred samples which are all equal to zero.

03:55.360 --> 04:01.030
So what you need to do now is actually to put each value we rate inside this.

04:01.190 --> 04:01.530
All right.

04:01.840 --> 04:06.150
And of course we need to create a counter int.

04:07.270 --> 04:16.570
That's the index can't start at zero because first we are going to use the index zero and an index one,

04:16.570 --> 04:21.670
two, three, etc., until we reach ninety nine, which is the last index.

04:21.920 --> 04:25.880
And after that we are going to compute the average and go back to zero again.

04:26.230 --> 04:29.710
So let's start with an index come to initialised to zero.

04:30.160 --> 04:35.040
So what we do know we are going to put the value we raise from the sensor to the array.

04:35.050 --> 04:36.130
So luminosity.

04:38.360 --> 04:42.260
Samples with the index counter.

04:44.870 --> 04:51.260
So this will be equal to analog grade from the photoresistor bin.

04:53.170 --> 05:00.930
All right, and then what we do is simply index conter plus plus so we start at zero.

05:00.940 --> 05:06.970
We put the first value, we read in the index zero, then we wait 15 milliseconds.

05:06.970 --> 05:07.780
We come back here.

05:08.050 --> 05:09.800
So now the index is one.

05:10.120 --> 05:13.090
So we put the value one, two, three, etc..

05:13.900 --> 05:22.810
And now what we need to do is, of course, to check for the end of the story, because once the content

05:22.900 --> 05:26.070
reaches ninety nine, we have the last value.

05:26.260 --> 05:32.410
And when the countdown reaches one hundred and we get here, we don't want to do luminosities samples

05:32.410 --> 05:35.180
with one hundred because this is outside of the array.

05:36.010 --> 05:41.920
So what we are going to do now is before we do this, we are going to check the end condition.

05:42.580 --> 05:50.290
So if index counter is equal to luminosity sample size.

05:51.840 --> 05:59.550
So once we have reached one hundred in this case, what we can do is simply reset the counter.

06:02.250 --> 06:05.260
So index counter is zero, which means that the next.

06:05.280 --> 06:12.120
So just after this, we are going to start again to feel the error rate from zero and then one, two,

06:12.120 --> 06:13.050
three, etc..

06:13.620 --> 06:19.730
And then, of course, at this point, the rate is filled from zero to ninety nine for all the indexes.

06:19.740 --> 06:22.720
We have some values that we can analyze.

06:22.980 --> 06:29.400
So what we are going to do now is to compute and print average.

06:31.380 --> 06:34.610
We just compute in the average and then we start again.

06:34.620 --> 06:41.490
And so we replace all the previous values one by one theory until we reach one hundred and then we compute

06:41.490 --> 06:44.700
and print the average again, etc., etc..

06:44.850 --> 06:53.850
So to do this, I am going to create a new function here that see into compute.

06:55.210 --> 06:57.730
Average luminosity.

07:00.410 --> 07:07.100
OK, so it's because I'm going to return the average as an int, but you could use double if you want

07:07.100 --> 07:10.440
more precision, but it is quite good for what we want here.

07:10.880 --> 07:15.200
So I'm going to go through all the values of the orange.

07:15.200 --> 07:22.460
So classic if it is zero and then I lower than the size of the array.

07:22.940 --> 07:24.780
Strachey lower I plus.

07:30.250 --> 07:38.440
And what I'm going to do is so I'm going to create a viable sun, which actually I'm going to put as

07:38.440 --> 07:44.420
a long because into the max value of it is thirty two thousand something.

07:44.960 --> 07:50.320
So here, because we have a lot of values, well, it's quite probable that if we add all the values

07:50.320 --> 07:54.330
together, we are going to go beyond the maximum of the.

07:54.550 --> 07:56.230
So I'm going to use long for the.

07:56.980 --> 08:04.650
And what I'm going to simply sum plus of course initially the sum to zero sum plus equals.

08:04.660 --> 08:06.190
So we add to the sum.

08:08.840 --> 08:17.000
The sample that we have in the three, just so we go through all the values in the area and we just

08:17.000 --> 08:19.690
added them to the same mission to zero.

08:19.710 --> 08:24.340
So at the end, this will contain the sum of all the samples in the area.

08:25.010 --> 08:32.540
And then we simply do return some divided by the luminosity sample size.

08:33.980 --> 08:35.420
Which here is one hundred.

08:35.900 --> 08:44.030
So we add all the elements and then we divide the sum by one hundred, which gives us the average of

08:44.030 --> 08:45.080
all the values.

08:46.060 --> 08:53.380
And this is also written as an e-book, because now the average will be between zero and one thousand

08:53.380 --> 08:53.980
twenty three.

08:54.220 --> 08:55.690
All right, so we're dysfunctional.

08:55.750 --> 08:57.640
We get them right now.

08:57.640 --> 09:03.100
What I'm going to do is create another function, print, average luminosity.

09:05.960 --> 09:11.360
I create another function so I can make a nice prenatal care with lots of details and then I'm going

09:11.360 --> 09:13.340
to call that function Freundel.

09:14.390 --> 09:16.520
So here, let's get so let's do it.

09:16.550 --> 09:20.390
Other age is equal to compute average luminosity.

09:22.230 --> 09:27.740
So I called dysfunction inside dysfunction, and then I will call dysfunction inside the loop.

09:29.650 --> 09:36.940
So what I didn't do, Sam, so if you sell in the setup where you just sit up here, I need to do that.

09:38.400 --> 09:39.060
Biggin.

09:42.070 --> 09:44.230
OK, that's all we need, Indesit.

09:45.490 --> 09:50.530
And now I'm going to do that, so let's do something nice print.

09:52.250 --> 09:54.290
Of the range luminosity.

09:57.730 --> 10:01.200
MySpace and then on the same line, say I, the print.

10:02.450 --> 10:03.900
That's but the number of simple.

10:06.910 --> 10:13.150
And then let's say that print samples based here.

10:15.610 --> 10:16.870
They can avory.

10:19.450 --> 10:24.550
And this childhood friend, the luminosities sample delay.

10:26.740 --> 10:29.330
OK, so not great.

10:30.590 --> 10:35.750
Not a single print with nearly Saigon's.

10:37.730 --> 10:44.940
And finally, you say on that print and then we the average it.

10:46.720 --> 10:51.110
So this way, we just don't train the average, we print some information.

10:51.130 --> 10:51.350
OK.

10:51.370 --> 10:58.080
That gives us more useful information actually, because when we see the average on the same, we will

10:58.090 --> 11:03.160
see the average luminosity for one hundred samples taken every 50 milliseconds.

11:03.160 --> 11:09.990
Is that when we finally find this value on this value, the behavior of the problem will change.

11:10.000 --> 11:13.630
And also this will give us more useful information.

11:14.020 --> 11:14.370
All right.

11:14.380 --> 11:22.780
So now that I have this function and going to call it right here, print other and remove all of that.

11:23.990 --> 11:34.160
So when we have reached the maximum index plus one of the array, which is the precise reason, reset

11:34.370 --> 11:41.990
the counter to continue to rate values and we also print the average luminosity by computing the average

11:41.990 --> 11:43.120
here and then printing.

11:43.130 --> 11:44.880
Nice message for the user.

11:45.950 --> 11:53.210
Just one thing I'm going to add is after we increase the control and we read some value, I'm just going

11:53.210 --> 11:56.060
to add that print.

11:56.150 --> 12:01.700
I'm just going to add a dot so we can kind of see a progression line.

12:01.740 --> 12:07.670
OK, because if I don't add anything, we will not have any output and maybe you don't know what is

12:07.670 --> 12:08.120
happening.

12:08.150 --> 12:12.530
OK, so here, every time we see a dot, we know that we have.

12:12.530 --> 12:12.830
Right.

12:12.830 --> 12:16.170
And added a new value inside this sample array.

12:16.370 --> 12:22.700
And because I use print here, what I'm going to do also is when a computer and print the average,

12:22.700 --> 12:25.310
I'm going to use the print and then.

12:26.560 --> 12:30.130
Just an empty print, Alan, but just to make sure that I go back to a new life.

12:30.160 --> 12:33.940
OK, so we better understand this when I actually run to code.

12:34.270 --> 12:35.800
So it seems good.

12:36.310 --> 12:43.720
Let's first just compile it as activity 20 to see if we have any computation error.

12:44.890 --> 12:46.980
This seems to be good.

12:46.990 --> 12:57.910
So now I am going to upload the code and open the monitor so you can see we have a progression by here.

12:58.360 --> 12:58.960
Quite nice.

12:59.890 --> 13:05.230
And then OK, we have some message, average luminosity for one hundred samples.

13:05.690 --> 13:11.950
I'm going to stop this so they can every 15 minutes we have 341 here.

13:11.950 --> 13:17.080
We have three hundred twenty nine k you can see the average, we get every fight.

13:17.710 --> 13:24.420
Now let's say you put my hand on top of the senseor and you can see the average will be lower.

13:25.000 --> 13:26.840
We have average of sixty five.

13:27.610 --> 13:28.180
All right.

13:29.990 --> 13:36.170
So now you have an efficient way, so this actually this code is kind of a template that you can use

13:36.410 --> 13:39.150
to measure the average of any sense.

13:39.150 --> 13:46.010
So you have to keep this code close to you because you are probably going to need it in the future,

13:46.340 --> 13:51.790
whether it's a photoresistor sensor or a distance sensor, any kind of sensor.

13:52.370 --> 13:54.760
And that is the end of this activity.

13:55.010 --> 13:59.510
And that's also the end of the section on the photoresistor sensor.
