This is the solution of the previous activity where you have to make an entity fade in, fade out from
directly from the potential meter, and on top of that, you add a cap value that you set from the sale.
And when you set the cover, you from the sale, you also save to it.
So the next time you start the program, you keep the same default setting.
So we have a few different things to do.
Let's go step by step and let's choose the first thing to do so we can directly validate what we are
doing with the memory.
So I'm going to do the sale code.
So first sale begin.
Right, and in the loop, I am going to do if.
Take a look at the label.
Is your return zero?
So if we have data that was received in decision and going to the end data is serial that pass it.
All right.
So I get an integer, OK?
Because this is what we expect.
I get an integer.
And I straight into this volume, though.
Of course, what I'm going to do is also say that that.
Time out with 10 milliseconds so we don't wait too long for this.
OK, so if you don't understand everything here, please go back to the cellular section to practice
more and which the clinicians.
All right.
And now this data before I do anything, I'm going to validate the data.
Whenever you receive something from an external source, you want to validate the data because you don't
know.
Maybe I could send the value.
Ten thousand, OK, if I send 10000.
Well, the thing is that these data, this value is going to be used for the analogue right.
Function.
So we expect a value that is the data is greater or equal than zero and the data is lower.
So let's say strictly lower than 256, which means 255 just for readability.
And this.
And OK, now we have validated the data.
So what I'm going to do is say that print and then data, so I'm just going to validate that when we
are at this point, then I will be able to do it from that.
Right, Kate?
Because we are in the loop.
So I don't want to do it from that.
Right.
Somewhere where I'm not sure how often it will be updated.
But no, let's run that first program.
Something to see if it is activity.
13.
OK, let's open the sale monitor.
Which has nothing for now, and we seen the value, let's say twenty three, I get twenty three and
say I think zero zero.
Two hundred fifty five.
Yes, that's the minimum.
The maximum.
Let's try minus one.
OK, we have nothing.
Let's try 256.
We have nothing in any other value above two hundred fifty six.
We would have nothing.
So we only write it, we sell the print and then when we have received the data and the data is valid
ok.
So we know we can do it from your right, OK, and remove Sayle that print, Ellen.
Here we are sure we are not going to rate it at full speed, and I did that to verify the code, actually,
because when you send some code, maybe it's competing correctly, but maybe you have made some little
mistake and then the behavior is not what you would expect.
So by doing this, you are sure the behavior is correct, so it's not right.
So I'm going to write an address, let's say altoist 350.
I'm going to write the data and it's better not to just how it could be, otherwise what I can do is
create a define here.
So define EPROM and race.
Max, let's see, brightness or intensity of the same thing and let's say three hundred fifty, OK,
and I'm going to put that here.
So the name is quite long but quite explicit.
And of course, because I use EPROM, I need to do include.
EPROM notes H.
All right, so now we have commercialized the cell communication and whenever we send some data, so
some number to the from the computer, we're going to validate that data and write it in to the.
So let us handle the energy and the potential to bring all that.
Well, let's hear Define Energy Bill, which is AFSA 10.
Define what lunchmeat.
In which is a two and in the void, he said, I'm going to do.
Incommode.
In.
But I do need to set a pin mode for the put on the pin because as default, this is an input pin and
then in the loop.
So we have our action one which is checking the sale.
So this is going very fast.
And then I can do my sergant action and I'm going to read the value from the put on Schmitter and also
because the value from the potentiator is from zero to one thousand twenty three, I would need to divide
this value by four.
But we can have a value between zero and two hundred and fifty five for the analogue.
Right.
Function them directly going to the bait and the ripeness is equal to analog read on the potential to
be.
It divided by four.
I read this to give the value between zero and one thousand twenty three, I divide by four and then
I put this directly into the brightness viable, which is a bit dated.
OK, but instead of just putting this inside a viable and then completing this, I just put everything
together and then we have analogue.
Right.
And then with the added brightness.
But the thing we want to cap the volume so we will need to have somewhere a max brightness volume.
OK, I'm going to create here, right, max brightness.
So we have the available not initialized yet, but I'm going to use it here.
So if ready.
Right now, this is.
Greater than the max.
Brightness, what we are going to do is to see that the energy.
So the brightness is equal to the max brightness.
And this is how you can cut a volume.
OK, you check if the value is greater than the maximum.
If yes, then you just say that the value becomes the maximum.
So this can be very useful in many different applications.
So what do we do here is we check the brightness from the bottom and then we make sure it's not greater
than the maximum or else we change it and then we can give it as a parameter for our function.
And now let's actually see how to set this value here.
So first here, when we actually do this, when we write to the memory, we can also do Max.
Rottenness is the data, so the validated data.
OK.
Once we validate the data, we save it into the EPROM, but we also set it in our code so that the value
we send to the sale will be used here.
So that is during the execution of the program.
But now how to initialize the valuable when the program starts?
Well.
So we are going to initialize it here.
Max.
Brightness.
Is equal to EPROM.
Read from this.
Otherwise, OK, so by using a defined here, I can use the name here of the constant here and here,
so if I want to change the address, I just need to change it here and I can be sure that it would not
affect the program.
OK, so here we have negative brightness.
We said it directly first from the employment rates or the value that was stored previously and then
during the program execution.
We can update this at any time.
So we updated in the code and we also added eat into it from memory.
So the next time we start the program again with this line, we are going to get the last value that
we have set here with this and pass it and note that we need to also handle the case if we haven't written
in the April yet.
OK, so far we haven't written in the three hundred fifty address.
So what we are going to write here, maybe two hundred fifty five are also maybe zero and we don't want
the next brightness of zero to start the program because this means here if it's zero.
Well the analog rate we just sent is zero and LG will not be put on.
What I'm going to do is check if.
Mike's Bruckner's.
Is equal to equal to zero.
Then I'm going to do Max.
Bruckner's I'm going to set a default value, let's see, two hundred fifty five and instead of cutting
it, I'm going to do you define.
Brightness.
Default, and it's a two hundred fifty five and let's put that here.
OK, so this way we can change all the parameters directly from those two defined.
All right.
So for the first run, if what we read from the EPROM zero, then we directly change it, but we don't
have zero as a default value and then everything will restart the program.
If we have set a new value here, we're going to the new value.
All right.
And now let's upload the program.
So as you can see here, this is on, but not at the max intensity, so this is just because it was
in the middle of the potential meter.
So I turn it.
Off I go to the minimum and then I turn it on, you can see it goes to the maximum.
Know what I'm going to do?
I'm going to open the cell monitor.
So this will restart the program, as you may know, and I'm going to set a different view, so let's
say I send 60, I send 60.
You can see the brightness is lower than before, OK, because the max brightness is 60, OK, they
can go down to zero and then they can go up, but it will cap at 60.
OK, if I put, let's say, 200.
It the brightness has just increased.
I can go up to two hundred or so as low as zero, up to 200.
Let's put a little bit at the brightness.
Not maximum brightness is eight.
So from zero eight in any value after eight, we just stay at 80.
OK, so I can tend to put on shmita.
The brightness will not increase after a certain point.
And no, let's restart the program.
I'm going to press on this reset button here.
The program has restarted and oh, well, the max intensity is still the same as before.
OK, it is 80 now, let's say, but two hundred forty.
OK, much brighter.
I can.
Or is it the program and it's still 240, OK, can go down and can go up, it's tribal for a very small
value, 20, OK, so the max is 20, I can go to zero, etc., but it's going to zero.
Let's restart the program.
And now let's go up and I can only go up to 20 in the morning.
All right, and that's the end of the security as well as the section on April.
So the important thing is make sure you don't write at full speed.
OK, but if you correctly test your program, you should be fine.
And as you can see, that's a quite powerful way of setting a default value and keeping this default
value for multiple runs of your program.