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, and measure brightness and hue.

Below is a list of available blocks:

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
    [Repeatedly flash the Optical Sensor's lights.]
    forever
    set [Optical1 v] light [on v]
    wait (1) seconds
    set [Optical1 v] light [off v]
    wait (1) seconds

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
    [Repeatedly change the brightness of the Optical Sensor's lights.]
    forever
    set [Optical1 v] light power to (10) %
    wait (2) seconds
    set [Optical1 v] light power to (50) %
    wait (2) seconds
    set [Optical1 v] light power to (100) %
    wait (2) seconds

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
    [Turn until the Optical Sensor detects an object.]
    set turn velocity to (10) [% v]
    set [Optical1 v] light power to [75] %
    turn [right v]
    wait until <[Optical1 v] found an object?>
    print [Object detected] on screen ◀ and set cursor to next row
    stop driving

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
    [Turn until the Optical Sensor detects a red object.]
    set turn velocity to (10) [% v]
    set [Optical1 v] light power to [75] %
    turn [right v]
    wait until <[Optical1 v] detects [red v]?>
    print [Red detected] on screen ◀ and set cursor to next row
    stop driving

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
    [Display a message when the Optical Sensor detects red.]
    wait until <[Optical1 v] detects [red v]?>
    print ([Optical1 v] color name) on screen ◀ 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
    [Display the brightness when the Optical Sensor detects red.]
    wait until <[Optical1 v] detects [red v]?>
    print ([Optical1 v] brightness in %) on screen ◀ 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°.

    ([Optical1 v] hue in degrees)

Parameters

Description

optical sensor

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

Example

    when started :: hat events
    [Print when the Optical Sensor detects pink.]
    forever
    clear all rows on screen
    set cursor to row (1) column (1) on screen
    if <<([Optical1 v] hue in degrees) [math_greater_than v] [290]> and <([Optical1 v] hue in degrees) [math_less_than v] [350]>> then
    print [Pink!] on screen ▶
    wait (0.1) seconds
    else
    print [Not pink.] on screen ▶
    wait (0.1) seconds

When Optical#

The When Optical block runs the attached stack of blocks when the selected Optical Sensor detects or loses an object.

    when [Optical1 v] [detects v] an object :: hat events

Parameters

Description

optical sensor

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

state

Which action to trigger the hat block:

  • detects
  • loses

Example

    when [Optical1 v] [detects v] an object :: hat events
    [Find an object when it is lost.]
    turn [right v]
    wait until <[Optical1 v] found an object?>
    stop driving