Optical Sensor#
Introduction#
The optical class is used to access data from the Optical Sensor. It uses reflected light to detect objects, identify colors, and measure brightness and hue.
Class Constructor#
optical Optical = optical(index);
Class Destructor#
Destroys the optical object and releases associated resources.
virtual ~optical();
Parameters#
Parameter |
Type |
Description |
|---|---|---|
|
|
The Smart Port that the Optical Sensor is connected to, written as |
Examples#
// Create an optical instance in Port 1
optical Optical1 = optical(PORT1);
Member Functions#
The optical class includes the following member functions:
setLight— Turns the Optical Sensor’s LED on or off.setLightPower— Sets the Optical Sensor’s light power level.isNearObject— Returns whether or not the Optical Sensor detects an object within range.color— Returns the color detected by the Optical Sensor.brightness— Returns the amount of light reflected from the object.hue— Returns the hue detected by the Optical Sensor.objectDetected— Registers a function to be called when the Optical Sensor detects an object.objectLost— Registers a function to be called when the Optical Sensor loses an object.
Before calling any optical member functions, an optical instance must be created, as shown below:
/* This constructor is required when using VS Code.
Optical Sensor configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */
// Create an optical object in Port 1
optical Optical1 = optical(PORT1);
setLight#
Sets Optical Sensor’s LED to on or off. The light can help the Optical Sensor detect objects and colors more clearly.
Available Functionsvoid setLight(ledState state);
Parameter |
Type |
Description |
|---|---|---|
|
|
Sets the LED on or off. |
This function does not return a value.
Examples// Turn on LED with previous intensity.
Optical.setLight(ledState::on);
setLightPower#
Sets how bright the Optical Sensor’s light is. The light can help the Optical Sensor detect objects and colors more clearly.
A higher percentage makes the light brighter. A lower percentage makes the light dimmer.
If the Optical Sensor’s light is off, setting the light power above 0% will turn the light on.
If the Optical Sensor’s light is on, setting the light power at 0% will turn the light off.
Available Functionsvoid setLightPower(
int32_t intensity,
percentUnits units = percent );
Parameter |
Type |
Description |
|---|---|---|
|
|
The power level to set the light from 0 – 100. |
|
|
The unit that represents the light intensity: |
This function does not return a value.
Examples// Set the light power to 50 percent.
Optical.setLightPower(50, percent);
isNearObject#
Returns whether or not the Optical Sensor detects an object within range.
Available Functionsbool isNearObject();
This function does not accept any parameters.
Return ValuesThis function returns a Boolean indicating if a nearby object is detected:
true— An object is detected nearby.false— An object is not detected.
// If an object is detected by the Optical Sensor, print
// "near object".
if (Optical.isNearObject()){
Brain.Screen.print("near object");
}
color#
Returns the color detected by the Optical Sensor, based on the detected hue value.
Available Functionsvex::color color();
This function does not accept any parameters.
Return ValuesThe color detected by the Optical Sensor as an instance of the color class.
Possible colors are:
blue– Hue value between 196° and 229°blue_green– Hue value between 140° and 195°blue_violet– Hue value between 230° and 239°green– Hue value between 84° and 139°orange– Hue value between 21° and 30°red– Hue value between 350° and 14°red_orange– Hue value between 15° and 20°red_violet– Hue value between 281° and 350°violet– Hue value between 240° and 280°yellow– Hue value between 54° and 74°yellow_green– Hue value between 75° and 83°yellow_orange– Hue value between 31° and 53°
// Set a variable, detectColor, to the color detected by the
// Optical Sensor
color detectColor = Optical.color();
// Print the color detected by the Optical Sensor
// to the Brain's screen
Brain.Screen.print(detectColor);
brightness#
Returns how much light is reflected back to the Optical Sensor.
A higher percentage means more light is reflected back to the Optical Sensor. A lower percentage means less light is reflected back.
Available Functionsdouble brightness(bool bRaw = false);
Parameter |
Type |
Description |
|---|---|---|
|
|
A Boolean value to read raw brightness data instead of percentage. The default is |
A double representing the brightness detected by the Optical Sensor in the range 0 - 100%, or a double representing the raw data detected by the sensor.
Examples// Set a variable, brightness, to the value of the brightness
// detected by the Optical Sensor
double brightness = Optical.brightness();
// Print the brightness detected by the Optical Sensor to the
// Brain's screen
Brain.Screen.print(brightness);
hue#
Returns the hue detected by the Optical Sensor.
Hue is a way to describe color using numbers around a color wheel.

double hue();
This function does not accept any parameters.
Return ValuesA double representing the value of the hue detected by the Optical Sensor in the range 0 — 359.00.
Examples// Set a variable, hue, to the value of the hue detected
// by the Optical Sensor.
double hue = Optical.hue();
objectDetected#
Registers a callback function for when an object is detected.
Available Functionsvoid objectDetected(void (* callback)(void));
Parameter |
Type |
Description |
|---|---|---|
|
|
A previously defined function that executes when the Optical Sensor detects a new object. |
This function does not return a value.
ExamplesDefine the callback function (outside of
int main())// Called when the Optical Sensor detects an object void detected() { // The Brain will print that the Optical Sensor detected an // object to the Brain's screen. Brain.Screen.print("object detected"); }Register the callback inside
int main()/* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ int main() { // Run when the Optical Sensor detects an object. Optical.objectDetected(detected); }
objectLost#
Registers a callback function for when an object is lost.
Available Functionsvoid objectLost(void (* callback)(void));
Parameter |
Type |
Description |
|---|---|---|
|
|
A previously defined function that executes when the Optical Sensor loses a detected object. |
This function does not return a value.
ExamplesDefine the callback function (outside of
int main())// Called when the Optical Sensor loses an object void lost() { // The Brain will print that the Optical Sensor lost an // object to the Brain's screen. Brain.Screen.print("object lost"); }Register the callback inside
int main()/* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ int main() { // Run lost when the Optical Sensor loses an object. Optical.objectLost(lost); }