This is the solution of the previous activity where you need to bring the distance of the ultrasonics
and Suso the distance measured by the sensor in the LCD display screen.
So what are you going to do?
I'm not going to start from scratch here.
I recommend you to do it so you can practice more.
I'm going to take the code for the ultrasonics, and so I'm going to take the code for the LCD screen
and then add the functionality.
So I'm going to open the activity, will hear the activity.
15 in this activity.
We were using the ultrasonic sensor with some LCD.
OK, we are not going to use HD here, so I'm just going to take what I need here.
So I have to define for the eco and the trigger pins and have all of that that I'm going to use.
OK, so to set the time for the trigger, to set time for the pulse in the distance, the previous distance
for the filter that we use.
And then to trigger ultrasonics and so is the same function, I just think these douget, ultrasonic
dist., is also the same.
OK.
They go down, then we have the echo interrupt, which is the same I don't mean this poor elitist from
distance because we are not going to use them in an indoor setup.
So in the setup, I have my Selvig in.
I have the PIN code for echo and trigger pins.
I don't need to mode for the ads, and I need also this interrupt.
That's Bunnett here.
And then in the void loop, where what do I do, I am just going to take everything.
OK, and just remove this, we don't need to pull this from a distance, so that is the code to actually
trigger the ultrasonic sensor every 16 milliseconds.
And then we have to interrupt to handle the measurement of the distance and then we get the distance
in our code directly in here.
We just print it on the same monitor.
Actually, I'm going to run most of sales because we are not going to use cell in this activity.
But it can be good for you to use it when you're actually writing this program to test the program and
see if things are working correctly here.
I suppose that it's going to work because it was working for this activity.
So I think I got everything I need.
Just verify the sketch work activity.
17.
Let's see if we have an error, good.
I'm going to open the activity.
16.
In which we have used the LCD screen and I'm going to initialize it here, so I'm going to include the
library.
And I'm going to do the defiance of the other for the defying doesn't matter.
OK, then NCDC going to also take the course online here.
So let's put that here.
OK, the other here doesn't really matter.
You have all your designs and then all the initialization of your global variables.
And then we have the different functions, and so we don't need to use a text on display.
OK, why do we need to do now is to set up the LCD we LCD Biggin.
And I'm going to do that just here in the city.
And that's pretty much.
So now we have the reading of the ultrasonics, and so every 60 milliseconds, every whatever we put
in is viable and the screen is initialized.
So what we can do is go back to this void.
Look, here we get a new distance if new distance available.
So we said deflect false and we get the ultrasonic distance.
So we will get a distance in centimeters.
And here what we want to do is to print the distance on the LCD screen.
And we also want first to print the rights of a frequently we receive the data.
OK, so let's suppose that this right here is not going to change in the code.
So what I'm going to do is here in the setup density print.
So I'm going to the LCD sets Kissack with zero and zero, which actually I don't need to do.
But just so it's clearer in the code and I'm going to print reads.
And then Anthony screens, I'm going to take the view here, which is ultrasonic trigger delay and put
it here and then I'm going to actually print.
We've space and and as for this.
So the first line we have the right and as you can see, if you print, print and print everything,
the cursor will be moved.
OK, if I don't, said the cursor, every time I bring the cursor, we automatically be moved.
Let's just upload this to be sure that the LCD working and that we have the right print in here.
And OK, we see right, 60 and 60 milliseconds, so if we ever change it, that then when we start the
problem again, we are going to have a different view here.
Great.
Now let's create a new function here, void brains, distance on LCD and double distance.
So in this function, we are simply well, this function is pretty self-explanatory.
We are going to take this and printed on the.
So what I'm going to do, I'm going to do first LCD sit just so zero and then one.
OK, the first column, the second line.
And I'm going to bring as print.
These tents like this.
And as brain, so that with brains after, so the cursor will be here and not here, brain distance.
OK, let's check here.
Actually, the length of the train.
OK, we have one, two, three, four, five, six, seven, eight, nine, 10.
We already have ten characters for this.
So let's say we have so we have 10 here.
Let's say now we have the maximum, which is four hundred, you will have this.
So 10, 11, 12, 13, 14, 15, 16, so that's the max value.
I mean, we've in the hundreds sort of three numbers here, we not take more than 16 characters now,
let's say with just four centimeters.
Well, the thing is that we we need to add two spaces to this.
Why is that?
Because simply this string will be shorter than the previous one.
Let's say you go from four hundred to four.
So if you bring a string shorter than the previous one, we have seen in the previous activity that
it's not going to replace all the nine, but only the characters here.
So you may have some weird characters or some additional characters at the end of the line.
So what we can do here?
So different choices.
The easiest one is entity print and add just.
Two spaces, OK, which means that for this case is going to be at 16 characters and for this case it's
going to be at 18 characters, but if you tried to bring out 18 characters, simply the first 16 would
be printed.
So that's not a problem.
All right.
The next solution will be just to create string objects, to put this into the string and then to do
a follow up on the string to check if you need to add or not spaces.
But here, it's really easy to do that, and that's not going to make any problem in the project.
All right.
So this function is actually pretty simple.
We said the kuso and then we print some text.
Now, let's call this in here.
With distance, and that's pretty much OK, we have the function that we call here, whenever we get
the distance, we update the LCD display screen and let's upload the code to see if it works.
So you can see on the first line, we have great 60 milliseconds and on the second line we have the
distance in centimeter that is updated every so 60 milliseconds.
So not it's one hundred thirty eight, which is the wall behind me.
I'm going to put my hand here and you can see it's going to be at 11 centimeters.
I go closer.
You can see my hand on the screen.
We are at here six or seven centimeters.
OK, so it's walking.
And no, you don't need to use the same monitor or any daybook tool from the original.
You can directly get the feedback from the sensor on down in the world.
So that's actually the first point where you can get such a feedback without doing anything with your
computer.
So now you could just, for example, power on your in the world.
The code will be running on the ordinary both directly and you can just see the distance on the LCD
screen.
No need to open, for example.
All right.
And that is the end of this activity.