Distance Sensor#

Introduction#

The Distance Sensor category can include blocks depending on whether you’re working with the VEX IQ (1st gen) or VEX IQ (2nd gen) Distance Sensor. These blocks allow your robot to detect the presence of an object, measure its distance, determine the object’s size classification, and report its relative velocity.

Below is a list of available blocks:

VEX IQ (1st gen) – Blocks that are compatible with the VEX IQ (1st gen) Distance Sensor.

  • Distance found object? – Returns whether any object is currently detected.

  • object distance – Returns the distance of the nearest object from the Distance Sensor from 24mm to 1000mm.

VEX IQ (2nd gen) – Blocks that are compatible with the VEX IQ (2nd gen) Distance Sensor.

VEX IQ (1st gen)#

Distance found object?#

The Distance found object? block returns a Boolean indicating whether the Distance Sensor has detected an object.

  • True – The Distance Sensor has detected an object.

  • False – The Distance Sensor has not detected an object.

    <[Distance1 v] found an object?>

Parameters

Description

distance sensor

Which Distance Sensor to use, configured in the Devices window.

Example

    when started :: hat events
    [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 returns the distance of the nearest object from the Distance Sensor in a range from 24mm to 1000mm or 1 inch to 40 inches.

    ([Distance9 v] distance in [mm v])

Parameters

Description

distance sensor

Which Distance Sensor to use, configured in the Devices window.

unit

The unit of measurement, which can be one of the following:

  • mm (millimeters)
  • inches

Example

    when started :: hat events
    [Don't print the message until 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 (2nd gen)#

object distance#

The object distance returns the distance of the nearest object from the Distance Sensor in a range from 20mm to 2000mm or 0.78 inches to 78 inches.

    ([Distance1 v] object distance in [mm v])

Parameters

Description

distance sensor

Which Distance Sensor to use, configured in the Devices window.

unit

The unit of measurement, which can be one of the following:

  • mm (millimeters)
  • inches

Example

    when started :: hat events
    [Don't print the message until 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 block returns the current velocity of a detected object in m/s (meters per second).

    ([Distance1 v] object velocity in m/s)

Parameters

Description

distance sensor

Which Distance Sensor to use, configured in the Devices window.

Example

    when started :: hat events
    [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? block returns a Boolean indicating if an object detected by the Distance Sensor is a specific size. The Distance Sensor determines the size of the object detected (none, small, medium, large) based on the amount of light reflected and returned to the sensor.

  • True – The Distance Sensor has detected an object of the specified size.

  • False – The Distance Sensor has not detected an object of the specified size.

    <[Distance1 v] object size is [small v]?>

Parameters

Description

distance sensor

Which Distance Sensor to use, configured in the Devices window.

object size

The size of the object to check for:

  • small
  • medium
  • large

Example

    when started :: hat events
    [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 object?#

The Distance Sensor found object? block returns a Boolean indicating whether the Distance Sensor has detected an object.

  • True – The Distance Sensor has detected an object.

  • False – The Distance Sensor has not detected an object.

    <[Distance1 v] found an object?>

Parameters

Description

distance sensor

Which Distance Sensor to use, configured in the Devices window.

Example

    when started :: hat events
    [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