This is the solution of the previous activity when you need to create an automatic light system, so
with two leads, one that you just going to pull up on under a certain threshold and the other one that
will be put on with the right, so you will modify the brightness.
So the less light, the higher the brightness.
So first of all, I'm going to define that threshold, OK?
For the engineering team to do that, I'm going to open the previous project we have created with the
photoresistor here.
So that is the code that we have just used before.
I'm just going to make sure that is plugged and to upload it.
And then using the science monitor or just let's use the sale plotter here, I'm just going to see so
this is the value currently that I have with the luminosity I have in my room.
OK, and if I put my hand, you can see it's going down.
Basically, I can see that you have 300 or 400.
So let's see, I can set a threshold at three hundred and let's say 30.
So I'm sure that if the luminosity is like it is right now, then I consider this as daylight.
And if it is lower, so if I put my hand or it's getting darker, then the energy will be put on.
So let's go back to an activity code and I'm going to create here defined let's see, luminosity.
Night.
Threshold, which will be three hundred and thirty, so that's the first value and of course, you can
add to it depending on your hardware set up and maybe I will modify it.
And when I do the experiment, that's already already by value.
And then, of course, you have to tinker that way.
So, no, I don't have my threshold.
Let's just use some other define for or to register.
Being, which is a zero, let's just initialize all the pain, so any one that's the one thing is already
the pain on 12 and define energy to being, which has been number 10.
So in this setup, I don't need to initialize the photoresistor PIN because this is an analogy in between.
I need to use spin mode for the LCD.
One thing as output and also pin code for the ability to spin as output also.
So whether I use digital right or analog right on the end it is I need to initialize them as output
is done in the loop.
So I'm going to read the view from the photoresistor ain't within it, OK, because the value we will
read will be from zero to one thousand twenty three.
So let's say Luminosity is equal to another read from the photoresistor Bill.
So I get the value and now I can check for the threshold if and know that here, so I'm not going to
add any delay and I'm not also going to use the time functionality because, well, here we can arrive
at full speed.
OK, that's not a problem.
We can use the analogy whenever we enter the void loop.
That's not going to pose any problem.
However, for example, if you want to breed, let's say you want to also print the analog read, then
maybe for printing that value.
That would be great to use the time functionality.
OK, for example, to print every one hundred milliseconds.
But here we just take the value and then we just put on some LCD.
So let's go full speed.
So if the luminosity.
Is lower than the threshold.
So here I can use strictly lower or lower or equal, that's not really important.
We are not that that's super precise with this.
Not very important.
So if it's lower than the threshold, I'm going to do digital.
Right.
Any one being high and else I'm going to do digital.
Right.
Any.
One thing we've know, so if it's getting too dark, we were on the Petitti, it is getting lighter
with power of the editing.
Right.
And now what we're going to do is to use unallowed right on the edit to people.
But before that, we need to compute the value for the peak.
So let's say it brightness is equal to.
So we're going to use the luminosity we need to divide it by four.
So let me know.
A city divided by four.
But actually, if we do that, the more luminosity, the greater the brightness.
So what do we need to do is to reverse it and to reverse it.
It's really simple.
You do two hundred fifty five minus luminosity divided by four.
And he had no need for parentheses because this has a higher priority of the subtraction.
So 255 minus the luminosity divided by four.
So if we have a high luminosity we will have low brightness and if we have a low luminosity, we will
have a high brightness.
And now let's do analogue right.
To begin with brightness and that's it.
So that's not a really long program.
So now what I'm going to do, I'm going to upload that code to verify and upload activity 19.
So he as you can see, we have the added to the green and PIN number 10, which is both on but with
not the maximum brightness and the energy, the red pin, the 12 is pulled off.
I'm going to put my hand and you can see I just put my hand here on top of the sensor and the energy
will be both on.
I remove my hands and it is put up.
OK, this is just because I have a threshold that just corresponds to this luminosity.
So it's very sensitive.
No, you can see I just put my hand a little bit close to the sensor and automatically the energy will
be put on.
So when you do that, if you see that, for example, the energy is on too quickly, what you can do
is simply you can reduce the threshold.
Or if you see that it's hard to put on the energy, what you can do is increase the threshold.
I'm going to add more light here and OK.
And you can see well, I don't know if you can see clearly, but the green energy here, the brightness
is lower.
OK, if I turn off the light now, the brightness is higher.
All right.
As you can see, this is easy to do.
And now you have a automatic light system that you can use for many different applications.