#

要使灯光命令在 VEXcode V5 中显示,必须在“设备”窗口中配置 3 线光传感器。

更多信息,请参阅以下文章:

初始化灯光类#

光传感器是通过使用以下构造函数创建的:

The light constructor creates a light object in the specified Three Wire Port:

范围

描述

port

The 3-Wire Port that the Light Sensor is connected to, whether it’s a port on the Brain, or a 3-Wire Expander.

A Brain or 3-Wire Expander must be created first before they can be used to create an object with the light Class constructor.

// Create the Brain.
brain Brian;
// Construct a Light Sensor "light" with the Light class.
light Light = light(Brain.ThreeWirePort.A)

This Light object will be used in all subsequent examples throughout this API documentation when referring to light class methods.

类方法#

brightness()#

The brightness(units) method returns the brightness level of light falling on the Light Sensor. The brightness of the Light Sensor is an estimation based on the raw value of the sensor.

亮度为 0% 时,其原始值为 900 或更高。亮度为 100% 时,其原始值为 0。

参数

描述

units

The only valid unit for brightness is percent.

**返回值:**表示光传感器亮度级别的双精度浮点数,范围为 0% - 100%。

// Get Light Sensor brightness in range of 0% - 100%.
double value = Light.brightness();

changed()#

The changed(callback) method registers a callback function for when the value of the Light Sensor changes.

参数

描述

callback

当光传感器的值发生变化时要调用的回调函数。

**返回值:**无。

// Define the lightChanged function with a void return
// type, showing it doesn't return a value.
void lightChanged() {
  // The Brain will print that the Light Sensor's value
  // changed on the Brain's screen.
  Brain.Screen.print("Light Sensor value changed");
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Drive the robot forward.
  Drivetrain.drive(forward);

  // Run lightChanged when the value of the Light Sensor
  // changes.
  Light.changed(lightChanged);
}