colorsensor#
Initializing the colorsensor Class#
The colorsensor constructor creates a colorsensor object in the specified Smart Port:
Parameter |
Description |
|---|---|
|
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() command 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.
colorType c = ColorSensor.colorname3();
colorname()#
The colorname() command 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 c = 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 |
|---|---|
|
Whether to return the raw value detected by the Color Sensor. The default is |
Returns: An integer representing the brightness.
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 |
|---|---|
|
A valid |
Returns: None.
The setLight(intensity, units) method sets the Color Sensor LED on or off.
Parameters |
Description |
|---|---|
|
The value for the intensity to set the light to. |
|
The only valid unit for intensity is |
Returns: None.
detects()#
The detects(color) command checks if the color sensor is detecting a colorType color.
Parameters |
Description |
|---|---|
|
A |
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 |
|---|---|
|
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);
}
timestamp()#
The timestamp() method requests the timestamp of the last received status packet from the Color Sensor.
Returns: Timestamp of the last status packet as an unsigned 32-bit integer in milliseconds.
installed()#
The installed() command returns if the color sensor is installed.
Returns: A boolean indicating if the color sensor is installed.