# Coding the V5 Distance Sensor using Math Inequalities

Using the V5 Distance Sensor on your robot during a VEX Robotics Competition (VRC) match can allow your robot to score more points using autonomous movements.

## The V5 Distance Sensor

The V5 Distance Sensor is one of the powerful V5 sensors which are designed for complete integration with the V5 robotics platform.

• This sensor uses a pulse of classroom-safe laser light to measure the distance from the front of the sensor to an object.
• The V5 Distance Sensor can be used to detect an object and determine the relative size of the object. The approximate size of an object is reported as small, medium, or large.
• The sensor can also be used to calculate a robot’s approach speed. Approach speed measures the speed of the robot/sensor as it moves toward the object.

For more detailed information on the V5 Distance Sensor, how it works, and how to use it with VEXcode V5, view this article from the VEX Library.

## V5 Distance Sensor Example

The goal of this project is to have the robot pick up and move a Mobile Goal using the V5 Distance Sensor to detect how far away the goal is.

This is the code example that will be covered in this article.

A breakdown of the behaviors in the example, and how to obtain the parameters used to code the V5 Distance Sensor, will be covered.

You can build the project in VEXcode V5 as you follow along with the article, or you can just read the article for inspiration as an example of how to include the V5 Distance Sensor when you code your own custom robot.

This video shows the code above being executed and the robot picking up and moving a Mobile Goal.

Here is the Robot Configuration used in VEXcode V5 to add the robot and the Distance Sensor to the configuration for this example, if you’d like to try the example yourself.

### Robot used for this example

The robot used in this example is the 2021-2022 VRC Hero Bot, Moby. Moby’s design does not have any sensors, the V5 Distance Sensor was added to Moby for this example.

For this example, the V5 Distance Sensor is mounted near the center of Moby’s Forks.

You do not have to use Moby to code the V5 Distance Sensor or to compete in this year’s competition. You can mount the sensor on your custom robot wherever you see fit.

Note: ensure no structure on the robot is in front of the small laser window on the face of the sensor. There needs to be a clear path in front of the sensor between the goal and the sensor.

### Breakdown of the behaviors to score

To pick up and move a Mobile Goal using feedback from the V5 Distance Sensor, first break down how the robot should move.

First, the robot should be placed to face the Mobile Goal.

Then, the robot should drive forward until the Distance Sensor detects that the Mobile Goal is inside the Forks and close to the sensor.

Once the sensor detects that the Mobile Goal is inside the Forks or close to the sensor, the robot stops driving and raises the Forks to pick up the Mobile Goal.

Once the Mobile Goal has been picked up, the robot will turn left for 90 degrees and drive forward for 600 millimeters (mm).

The robot will then lower the Forks to place the Mobile Goal down, and drive in reverse away from the Mobile Goal so that it does not accidentally knock it over during the next move.

## Understanding parameters

In order to execute the above behaviors, it is important to understand how far away the Mobile Goal is from the V5 Distance Sensor.

### Parameter to pick up the Mobile Goal using the V5 Distance Sensor

In order for the robot to drive forward until the Distance Sensor detects that the Mobile Goal is inside the Forks and close to the sensor, the values from the sensor need to be recorded from the V5 Brain.

Place the Mobile Goal in between the robot’s Forks.

Then, from the Brain’s screen, select ‘Devices’ and then the icon for the Distance Sensor.

The distance in millimeters (mm) that the Mobile Goal is away from the sensor is reported on the Brain’s screen.

This is the value that will be used in the project when picking up the Mobile Goal using the Distance Sensor.

### Parameter to raise and lower the Forks

Place the Mobile Goal inside the Forks again.

Then, from the Brain’s screen, select ‘Devices’ and then the icon for Motor 2. Motor 2 controls the left Fork.

Manually raise the Forks until the bottom part of the Mobile Goal is not blocking the view of the Distance sensor.

When manually raising the Forks, ensure to raise each Fork on the left and right side of Moby simultaneously, since each is controlled by their own motor.

Note: avoid pinch points when manually raising the Forks.

The number of degrees that the Forks have been raised is reported on the Brain’s screen.

This is the value that will be used in the project when raising and lowering the Forks to pick up and place the Mobile Goal.

### Using Operator blocks

In this project, the <Less than> operator block is used to code the Distance Sensor.

This is because as the Distance Sensor gets closer and closer to an object, the distance in millimeters between the sensor and that object, decreases.

Threshold values can be selected to trigger behaviors of the robot once the distance is less than a predetermined amount.

For example, when the robot is driving toward the Mobile Goal, use the <Less than> operator block to trigger the behavior of the robot stopping once the distance is less than approximately 139 millimeters (mm).

If that value changed to 10 millimeters (mm) for example, the robot would actually run into the Mobile Goal and potentially damage itself, because 10 millimeters (mm) away from the sensor is not far enough to prevent the Forks of the robot from touching the Mobile Goal.

For more information, help, and tips, check out the many resources at VEX Professional Development Plus

Last Updated: