Distance Sensor#
Introduction#
The Distance Sensor category can include different blocks depending on whether you’re using a VEX IQ Distance Sensor (1st gen) or (2nd gen).
Distance Sensor blocks can help the robot detect whether an object is in front of the sensor and report how far away that object is. The VEX IQ Distance Sensor (2nd gen) can also report the detected object’s relative velocity and estimate whether the object is small, medium, or large.
Below is a list of all blocks:
VEX IQ Distance Sensor (1st gen) — Blocks that are compatible with the VEX IQ Distance Sensor (1st gen).
Distance found an object — Reports whether the Distance Sensor currently detects an object.
object distance — Reports the distance between the Distance Sensor and the nearest detected object.
VEX IQ Distance Sensor (2nd gen) — Blocks that are compatible with the VEX IQ Distance Sensor (2nd gen).
object distance — Reports the distance between the Distance Sensor and the nearest detected object.
object velocity — Reports how quickly the detected object is moving toward or away from the Distance Sensor.
object size is — Reports whether the detected object matches the selected size.
Distance found an object — Reports whether the Distance Sensor currently detects an object.
VEX IQ Distance Sensor (1st gen)#
Distance found an object#
The Distance found an object Boolean block reports whether the Distance Sensor currently detects an object.
True — The Distance Sensor detects an object.
False — The Distance Sensor does not detect an object.
<[Distance1 v] found an object?>
Parameters |
Description |
|---|---|
distance sensor |
The Distance Sensor to use. Choose from the Distance Sensors configured in the Devices window. |
Example
when started
[Spin until the Distance Sensor detects an object.]
turn [right v]
wait until <[Distance1 v] found an object?>
stop driving
object distance#
The object distance reporter block reports the distance between the Distance Sensor and the nearest detected object.
([Distance1 v] distance in [mm v])
Parameters |
Description |
|---|---|
distance sensor |
The Distance Sensor to use. Choose from the Distance Sensors configured in the Devices window. |
unit |
The distance unit: mm (24 mm to 1000 mm) or inches (1 inch to 40 inches). |
Example
when started
[Print the distance only after the Distance Sensor detects an object.]
turn [right v]
wait until <[Distance1 v] found an object?>
stop driving
print ([Distance1 v] distance in [mm v]) on [Brain v] — and set cursor to next row
VEX IQ Distance Sensor (2nd gen)#
object distance#
The object distance reporter block reports the distance between the Distance Sensor and the nearest detected object.
([Distance1 v] object distance in [mm v])
Parameters |
Description |
|---|---|
distance sensor |
The Distance Sensor to use. Choose from the Distance Sensors configured in the Devices window. |
unit |
The distance unit: mm (20 mm to 2000 mm) or inches (0.78 inches to 78 inches). |
Example
when started
[Print the distance only after the Distance Sensor detects an object.]
turn [right v]
wait until <[Distance1 v] found an object?>
stop driving
print ([Distance1 v] object distance in [mm v]) on [Brain v] — and set cursor to next row
object velocity#
The object velocity reporter block reports the relative velocity of the detected object as a decimal number in m/s (meters per second).
Velocity shows how quickly the object is moving toward or away from the Distance Sensor. A value near 0 means the object is not moving much relative to the sensor.
([Distance1 v] object velocity in m/s)
Parameters |
Description |
|---|---|
distance sensor |
The Distance Sensor to use. Choose from the Distance Sensors configured in the Devices window. |
Example
when started
[Display the velocity of an object moving in front of the Distance Sensor.]
set print precision to [0.01 v] on [Brain v]
forever
set cursor to row (1) column (1) on [Brain v]
print ([Distance1 v] object velocity in m/s) on [Brain v] — and set cursor to next row
wait (0.1) seconds
clear row (1) on [Brain v]
object size is#
The object size is Boolean block reports whether the object detected by the Distance Sensor matches the selected size.
The Distance Sensor estimates object size based on how much of the sensor’s view is taken up by the detected object.
True — The Distance Sensor detects an object of the selected size.
False — The Distance Sensor does not detect an object of the selected size.
<[Distance1 v] object size is [small v]?>
Parameters |
Description |
|---|---|
distance sensor |
The Distance Sensor to use. Choose from the Distance Sensors configured in the Devices window. |
object size |
The object size to check for:
|
Example
when started
[Drive toward a small object until it appears large.]
if <[Distance1 v] object size is [small v]?> then
drive [forward v]
wait until <[Distance1 v] object size is [large v]?>
stop driving
end
Distance found an object#
The Distance found an object Boolean block reports whether the Distance Sensor currently detects an object.
True — The Distance Sensor detects an object.
False — The Distance Sensor does not detect an object.
<[Distance1 v] found an object?>
Parameters |
Description |
|---|---|
distance sensor |
The Distance Sensor to use. Choose from the Distance Sensors configured in the Devices window. |
Example
when started
[Print the distance detected when an object is in front of the Distance Sensor.]
turn [right v]
wait until <[Distance1 v] found an object?>
print ([Distance1 v] object distance in [mm v]) on [Brain v] — and set cursor to next row
stop driving