光#
初始化灯光类#
使用以下构造函数创建光传感器:
The light
constructor creates a light object in the specified Three Wire Port:
范围 |
描述 |
---|---|
|
必须先创建 Brain 或 3-Wire Expander,然后才能使用 light Class 构造函数创建对象。
// 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);
}