Well a red object should return zero as that is the value that leJOS uses for red. So it just sounds like your sensor is nor differentiating between red and yellow very well. So you need to try and work out why that might be. Have you tested your sensor just on a red and a yellow object? Make sure you use an actual LEGO yellow brick as this will be the colour that the sensor has been optimised for. Does your sensor recognise those objects correctly? If so how does the yellow of the brick compare to whatever it is that you are trying to recognise with your robot? You may also need to experiment with the placement of your sensor (how far away from the object) and the lighting conditions (does building a shade around the sensor help).

I doubt if there is anything wrong with your code. However I notice that you constantly call getColorIdMode before every fetchSample, there is no need to do this, just get it once and store the handle for use later.

Already probe, when the robot is stopped also reads the same values, use another sensor and have the same problem. He sometimes reads value three. That's why I call constantly to getColorIdMode, so that at some point I read three.The sensor is closest to the object and reads three.

How do you optimize this? How to improve my code so that it reads as soon as possible even using the sensorPiso.setFloodlight (Color.NONE) statement. I have the problem.

No changes to your code will make the sensor any better at differentiating between red and yellow, you may be able to make changes to your robot to do that (like shading the sensor from external light).

You may be able to do a better job of detecting the different colours by using the sensor in RGB mode and comparing the values returned but this is not a trivial thing to do.

What exactly is it that you are testing the colour of? Is it some sort of track or competition board (post a picture of it if you can). It may just be that the yellow colour being used is not the same as the LEGO yellow.

Oh and please stop sending a private message to me for each of your posts. Just post your questions on the forum.