colorsensor#

Initializing the colorsensor Class#

The colorsensor constructor creates a colorsensor object in the specified Smart Port:

Parameter

Description

port

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

// Construct a Color Sensor "ColorSensor" with the
// colorsensor class.
colorsensor ColorSensor = colorsensor(PORT1)

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

Class Methods#

colorname3()#

The colorname3() method returns the closest color detected to red, green, or blue.

Returns: A ColorType that represents the name of the closest color detected to red, green, or blue.

// Get the name of the detected color "detectedColor" that's
// closest to red, green, or blue.
colorType detectedColor = ColorSensor.colorname3();

colorname()#

The colorname() method returns the name of the closest color detected to the preset ColorType values.

Returns: A ColorType that represents the name of the closest color detected to twelve possible values.

// Get the name of the detected color.
colorType detectedColor = ColorSensor.colorname();

hue()#

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

Returns: An integer representing the hue.

brightness()#

The brightness(bRaw) method reads the brightness value from the Color Sensor.

Parameters

Description

bRaw

Whether to return the raw value detected by the Color Sensor. The default is false.

Returns: An integer representing the brightness.

color()#

The color() method reads the color from the Color Sensor.

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

// Set the color object "Yellow" to the color detected by
// the Color Sensor.
color Yellow = ColorSensor.color();

isNearObject()#

The isNearObject() method returns if the proximity sensor detects an object.

Returns: true if near an object. false if it is not.

// Check if the Color Sensor is near an object.
if ColorSensor.isNearObject(){
    print('near object');
}

setLight()#

This method is called in the following ways:

The setLight(value) method sets the Color Sensor LED on or off.

Parameters

Description

value

A valid LEDStateType.

Returns: None.

The setLight(intensity, units) method sets the Color Sensor LED on or off.

Parameters

Description

intensity

The value for the intensity to set the light to.

units

The only valid unit for intensity is percent.

Returns: None.

detects()#

The detects(color) method checks if the Color Sensor is detecting a colorType color.

Parameters

Description

color

A ColorType representing the color to be detected.

Returns: A boolean indicating whether the color has been detected.

objectDetected()#

The objectDetected(callback) method registers a function to be called when an object detected event occurs.

Parameters

Description

callback

A function that will be called when an object detected event occurs.

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 Color 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 Color Sensor detects an object.
  ColorSensor.objectDetected(detected);
}

installed()#

The installed() method returns if the Color Sensor is installed.

Returns: true if the Color Sensor is installed. false if it is not.