# How to align to a wall with Ultrasonic Sensors from LEGO Mindstorms EV3 Set (part 2)

Implementing the next logic for aligning to the border when we approach the border from the right.

• #164
• 13 Nov 2015
• 6:17

You can approach the border/wall from the right or from the left. In the previous video we finished with doing it from he left. Now we continue with extending the program to be able to approach the wall from both sides. But it is not always that easy.

### English

Previously we did a program that was aligning the robot to the border using 2 LEGO Mindstorms ultrasonic sensors and it was working like this. In this video we'll also introduce the other logic for aligning the robot when we approach from the right. How can we do this? Let's see in the program.

Using the same logic here as in the previous program won't work because here in this program, that is working only when we are approaching the border from the left, this program works, because we are waiting for the ultrasonic sensor on port 4 to detect the border first. If the border is detected by the other sensor, the sensor on port 1, this program won't work.

So I'll just remove this blocks, like this, and we'll implement another logic. The other logic goes like this. We can use a switch block. In some of the previous programs we've used switch blocks and today we are again using a switch block. The goal of this switch block is to detect which of the sensors sees the border first. We'll add a loop and in this loop we would constantly check. What are we checking. We are checking the sensor on port 4 and we get the distance in inches. Let's say 4 inches. We are comparing for value less than or equal to 4 inches. When the sensor detects a value of less than or equal to 4 inches it will execute the blocks in this part of the switch and when the value is greater than 4 inches it will execute this part of the block.

What we want to do is the moment we detect something, that is the border, we want to stop the whole robot. The first sensor on port 4 has detected something, in this case the border, that is less than or equal to 4 inches and we stop the whole robot. Let's download and see if this program works.

Position the robot, like this, run the program. The robot moves and the moment the sensor on port 4 detects the border we stop. Now we must do the same thing for the other sensor.

If, for example, the sensor on port 4 is not detecting the wall we should check weather the sensor on port 1 is detecting the wall. It is again an ultrasonic sensor, compare, distance in inches. We check if this sensor detects something less than or equal to 4 inches.

Now you see that this 2 switch blocks look kind of ugly currently and what we should do is change them to flat view, tabbed view. Now it would take less space on the screen and because of that it is more convenient to look at it. We have the following cases. Just to make sure that you can see it. If the sensor on port 4 detects the border, we stop. If the sensor on port 4 is not detecting the border, we check the one on port 1 and if the sensor on port 1 detects the border we again stop.

We run this program. Running the program again from the previous position. We run the program and the robot stops. Then we run the program from the other position and the robot stops.

What we basically have is a program that stops near the border no matter if we run from the left or from the right of the border.

Now what's left is to add the logic for aligning to the border no matter if we run from the left or from the right. It will be the same logic that we've used in the previous video but we'll continue with this logic in the next video.