This is the solution of the previous activity where you need to get some samples from the sensorial,
photoresistor Senzo, and then compute the average of all the values that you got over a certain period
of time.
OK, so we have chosen one hundred samples and fifty milliseconds, which means that the total will
be five seconds.
So let's start and again, let's do that step by step.
So the first thing we are going to do is actually to read from the sensor and using the time functionality
to be able to read every 50 milliseconds.
So let's initialize we define to register then, which is always a zero here.
I'm going to add unsigned.
Long last time.
Let's say right, luminosity.
Which is initialise to Moelis zero, that's the same for the beginning of the program and signed long,
let's say, luminosity, sunchoke, delay and this wedding 20 to 50 milliseconds.
Great.
So now, Inglenook.
So I don't need to initialize the pin again, this is an analog input pin in the loop.
What I am going to do is assign a long time now is equal to Moelis.
Right.
And then so this should be quite easy for, you know, with all the practice that we got, if the time
NUPE minus the last time we have.
Right.
The luminosity is greater than the simple dillehay we enter this.
If we do.
Last time we read the luminosity, we add the delay.
So the next time we enter this, if will be 50 milliseconds after that and then we can read we've analog.
So read the new front sensor and we are going to read that value and put this inside an array.
So that's step two here is we're going to create the right to store the different samples because we
just don't want to read the value and do something with it.
We want to be able to store a few values to here one hundred values over time.
So I'm going to create an array.
It's created here.
So an array of integer value.
OK, because we are only going to get values between zero and one thousand twenty three.
Let's say liminality simple.
Impulsiveness, look, I could name it luminosities sample list, luminosities sample array, also,
what you can do is simply add an S, make sure that when you name your array as easy to understand that
this is a list or at least it's plural and the size of it will be one hundred.
But again, I'm not going to have to code one hundred.
I'm going to create define here, let's say luminosity.
Sample.
Sighs One hundred, this is here and then I'm going to initialize all of the values in Detroit to zero,
and to do that, you can simply use Gary Braces and one zero.
You don't need to repeat the zero hundred time, OK?
If you just do that it with initialize all the values to zero, which is very practical and very efficient.
So.
Right, our luminosity sample contained one hundred samples which are all equal to zero.
So what you need to do now is actually to put each value we rate inside this.
All right.
And of course we need to create a counter int.
That's the index can't start at zero because first we are going to use the index zero and an index one,
two, three, etc., until we reach ninety nine, which is the last index.
And after that we are going to compute the average and go back to zero again.
So let's start with an index come to initialised to zero.
So what we do know we are going to put the value we raise from the sensor to the array.
So luminosity.
Samples with the index counter.
So this will be equal to analog grade from the photoresistor bin.
All right, and then what we do is simply index conter plus plus so we start at zero.
We put the first value, we read in the index zero, then we wait 15 milliseconds.
We come back here.
So now the index is one.
So we put the value one, two, three, etc..
And now what we need to do is, of course, to check for the end of the story, because once the content
reaches ninety nine, we have the last value.
And when the countdown reaches one hundred and we get here, we don't want to do luminosities samples
with one hundred because this is outside of the array.
So what we are going to do now is before we do this, we are going to check the end condition.
So if index counter is equal to luminosity sample size.
So once we have reached one hundred in this case, what we can do is simply reset the counter.
So index counter is zero, which means that the next.
So just after this, we are going to start again to feel the error rate from zero and then one, two,
three, etc..
And then, of course, at this point, the rate is filled from zero to ninety nine for all the indexes.
We have some values that we can analyze.
So what we are going to do now is to compute and print average.
We just compute in the average and then we start again.
And so we replace all the previous values one by one theory until we reach one hundred and then we compute
and print the average again, etc., etc..
So to do this, I am going to create a new function here that see into compute.
Average luminosity.
OK, so it's because I'm going to return the average as an int, but you could use double if you want
more precision, but it is quite good for what we want here.
So I'm going to go through all the values of the orange.
So classic if it is zero and then I lower than the size of the array.
Strachey lower I plus.
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
a long because into the max value of it is thirty two thousand something.
So here, because we have a lot of values, well, it's quite probable that if we add all the values
together, we are going to go beyond the maximum of the.
So I'm going to use long for the.
And what I'm going to simply sum plus of course initially the sum to zero sum plus equals.
So we add to the sum.
The sample that we have in the three, just so we go through all the values in the area and we just
added them to the same mission to zero.
So at the end, this will contain the sum of all the samples in the area.
And then we simply do return some divided by the luminosity sample size.
Which here is one hundred.
So we add all the elements and then we divide the sum by one hundred, which gives us the average of
all the values.
And this is also written as an e-book, because now the average will be between zero and one thousand
twenty three.
All right, so we're dysfunctional.
We get them right now.
What I'm going to do is create another function, print, average luminosity.
I create another function so I can make a nice prenatal care with lots of details and then I'm going
to call that function Freundel.
So here, let's get so let's do it.
Other age is equal to compute average luminosity.
So I called dysfunction inside dysfunction, and then I will call dysfunction inside the loop.
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.
Biggin.
OK, that's all we need, Indesit.
And now I'm going to do that, so let's do something nice print.
Of the range luminosity.
MySpace and then on the same line, say I, the print.
That's but the number of simple.
And then let's say that print samples based here.
They can avory.
And this childhood friend, the luminosities sample delay.
OK, so not great.
Not a single print with nearly Saigon's.
And finally, you say on that print and then we the average it.
So this way, we just don't train the average, we print some information.
OK.
That gives us more useful information actually, because when we see the average on the same, we will
see the average luminosity for one hundred samples taken every 50 milliseconds.
Is that when we finally find this value on this value, the behavior of the problem will change.
And also this will give us more useful information.
All right.
So now that I have this function and going to call it right here, print other and remove all of that.
So when we have reached the maximum index plus one of the array, which is the precise reason, reset
the counter to continue to rate values and we also print the average luminosity by computing the average
here and then printing.
Nice message for the user.
Just one thing I'm going to add is after we increase the control and we read some value, I'm just going
to add that print.
I'm just going to add a dot so we can kind of see a progression line.
OK, because if I don't add anything, we will not have any output and maybe you don't know what is
happening.
OK, so here, every time we see a dot, we know that we have.
Right.
And added a new value inside this sample array.
And because I use print here, what I'm going to do also is when a computer and print the average,
I'm going to use the print and then.
Just an empty print, Alan, but just to make sure that I go back to a new life.
OK, so we better understand this when I actually run to code.
So it seems good.
Let's first just compile it as activity 20 to see if we have any computation error.
This seems to be good.
So now I am going to upload the code and open the monitor so you can see we have a progression by here.
Quite nice.
And then OK, we have some message, average luminosity for one hundred samples.
I'm going to stop this so they can every 15 minutes we have 341 here.
We have three hundred twenty nine k you can see the average, we get every fight.
Now let's say you put my hand on top of the senseor and you can see the average will be lower.
We have average of sixty five.
All right.
So now you have an efficient way, so this actually this code is kind of a template that you can use
to measure the average of any sense.
So you have to keep this code close to you because you are probably going to need it in the future,
whether it's a photoresistor sensor or a distance sensor, any kind of sensor.
And that is the end of this activity.
And that's also the end of the section on the photoresistor sensor.