Lego NXT Color Sensor: Print color as string? (Java)

669 views Asked by At

How can I print the string value of the scanned color on the NXT-Screen? My code currently looks like this:

 ColorSensor colorSensor = new ColorSensor(SensorPort.S4);
 ColorSensor.Color color = colorSensor.getColor();
 System.out.println("Color = " + colorSensor.getColorID() + " " + color.getColor());

It only prints out the number of the color twice, but I want it to print "red" or "blue" and so on. Is this possible?

1

There are 1 answers

0
TornaxO7 On

Well, you can't really print "Red" or "blue" out. Each color has a number.
Here is the example of my other answer (click here):

public void isWhite() {
    ColorSensor cs = new ColorSensor(SensorPort.S3);
    cs.setFloodlight(false);
    if (cs.getColor().getColor() == Color.WHITE) {            
      System.out.println("It's white!");
    }     
    System.out.println("It's not white...");
    }

So you have to make your own method to print out which color it is.
Click here if you want to see the constants of the colors