Color Sensors

Detecting Colors

Resources

It is commonly known that colors can be broken down into three basic colors: red, green, and blue. The color sensor scans what is in front of it and returns the basic color values of the scene. By comparing these returned values to predefined threshold values, we can easily determine what color an object in front of the robot is.

Initialization

ColorSensor colorSensor;
colorSensor = hardwareMap.get(ColorSensor.class, "color_sensor");

Implementation

colorSensor.enableLed(true);

Find red, green, blue, or alpha values from 0-255

int red = colorSensor.red();
int blue = colorSensor.blue();
int green = colorSensor.green();
int alpha = colorSensor.alpha(); //0 - Transparent, 255 - Opaque

Return a combined argb color value by using the argb() command.

Rev Color Sensor V3

The newest color sensor by Rev Robotics, version 3, has some extra capabilities, and therefore a different class to use for its functions.

Initialization

The main difference between the Rev V3 and other color sensors is the getDistance() function, meaning the color sensor can return the distance of an object within a certain range, from 1 cm - 10 cm.

It is recommended that you do not use the V3 Color Sensor to actually determine the distance between your robot and an object, but instead use it to determine if an object is in front of the sensor, maybe to see if there is an object in a specific robot module.

Color Thresholding

It is commonly known that colors can be broken down into three basic colors: red, green, and blue. The color sensor scans what is in front of it and returns the basic color values of the scene. By comparing these returned values to predefined threshold values, we can easily determine what color an object in front of the robot is.

Because the resources listed above provide a sufficient explanation of how the code below works, we will just provide you with an implementation.

Last updated