1

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.

Milhous
  • 14,473
  • 16
  • 63
  • 82

2 Answers2

1

Here is a simple example for monitoring the NXT 2 color sensor:

import lejos.nxt.*;
import lejos.robotics.*;

public class MyColorSensor {
    public static void main(String argv[]) {
        ColorSensor cs = new ColorSensor(SensorPort.S1);

        for(int i = 0; i < 10; i++) {
            Color color = cs.getColor();
            System.out.println("Color = " + cs.getColorID() + " " + color.getColor() +
                "(" + color.getRed() + "," + color.getGreen() + "," + color.getBlue() 
                +") " + color.getColor());
            Button.waitForAnyPress();
        }       
    }
}
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
lyaffe
  • 1,407
  • 15
  • 24
1

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);
}
Eric
  • 95,302
  • 53
  • 242
  • 374
Milhous
  • 14,473
  • 16
  • 63
  • 82