# Using the Distance Sensor in VEXcode VR

The Distance Sensor on the VR Robot reports the distance between the VR Robot and the nearest object. The sensor calculates distance by using the time it takes for ultrasonic waves to bounce off of an object and return to the sensor.

The following article will cover:

• Distance Sensor on the VR Robot
• VEXcode VR Blocks used with the Distance Sensor
• Distance found object Block
• Distance from Block
• Common Uses of the Distance Sensor
• Distance Sensor Example Project

## Distance Sensor on the VR Robot

The Distance Sensor is a sensor that reports the distance between the VR Robot and the nearest solid object.

The Distance Sensor uses the same principles as sonar devices to measure sound. It sends out a tiny pulse of ultrasound with its emitter at one side of the sensor, and then times how long it takes to hear the sound reflect back to the receiver at the other side of the sensor. The sensor calculates the distance based on how long the sound pulse took to make the round trip. Then, the Distance Sensor reports to the VR Robot how far the nearest object is.

## VEXcode VR Blocks used with the Distance Sensor

The blocks used with the Distance Sensor can be found in the Sensing category.

### Distance found object Block

The Distance Sensor detects if there is an object in front of it using the Distance found object block. The Distance Sensor can detect an object or surface within 3000 mm (~118 inches) of the sensor.

The Distance found object block is a Boolean block that returns a condition as either true or false and fits inside any blocks with hexagonal (six-sided) spaces for other blocks. For more information on boolean blocks visit, Block Shapes and Meaning - Tutorials - VEXcode VR article.

### Distance from Block

Using the Distance from block, the Distance Sensor can report the distance of the nearest object in millimeters (mm) or inches.

The Distance from block reports number values and fits inside any blocks with oval spaces. For more information on reporter blocks visit, Block Shapes and Meaning - Tutorials - VEXcode VR article.

The value of the Distance from block can be displayed on the Monitor Console in VEXcode VR. To learn more about the Monitor Console, view the Variable and Sensing Values Monitoring - Tutorials - VEXcode VR article.

The value of the Distance from block can be reported in millimeters (mm) or inches.

The units of millimeters (mm) or inches will appear in the Monitor Console. To learn more about the Monitor Console, view the Variable and Sensing Values Monitoring - Tutorials - VEXcode VR article.

The value of the Distance found object block is a Boolean block that reports true when the Distance Sensor is close to an object, and reports false when it is not close enough to an object.

True or false will be reported and appear in the Monitor Console.

The width of the Distance Sensors range of vision changes as it looks further away from the front of the VR Robot:

• The sensor can detect objects within a 10 degree field of view when looking for objects less than 1000 millimeters (~ 39 inches) away.
• The sensor can detect objects within a 5 degree field of view when looking for objects between 1000 millimeters (~ 39 inches) and 2000 millimeters (~ 78 inches) away.
• The sensor can detect objects within a 2 degree field of view when looking for objects greater than 2000 millimeters (~ 78 inches) away.

## Common Uses of the Distance Sensor

The Distance Sensor can detect if there is an object in front of the VR Robot. This can be used to avoid an obstacle.

The Distance Sensor can measure the distance between the front of the Distance Sensor and the front of the object. This can be helpful to complete a maze without continuously bumping into the walls or to avoid an object.

## Distance Sensor Example Project

In this example, the VR Robot will drive forward if the Distance Sensor finds an object. If it does not detect an object, the VR Robot will turn right and keep checking for an object.

Note: This example will find the first building, but needs improvements to avoid falling off the table.