light#

Initializing the light Class#

A Light Sensor is created by using the following constructor:

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

Parameter

Description

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.

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.

A brightness of 0% is a raw value of 900 or greater. A brightness of 100% is a raw value of 0.

Parameters

Description

units

The only valid unit for brightness is percent.

Returns: A double representing the brightness level of the Light Sensor in the range of 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.

Parameters

Description

callback

The callback function to be called when the value of the Light Sensor changes.

Returns: None.

// 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);
}