Optical#

Introduction#

The Optical Sensor provides methods to return data from different objects. This allows the Brain to detect colors as well as their hue and brightness.

For the examples below, the configured Optical Sensor will be named Optical1 and will be used in all subsequent examples throughout this API documentation when referring to optical class methods.

Below is a list of all available methods:

Actions – Detect objects with the Optical Sensor.

  • objectDetected – Registers a function to call when the Optical Sensor detects an object.

  • objectLost – Registers a function to call when the Optical Sensor loses an object.

Mutators – Modify the Optical Sensor lights and detection ranges.

  • setLight – Turns the Optical Sensor’s LED on or off.

  • setLightPower – Sets the Optical Sensor’s LED to a specific brightness.

  • objectDetectThreshold – Sets the distance an object has to be from the Optical Sensor for it to be detected.

Getters – Return object data with the Optical Sensor.

  • 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 as a float from 0 to 359.99 degrees.

  • installed – Returns whether or not the Optical Sensor is connected to the Brain.

Constructors – Manually initialize the Optical Sensor.

  • optical – Create an Optical Sensor.

Actions#

objectDetected#

objectDetected registers a function to be called when the Color Sensor detects an object.

Usage:
Optical1.objectDetected(callback);

Parameters

Description

callback

A function that is previously defined to execute when the Optical Sensor detects an object.

// Example coming soon

objectLost#

objectLost registers a function to be called when the Color Sensor loses an object.

Usage:
Optical1.objectLost(callback);

Parameters

Description

callback

A function that is previously defined to execute when the Optical Sensor loses an object.

// Example coming soon

Mutators#

setLight#

setLight turns the Optical Sensor’s LED lights on or off.

Usage:
Optical1.setLight(state);

Parameters

Description

state

The state to set the LED lights to:

  • ledState::off
  • ledState::on
// Example coming soon

setLightPower#

setLightPower sets the specific brightness of the Optical Sensor’s LED lights.

Usage:
Optical1.setLightPower(power, units);

Parameters

Description

power

The brightness that the LED lights will be set to from 0 to 100.

units

The units that represent the LED brightness:

  • percent
// Example coming soon

objectDetectThreshold#

objectDetectThreshold sets the distance away an object must be before the Optical Sensor will detect it.

Usage:
Optical1.objectDetectThreshold(value);

Parameters

Description

value

The new detection threshold as an integer from 0 to 255. A value of 0 means no change to the threshold.

// Example coming soon

Getters#

isNearObject#

isNearObject returns a Boolean indicating whether or not the Optical Sensor detects a close object.

  • 1 – The Optical Sensor detects a close object.

  • 0 – The Optical Sensor does not detect a close object.

Usage:
Optical1.isNearObject()

Parameters

Description

This method has no parameters.

// Example coming soon

color#

color returns the detected color from the Optical Sensor as an instance of a predefined color object, as shown below:

  • red

  • green

  • blue

  • white

  • yellow

  • orange

  • purple

  • cyan

  • red_violet

  • violet

  • blue_violet

  • blue_green

  • yellow_green

  • yellow_orange

  • red_orange

  • black

  • transparent

Usage:
Optical1.color()

Parameters

Description

This method has no parameters.

// Example coming soon

brightness#

brightness returns the brightness value from the Color Sensor as a double from 0 to 100.

Usage:
Optical1.brightness()

Parameters

Description

This method has no parameters.

// Example coming soon

hue#

hue returns the hue value detected by the Color Sensor as a double from 0 to 359.99.

Usage:
Optical1.hue()

Parameters

Description

This method has no parameters.

// Example coming soon

installed#

installed returns a Boolean indicating whether the Optical Sensor is currently connected to the Brain.

  • 1 – The Optical Sensor is connected to the Brain.

  • 0 – The Optical Sensor is not connected to the Brain.

Usage:
Optical1.installed()

Parameters

Description

This method has no parameters.

// Example coming soon

Constructors#

optical#

optical creates an object of the optical Class in the specified port.

Usage:
optical Optical1 = optical(port);

Parameter

Description

port

Which Smart Port that the Optical Sensor is connected to as PORT followed by the port number, ranging from 1 to 12.

// Example coming soon