光#
初始化灯光类#
使用以下构造函数创建光传感器:
The light constructor creates a light object in the specified Three Wire Port:
范围 |
描述 |
|---|---|
|
The 3-Wire Port that the Light Sensor is connected to, whether it’s a port on the |
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。
参数 |
描述 |
|---|---|
|
The only valid unit for brightness is |
**返回:**表示光传感器亮度级别的双精度值,范围为 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.
参数 |
描述 |
|---|---|
|
当光传感器的值发生变化时调用的回调函数。 |
**返回:**无。
// 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);
}