This is the solution of the previous activity where you need to inform the user of the potential danger,
OK, if the ultrasonic sensor detects an object that is too close from the robot or the area.
So what I'm going to do first, I mean, it could be a. a crude and create new sketch that I'm going
to name or copy paste and save as activity.
15.
So all the kahir is already set up to correctly get the distance from the ultrasonic sensor, and we
don't block the execution of the application by using the Internet.
So I know what I'm going to do.
I'm going to first initialize the three entities.
Let's go back here.
Let's put that here.
Define any one in which is 12.
And the to the 11, so the other of the 10 is actually not that important as long as you just are consistent
with how you use the valuable then in your program.
And this so in the OK, we have to define here in the set up.
I am going to do more in any one.
In.
And I'm going to do the same for the energy, too, and for the three entities are set up and now I'm
going to.
So I'm not going to write the code here.
I'm going to do it nice with a new function that I'm going to put in void of to name dysfunction, let's
say power.
And it is from distance, OK, and this will get a perimeter double distance.
So we are going to check for the distance we receive here in centimeter.
I'm going to use a classic structure, OK, if dist..
Is greater than one hundred and it's a greater or equal.
OK, then, over on.
Great.
And if we use offensive, if the distance is greater or equal than, let's say, 15.
And the distance.
Is lower.
Then 100.
Then we Google glasses, but actually this we don't need to use it.
OK, this condition is already through here because, well, if the distance is greater equal than 100,
we go here and then if not, we go in.
And if so, when we are in the cells, if we already know that the distance is strictly lower than one
hundred.
So we don't need to check that condition.
So if the distance is greater than 100, great, if not so if the distance is lower than 100 but greater
than 15, then.
But we're on.
Yellow.
And then something else because.
It means that the distance is strictly lower than 15, so we don't need to check the condition that
we record.
That is redundant.
We just need to write the catch and put on.
Right.
So power hungry.
We are going to do digital, right.
And one in to know she's the right one, digital rights to.
In two local also yellow digital rights, any three been high.
This is the green one, I mean, to copy this and put it there.
So now if the distance so if we are in the warning mode, we want to pull up on the yellow two.
And if we are in the danger zone, we're having to pull up on any one but luck.
So we have low, low, high, low, high, low and high, low, low.
All right.
So then what do we need to do is simply to call this function?
Here, so we can print.
Let's let's do that here so we get the distance and then we call power and it is from a distance we
in the distance as a parameter here.
And then we print also the distance to remove that.
And well, that's pretty much it.
So you see, we just have added one function, OK, and now the code in the void loop is almost the
same and we didn't have to modify anything.
It still works great, OK, because we have correctly used the time functionality here to trigger the
retro sonic sensor and not block the code with delay.
And because we have also used the pulse in function correctly so we don't block anything and now we
have much more flexibility in the program.
So now what are we going to do is to upload the.
So let's verify first.
And now let's upload the code.
OK, let's open the cellular monitor and as you can see here, so we have OK, I'm going to move it.
OK, we have one hundred forty centimeter and the green is spoiled, and now I'm going to put some obstacles,
I'm going to put myself in the way, OK?
And you can see here we are at 75 centimeters and you have the yellow there is on.
You can see it.
It's blinking a little bit.
And it's because, well, you need to make sure that the scenario is quickly plug in the brain balled.
It might move a little bit, so make sure it's correctly plugged.
OK, that's one of the drawback of the breadboard is something you might have.
Some components that are not like that will make the mixture directly, like in the rainbow.
And now you can see if I put my hand very close from the sensor, you can see we have some going to
put my two hands.
You can see we have the right danger.
The obstacle is ready to close.
I'm going to go back.
It goes to yellow and then I'm going to remove every obstacle and now it goes to green.
All right, it's working, so now you have a visual indication to know if the obstacle is too close
from the sensor.