Optical Sensor#

Introduction#

The V5 Optical Sensor detects the color, brightness, and presence of nearby objects using a combination of an RGB color sensor and proximity sensor. It can also use its built-in LED to illuminate objects for more accurate detection.

The VEX V5 Optical Sensor.

Below is a list of available blocks:

set Optical light#

The set Optical light block turns the Optical Sensor’s LED on or off. This can help increase the sensor’s accuracy when detecting colors.

设定 [Optical 1 v] 灯 [on v]

Parameters

Description

device

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

state

The state to set the LED to:

当开始
[Turn on the sensor's LED.]
设定 [Optical 1 v] 灯 [on v]

set Optical light power#

The set Optical light power block sets the brightness of the Optical Sensor’s LED. If the LED’s brightness is set above 0 while it is off, it will automatically turn the LED on.

设定 [Optical 1 v] 灯亮度为 [50]%

Parameters

Description

device

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

brightness

The brightness to set the LED to as a percent.

当开始
[Turn on the sensor's LED at half brightness.]
设定 [Optical 1 v] 灯亮度为 [50]%

Optical Sensor found object?#

The Optical Sensor found object? block returns a Boolean indicating whether the sensor is detecting an object.

  • True – The Optical Sensor is detecting an object.

  • False – The Optical Sensor is not detecting an object.

<[Optical 1 v] 发现对象?>

Parameters

Description

device

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

Example

Drive forward until an object is detected#
当开始
[Drive forward until an object is found.]
驱动 [向前 v]
等到 <[Optical 1 v] 发现对象?>
驱动停止

Optical Sensor detects color?#

The Optical Sensor detects color? block returns a Boolean indicating whether the sensor is detecting a specified color.

  • True – The Optical Sensor is detecting the specified color.

  • False – The Optical Sensor is not detecting the specified color.

Note: The Optical Sensor is looking for hue ranges that match the specified color. For detecting specific hue ranges, see the Optical hue block.

<[Optical 1 v] 测得 [红色 v]?>

Parameters

Description

device

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

color

Which color to look for:

  • red – A detected hue value between 340° - 20°.
  • green – A detected hue value between 80° - 140°.
  • blue – A detected hue value between 200° - 240°.
  • yellow – A detected hue value between 40° - 60°.
  • orange – A detected hue value between 20° - 40°.
  • purple – A detected hue value between 240° - 280°.
  • cyan – A detected hue value between 140° - 200°.

Example

Drive forward until an object is detected#
当开始
[Drive forward until an object is found.]
驱动 [向前 v]
等到 <[Optical 1 v] 发现对象?>
驱动停止

Optical brightness#

The Optical brightness block returns the amount of light reflected from the object as a percent.

([Optical 1 v] 亮度百分比)

Parameters

Description

device

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

Optical hue#

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

This value ranges from 0 to 359 degrees, which correlates to the color wheel:

The VEX Color Wheel showing the degrees of colors around a circle, with red at 0 degrees and transitioning through the rainbow as the value increases.

([Optical 1 v] 色调度数)

Parameters

Description

device

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

Example

当开始
[Look for the color pink using hue.]
永久循环
清屏
在屏幕上设定光标至 [1] 行 [1] 列
如果 <[300] [math_less_than v] ([Optical 1 v] 色调度数) [math_less_than v] [359]> 那么
在屏幕上打印 [This is pink!]▶
否则
在屏幕上打印 [No pink detected!]▶

when Optical#

The when Optical block runs the attached stack whenever the Optical Sensor detects or loses an object.

当 [Optical 1 v] [detects v] 一个对象 :: hat events

Parameters

Description

device

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