optical#

Initializing the optical Class#

An Optical Sensor is created by using the following constructor:

The optical constructor creates an optical object.

Parameter

Description

port

A valid Smart Port that the Optical Sensor is connected to.

// Construct an Optical Sensor "Optical" with the
// optical class.
optical Optical = optical(PORT1);

This Optical object will be used in all subsequent examples throughout this API documentation when referring to optical class methods.

Class Methods#

hue()#

The hue() method returns the value of the hue detected by the Optical Sensor.

Returns: A double representing the value of the hue detected by the Optical Sensor as a float in the range 0 - 359.99.

// Set a variable, hue, to the value of the hue detected
// by the Optical Sensor.
double hue = Optical.hue();

brightness()#

The brightness(bRaw) method returns the brightness detected by the Optical Sensor.

Parameters

Description

bRaw

A boolean value to read raw brightness data instead of percentage. The default is false.

Returns: A double representing the brightness detected by the Optical Sensor as a float in the range 0 - 100%.

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

color()#

The color() method returns the color detected by the Optical Sensor.

Returns: The color detected by the Optical Sensor as an instance of the color class.

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

isNearObject()#

The isNearObject() method returns if the Optical Sensor detects a nearby object.

Returns: true if a nearby object is detected. false if one is not.

// If an object is detected yb the Optical Sensor, print
// "near object".
if (Optical.isNearObject()){
  Brain.Screen.print("near object");
}

setLight()#

The setLight(value) method sets Optical Sensor’s LED to on or off.

Parameters

Description

value

A valid LED State Type.

Returns: None.

// Turn on LED with previous intensity.
Optical.setLight(ledState.on);

setLightPower()#

The setLightPower(value) method sets the light power of the Optical Sensor’s LED.

Parameters

Description

value

The power level to set the light from 0 - 100.

units

The only valid unit for reflectivity is percent.

Returns: None.

// Set the light power to 50 percent.
Optical.setLightPower(50, percent);

integrationTime()#

This method is called in the following ways:

The integrationTime(value) method sets the Optical Sensor’s integration time.

Parameters

Description

value

The integration time in milliseconds from 5 - 700.

Returns: None.

// Set the integration time to 50 milliseconds.
Optical.integrationTime(50);

The integrationTime() method gets the Optical Sensor’s integration time.

Returns: A double representing the Optical Sensor’s integration time.

// Get the Optical Sensor's integration time.
double intTime = Optical.integrationTime();

getRgb()#

The getRgb(raw) method returns the Optical Sensor’s RGB value.

Parameters

Description

raw

A boolean value to determine if you return raw or processed values. The default is true.

Returns: A tuple with red, green, blue, and brightness values.

// Get the RGB value of the Optical Sensor.
optical::rgbc value = Optical.rgb();

objectDetectThreshold()#

The objectDetectThreshold(value) method sets the object detection threshold.

Parameters

Description

value

A number in the range 0 - 255. A value of 0 will just return the current value.

Returns: The current value of the object detection threshold.

// Set the object detection threshold to 100.
int value = Optical.objectDetectThreshold(100);

gestureEnable()#

The gestureEnable() method enables gesture mode.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Returns: None.

gestureDisable()#

The gestureDisable() method disables gesture mode.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Returns: None.

getGesture()#

The getGesture() method returns the gesture detected by the Optical Sensor.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Returns: An optical::gesture object with the last gesture detection data.

objectDetected()#

The objectDetected(callback) method registers a callback function for when an object is detected.

Parameters

Description

callback

The callback function to be called when an object is detected.

Returns: None.

// Define the detected 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");
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Run detected when the Optical Sensor detects an object.
  Optical.objectDetected(detected);
}

objectLost()#

The objectLost(callback) method registers a callback function for when an object is lost.

Parameters

Description

callback

The callback function to be called when an object is lost.

Returns: None.

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

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Run lost when the Optical Sensor loses an object.
  Optical.objectLost(lost);
}

gestureUp()#

The gestureUp(callback) method registers a callback function for when an upward gesture is detected.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Parameters

Description

callback

The callback function to be called when an upward gesture is detected.

Returns: None.

gestureDown()#

The gestureDown(callback) method registers a callback function for when an downward gesture is detected.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Parameters

Description

callback

The callback function to be called when an downward gesture is detected.

Returns: None.

gestureLeft()#

The gestureLeft(callback) method registers a callback function for when an leftward gesture is detected.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Parameters

Description

callback

The callback function to be called when an leftward gesture is detected.

Returns: None.

gestureRight()#

The gestureRight(callback) method registers a callback function for when an rightward gesture is detected.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Parameters

Description

callback

The callback function to be called when an rightward gesture is detected.

Returns: None.

installed()#

The installed() method checks if the Optical Sensor is connected.

Returns: true if the Optical Sensor is connected. false if it is not.