光传感器#

介绍#

光传感器测量物体反射或照射到物体上的光的亮度。它输出一个模拟值,表示检测到的光量。

VEX V5 光传感器。

This page uses light_sensor as the example Light Sensor name. Replace it with your own configured name as needed.

以下是可用方法列表:

  • brightness – Returns the amount of light detected by the Light Sensor as a percent.

  • changed – Registers a function to be called whenever the Light Sensor’s brightness value changes.

构造函数 - 手动初始化光传感器。

  • Light – Create a Light Sensor.

亮度#

brightness returns the amount of light detected by the Light Sensor as an integer. This can be used to check if an object is bright enough to accurately report other visual data.

Usage:
light_sensor.brightness(units)

范围

描述

units

Optional. The unit to return the brightness with: PERCENT (default).

已更改#

changed registers a function to be called whenever the Light Sensor’s brightness value changes.

Usage:
light_sensor.changed(callback, arg)

参数

描述

callback

先前定义的 函数,当光传感器的亮度值发生变化时执行。

arg

Optional. A tuple containing arguments to pass to the callback function. See Functions with Parameters for more information.

def my_function():
  brain.screen.print("Brightness changed")

# Call my_function whenever the brightness changes
light_sensor.changed(my_function)

构造函数#

Constructors are used to manually create Light objects, which are necessary for configuring a Light Sensor outside of VEXcode.

Light#

Light creates a Light Sensor.

Usage:
Light(port)

范围

描述

port

The 3-Wire Port that the Light Sensor is connected to:

  • On the V5 Brainbrain.three_wire_port.x where x is the number of the port.
  • On a 3-Wire Expanderexpander.a where expander is the name of the expander instance.

# Create a Light Sensor in Port A
light_sensor = Light(brain.three_wire_port.a)