This is that number nine of the final project.
So instead we are going to allow the user to change the distance you need to between centimeters and
inches so the user will be able to toggle between the two using the remote controller.
And then what we also want to do is to make sure that we save the default unit for the next time we
restart the program.
So the default unit is centimeters and will restart its centimeters.
If the default unit is inches and then we restart, then the default unit will be inches.
And for that we are going to use EPROM.
So for this step three small thing we need to do first go the distance units using the remote controller,
then print of course, to create a unit on the LCD and save it to April.
So let's start by defining here.
I'm going to add some define.
Define distance, you need centimeter.
I'm going to use some define.
OK, let's put here the value is really not important defined distance unit and let's put in four inches.
So by doing this, you can see that if you want to add, for example, in the future another unit that
you want to print in.
Mm.
In meters, in feet, in whatever unit you can just add multiple defines you.
OK, and I'm going to create a global variable, let's put it there after is looked into this dense
unit and initialize it to distance units centimeters.
So that would be the default unit when you first start the program for the first time.
And actually one note here is that we aren't going to change the computations to inches in the program.
We keep centimeters.
We will use centimeters for all computations.
And then only then when we want to print on the LCD, we are going to toggle between the two units.
So I'm going to create now a new function.
So let's go down.
So we have a handle I command.
I'm going to create the function here.
So the other here, not that important.
Distance unit, OK?
And I'm going to if.
Distance you need is equal to.
So let's go back here to the distance, you need centimeters.
Then we say that the distance you need becomes inches and then else.
Distance you need.
Becomes it, so we just toggling between the two and note that here.
So with this, you may recognize the opportunity to use the one line, if which you may do so if you
only have two states.
And the thing is, I'm not using that here because maybe we can in the future add other units.
And also, if you add other units in the future, that may be also a good idea to change that if as
if as into a switch.
OK, but I'm going to give that if elsea because it's also convenient, basically switch and if you
are doing the exact same thing.
So I'm just toggling between the centimeter and inches when I call this.
So what I can do now is I have chosen the ECU button.
So why BQ?
Well, why not?
So when I press on the ECU button, I toggle the distance unit.
So if it was centimeters, it's going to be inches.
If it was inches is going to be centimeters and the first default value is centimeters.
Now that we have toggled distance unit, we need to make sure that we print the correct unit on the
LCD screen.
So when we print here.
This OK, and then dist. centimeters, I'm going to add it if you.
Distance unit is equal to so actually distance units centimeters.
Let's use inches here instead.
And say footprints in inches and the default so else will be the default behavior and a default will
be centimeters.
Just like centimeters in the default unit.
So that will make things simpler.
So if it's in inches, we're going to break in inches.
And so if it's in centimeters now, because we only have two units, we print the distance centimeters,
everything like we did before, quite normal.
Now, what we need to do here, the print we would need to convert the distance that we have here.
From centimeters to inches distance that we would need to multiply by something and what are we to do
here to make it easier?
I'm going to add another here, a define and call this centimeter two inches.
So just like this, for example.
And then I'm going to put the conversion factor, which is zero point three nine three seven one.
Where did I get that?
Will simply nutile centimeters, two inches in Google and you get that result.
So I just stop right here so I can just instead of hard cutting this number.
I can just do in my print the.
Function again, the distance multiplied by centimeters, two inches, and this is actually a good practice,
OK, when you need to convert between units, for example, you could have degrees and Rajan's for an
angle.
You could have sensors are finite for a temperature or here inches or centimeters or millimeters or
whatever, then just creates and defines.
OK, first you need to Sigrun you need and then you just need to multiply by these defined which makes
more sense in your program.
OK, it's much more reading.
And then we do ethnic prince.
With space and endless put in and a few other species, right, so now we can toggle the unit, we can
print a different result on the acidy depending on the unit.
And the thing is not if we restart the ALINO program and if it was inches, then when we restart, it's
going to still be the default value, which is right here.
Distance units in distance units centimeters.
So what I'm going to do now is to save.
So when we told you when we took out the distance units, I'm going to save that new distance unit into
April.
And when we start the program in the set up, I'm going to read that unit.
So I'm going to insulate it from here, include people.
All that locate and after all those designs, I'm going to add here.
Define EPROM address.
Distance unit, I'm going to create define for, let's say, 50, so we are going to use the address
50 of the EPROM to store the distance unit.
OK, so what we do in the Tuggle.
Distance unit, I'm going to do.
Is that right, we've addressed that we have so the address is here.
It's right here and distance.
OK, note that because we use EPROM, that's right, what you might want to do is to use Assael, not
print here, run the program first and see when you get to that point, OK?
If you only get to the point when you press on the button on the remote control, that's fine.
But if you have a problem and this is called very, very, very frequently and you want to check before
you're right on the problem, so I'm not going to do it.
But that's a good practice you can do before you're right with EPROM, right.
So I have saved my value when I toggle the distance unit, not in the setup, what I can do simply.
So let's do that here, for example.
It's not really important the way you do it.
I can do distance unit.
Is equal to promote trade with the onerous.
And just I'm going to make sure that even a distance unit is equal to 255, which can happen.
And the distance unit becomes.
Zero.
I'll just make sure that it is the distance in each 30 meters.
So when you read from the.
You also want to validate the data in case you have a written invalidated before he undoing the simple
logic and checking for the two hundred fifty five value.
But what you may also do is check for each dist. unit that you have that corresponds to one.
And if it doesn't correspond to any of the distance unit, you can use the default one.
OK.
So no, I think the code is complete.
So let's first verify it.
Final project, Stepanov, the nine.
OK, and as a quick recap, so.
We create toggle distance unit function that we call when we pressed the ECU button on the remote control.
In that we toggle between the two different distance units, centimeter inches that we have used.
So we have created a defined for each and the global viable to keep the distance unit once we toggle
the unit.
We also write it to the people.
Then when we print on the LCD, when we call that function within a distance, we're going to check
for the distance unit.
If this is inches, we print in inches.
If it's centimeters, we print in centimeters.
Now, let's say you restart the program.
Now, in the setup, you're going to read from the address for the distance unit and sits the distance
unit with the previous setting that you have set when you press the button in the previous frame.
All right.
So now let's upload to the original.
So now you can see the distance in centimeters, of course, that's the normal behavior we always had
before.
So 136 centimeters now going to press on the issue button here.
And so I had a low battery, so you can see that sometimes if you have low battery and the remote controller,
it may be some source of problem that the command is not correctly retrieved.
So now I have injuries.
You can see 135 centimeters correspond to about 50, 50 something inches.
OK, I can toggle between centimeters and.
And cheese, and you can see that if I it so I'm going to press the reset button.
Which will restart the program, initializing its inches again.
OK, it's not centimeters, it's inches because we have saved the unit in the memory.
All right, so everything is working now.
You can choose the distance need you want to print the distance on the LCD screen for the user, which
can be quite convenient.
And you can see here, again, that is a really good use case for using it from memory.