Lejos RGB color sensor

Milhous picture Milhous · Sep 28, 2010 · Viewed 8k times · Source

Does anyone have a code example of using the NXT RGB Color Sensor in the Lejos programming language. I have tried several different uses of setType() and setMode() but to no avail.

Answer

Milhous picture Milhous · Feb 15, 2011

Here is some working code:

ColorLightSensor cs = new ColorLightSensor(SensorPort.S1, SensorConstants.TYPE_LIGHT_ACTIVE);

for(int i = 0; i < 100 && !done; i++)
{
    cs.setFloodlight(lejos.robotics.Colors.Color.RED);

    sleep(1);

    LCD.clearDisplay();
    LCD.drawString("" + cs.getRedComponent(), 0,0);
    cs.setFloodlight(lejos.robotics.Colors.Color.GREEN);

    sleep(1);

    LCD.clearDisplay();
    LCD.drawString("" + cs.getGreenComponent(), 0,0);
    cs.setFloodlight(lejos.robotics.Colors.Color.BLUE);

    sleep(1);

    LCD.clearDisplay();
    LCD.drawString("" + cs.getBlueComponent(), 0,0);
}