Optical Sensor#
Introduction#
The optical class is used to access data from the V5 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 LED to a specific brightness.isNearObject— Returns whether or not an object is close to the sensor.color— Return the detected color as a predefined color object.brightness— Returns the detected brightness of an object.hue— Returns the detected hue value.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:
// Create an optical object in Port 1
optical Optical1 = optical(PORT1);
setLight#
Sets Optical Sensor’s LED to on or off.
Available Functionsvoid setLight(ledState state);
Parameter |
Type |
Description |
|---|---|---|
|
|
A valid
|
This function does not return a value.
Examples// Turn on LED with previous intensity.
Optical.setLight(ledState::on);
setLightPower#
Sets the light power of the Optical Sensor’s LED.
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 if the Optical Sensor detects a nearby object.
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 yb the Optical Sensor, print
// "near object".
if (Optical.isNearObject()){
Brain.Screen.print("near object");
}
color#
Returns the color detected by the Optical Sensor.
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.
Examples// 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 the brightness detected by the Optical Sensor.
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 as a float 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 value of the hue detected by the Optical Sensor.
Available Functionsdouble hue();
This function does not accept any parameters.
Return ValuesA double representing the value of the hue detected by the Optical Sensor as a float in the range 0 — 359.99.
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 |
|---|---|---|
|
|
The callback function to be called when an object is detected. |
This function does not return a value.
Examples// Define the lost function with a void return type,
// showing it doesn't return a value.
void detected() {
// The Brain will print that the Optical Sensor detected an
// object to the Brain's screen.
Brain.Screen.print("object detected");
}
// Run lost 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 |
|---|---|---|
|
|
The callback function to be called when an object is lost. |
This function does not return a value.
Examples// Define the lost function with a void return type,
// showing it doesn't return a value.
void lost() {
// The Brain will print that the Optical Sensor lost an
// object to the Brain's screen.
Brain.Screen.print("object lost");
}
// Run lost when the Optical Sensor loses an object.
Optical.objectLost(lost);