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 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.

设定 [Optical 1 v] 灯 [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

当开始
[Repeatedly flash the Optical Sensor's lights.]
永久循环
设定 [Optical 1 v] 灯 [on v]
等待 (1) 秒
设定 [Optical 1 v] 灯 [灭 v]
等待 (1) 秒

set Optical light power#

The set Optical light power block sets the brightness of the Optical Sensor’s light, helping create a more consistent and reliable light source for the sensor.

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

当开始
[Repeatedly change the brightness of the Optical Sensor's lights.]
永久循环
set [Optical1 v] light power to (10)%
等待 (2) 秒
set [Optical1 v] light power to (50)%
等待 (2) 秒
set [Optical1 v] light power to (100)%
等待 (2) 秒

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.

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

Parameters

Description

optical sensor

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

Example

当开始
[Turn until the Optical Sensor detects an object.]
设定转向速度为 (10) [% v]
set [Optical1 v] light power to [75]%
[右 v] 转
等到 <[Optical 1 v] 发现对象?>
在屏幕上打印 [Object detected]◀ 并设定光标为下一行
驱动停止

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.

<[Optical 1 v] 测得 [红色 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

当开始
[Turn until the Optical Sensor detects a red object.]
设定转向速度为 (10) [% v]
set [Optical1 v] light power to [75]%
[右 v] 转
等到 <[Optical 1 v] 测得 [红色 v]?>
在屏幕上打印 [Red detected]◀ 并设定光标为下一行
驱动停止

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

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

([Optical 1 v] 颜色)

Parameters

Description

optical sensor

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

Example

当开始
[Display a message when the Optical Sensor detects red.]
等到 <[Optical 1 v] 测得 [红色 v]?>
在屏幕上打印 ([Optical 1 v] 颜色)◀ 并设定光标为下一行

Optical brightness#

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

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

Parameters

Description

optical sensor

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

Example

当开始
[Display the brightness when the Optical Sensor detects red.]
等到 <[Optical 1 v] 测得 [红色 v]?>
在屏幕上打印 ([Optical 1 v] 亮度百分比)◀ 并设定光标为下一行

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°.

([Optical 1 v] 色调度数)

Parameters

Description

optical sensor

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

Example

当开始
[Print when the Optical Sensor detects pink.]
永久循环
清屏
在屏幕上设定光标至 (1) 行 (1) 列
如果 <<([Optical 1 v] 色调度数) [math_greater_than v] [290]> 与 <([Optical 1 v] 色调度数) [math_less_than v] [350]>> 那么
在屏幕上打印 [Pink!]▶
等待 (0.1) 秒
否则
在屏幕上打印 [Not pink.]▶
等待 (0.1) 秒

When Optical#

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

当 [Optical 1 v] [detects v] 一个对象

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

当 [Optical 1 v] [detects v] 一个对象
[Find an object when it is lost.]
[右 v] 转
等到 <[Optical 1 v] 发现对象?>
驱动停止