Optical Sensor#

Introduction#

The Optical Sensing category includes blocks that control and retrieve data from the VEX IQ Optical Sensor. This sensor can detect objects, identify colors, and measure brightness and hue.

Below is a list of all blocks:

set Optical light#

The set Optical light stack 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 — Turns the light on.
  • off — Turns the light off.

Example

    when started
    [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 stack block sets how bright the Optical Sensor’s light is. The light can help the Optical Sensor detect objects and colors more clearly.

A higher percentage makes the light brighter. A lower percentage makes the light dimmer.

If the Optical Sensor’s light is off, setting the light power above 0% will turn the light on.

If the Optical Sensor’s light is on, setting the light power at 0% will turn the light off.

    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
    [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 an object?#

The Optical found an object? Boolean block reports whether the Optical Sensor detects an object within range.

  • 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
    [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? Boolean block reports whether the Optical Sensor detects 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 – Hue value between 341° and 20°.
  • green - Hue value between 81° and 140°.
  • blue - Hue value between 200° and 240°.
  • yellow - Hue value between 41° and 60°.
  • orange
  • purple - Hue value between 281° and 320°.
  • cyan - Hue value between 141° and 200°.

Example

    when started
    [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 reporter block reports the name of the color detected by the Optical Sensor.

Possible colors are:

  • red

  • green

  • blue

  • yellow

  • orange

  • purple

  • cyan

Note: This block can be inserted into the set Touch LED color block to make the Touch LED match the detected color.

    ([Optical1 v] color name)

Parameters

Description

optical sensor

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

Example

    when started
    [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 reporter block reports how bright the detected light is, as a percentage from 0% to 100%.

A higher percentage means the Optical Sensor detects more light. A lower percentage means the Optical Sensor detects less light.

    ([Optical1 v] brightness in %)

Parameters

Description

optical sensor

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

Example

    when started
    [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 reporter block reports the hue detected by the Optical Sensor as a number from 0 to 359 degrees.

Hue is a way to describe color using numbers around a color wheel, as 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
    [Print when the Optical Sensor detects pink.]
    forever
    clear 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 hat block runs the attached stack of blocks when the selected Optical Sensor detects or loses an object.

    when [Optical1 v] [detects v] an object

Parameters

Description

optical sensor

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

state

When the attached stack of blocks will run:

  • detects – An undetected object is now detected
  • loses – A detected object is now no longer detected

Example

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