Optical Sensor#

Introduction#

The Optical Sensor category includes blocks that control and retrieve data from the VEX IQ (2nd generation) Optical Sensor. This sensor can detect objects, identify colors, measure brightness and hue, and detect gestures when set to gesture mode.

Below is a list of available blocks:

set Optical mode#

The set Optical mode block sets an Optical Sensor to detect either colors or gestures. By default, the sensor will detect colors unless this block is used.

    set [Optical1 v] to [color v] mode

Parameters

Description

optical sensor

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

mode

The mode to set the Optical Sensor to:

  • color
  • gesture

Example

    when started :: hat events
    [Set Optical Sensor to detect gestures.]
    set [Optical7 v] to [gesture v] mode
    [Don't print the message until a left gesture is detected.]
    wait until <[Optical7 v] gesture [left v] detected?>
    print [Left gesture detected ] on [Brain v] ◀ and set cursor to next row

set Optical light#

The set Optical light block turns the light on the Optical Sensor on or off. The light lets the Optical Sensor see objects better if it is looking at an object in a dark area.

    set [Optical1 v] light [on v]

Parameters

Description

optical sensor

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

state

The state of the Optical Sensor’s light to set:

  • on
  • off

Example

    when started :: hat events
    set [Optical7 v] light [on v]
    wait (2) seconds
    set [Optical7 v] light [off v]

set Optical light power#

The set Optical light power block sets the brightness of the Optical Sensor’s light.

    set [Optical1 v] light power to (50)%

Note: If the Optical Sensor’s light is off, this block will turn the light on.

Parameters

Description

optical sensor

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

brightness

The brightness of the Optical Sensor’s light from 0 to 100 as a percent.

Example

    when started :: hat events
    set eye light power to [75] %
    [Wait until Eye Sensor detects an object.]
    wait until <eye found an object?>
    print [Object detected] ◀ and set cursor to next row

Optical found object?#

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

  • True – The Optical Sensor has detected an object.

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

    <[Optical1 v] found an object?>

Parameters

Description

optical sensor

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

Example

    when started :: hat events
    set eye light power to [75] %
    [Wait until Eye Sensor detects an object.]
    wait until <eye found an object?>
    print [Object detected] ◀ and set cursor to next row

Optical detects color?#

The Optical detects color? block returns a Boolean indicating whether the Optical Sensor has detected a specific color.

  • True – The Optical Sensor has detected the specified color.

  • False – The Optical Sensor has not detected the specified color.

    <[Optical1 v] detects [red v]?>

Parameters

Description

optical sensor

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

color

Which color to check for:

  • red
  • green
  • blue
  • yellow
  • orange
  • purple
  • cyan

Example

    when started :: hat events
    [Wait until Eye Sensor detects the color red.]
    wait until <eye detects [red v]?>
    print [Color red detected] ◀ and set cursor to next row

Optical color name#

The Optical color name block returns the name of the color detected by the Optical Sensor.

Possible colors are:

  • red

  • green

  • blue

  • yellow

  • orange

  • purple

  • cyan

    ([Optical1 v] color name)

Parameters

Description

optical sensor

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

Example

    when started :: hat events
    [Wait until Eye Sensor detects the color red.]
    wait until <eye detects [red v]?>
    print [Color red detected] ◀ and set cursor to next row

Optical brightness#

The Optical brightness block returns the amount of light detected by the Optical Sensor in a range from 0% to 100%.

    ([Optical1 v] brightness in %)

Parameters

Description

optical sensor

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

Example

    when started :: hat events
    print (eye brightness in %) ◀ and set cursor to next row

Optical hue#

The Optical hue block returns the hue value of the color detected by the Optical Sensor.

Hue values range from 0 to 359 degrees, corresponding to positions on the color wheel shown below.

A circular color wheel displaying a full spectrum of hues labeled with degree values around the perimeter, increasing in 30-degree increments from 0° at the top to 360°.

    (eye hue in degrees)

Parameters

Description

optical sensor

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

Example

    when started :: hat events
    print (eye hue in degrees) ◀ and set cursor to next row

Optical detects gesture?#

The Optical detects gesture? block returns a Boolean indicating whether the Optical Sensor has detected a specific gesture.

  • True – The Optical Sensor has detected the specified gesture.

  • False – The Optical Sensor has not detected the specified gesture.

Note: The Optical Sensor must first be set to detect gestures using the set Optical Sensor mode block, otherwise it will not detect any gestures.

    <[Optical1 v] gesture [up v] detected?>

Parameters

Description

optical sensor

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

gesture

Which gesture to check for:

  • up
  • down
  • left
  • right

Example

    when started :: hat events
    [Set Optical Sensor to detect gestures.]
    set [Optical7 v] to [gesture v] mode
    wait until <[Optical7 v] gesture [left v] detected?>
    print [Left gesture detected.] ◀ and set cursor to next row