2

So, basically what i'm trying to do is:

i let my NXT drive a parkour, and while he's doing that, he has to display the colors of a piece of paper (he drives over the paper) on the LCD.

The colors are red, green and blue.

The one thing that's not working is: reading or "seeing" the color and displaying them on screen.

the code i got now is :

    ColorSensor cs = new ColorSensor(SensorPort.S1);
    Color color = cs.getColor();

    int groen = color.getGreen();
    int rood = color.getRed();
    int blauw = color.getBlue();

    String text = "";

    if (color.getColor() == groen){
        text = "groen";
    }
    else if (color.getColor() == rood ){
        text = "rood";
    }
    else if (color.getColor() == blauw ){
        text = "blauw";
    }


    LCD.drawString("kleur is: " + text, 0, 0);
    Thread.sleep(6000);
  • Can you check the different values that you obtain with getGreen, getBlue, getRed and getColor, and add them to your post? – Pablo Lozano Jun 02 '17 at 11:13
  • 1
    a ``Color`` consists of the three components red, green and blue (RGB). A getter such as ``getGreen()`` only gets your the green component of the whole color, you cannot compare that to another color and expect a meaningful result. – f1sh Jun 02 '17 at 11:19

1 Answers1

1

The getters of the Color object don't return any green, blue or red constant values, they tell you how green, red or blue is the detected color, from 0 to 256.

For example, a yellow-ish color should return a low red component, and higher blue and green values, very similar between them.

You could try something like:

ColorSensor cs = new ColorSensor(SensorPort.S1);
Color color = cs.getColor();
String text;

if (color.getGreen()>color.getRed() && color.getGreen()>color.getBlue()) {
    text="green";
} else if (color.getBlue()>color.getRed() && color.getBlue()>color.getGreen()) {
    text="blue";
} else {
    text="red";
}
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59