How to calibrate the Lego NXT color sensor with LeJos NXJ?

孤街醉人 提交于 2019-12-06 11:09:49

First you have to be aware of these conditions:

  1. The color sensor should be about 1cm over the color.
  2. The darker your room is, the more your sensor can detect.

So here is my code to control if the color is white:

public boolean isWhite() {
    //Gives "True" if the color is white
    ColorSensor cs = new ColorSensor(SensorPort.S3);
    cs.setFloodlight(false);
    if (cs.getColor().getColor() == Color.WHITE) {            
      return true;
    }     
    return false;           
    }

Explanation:
The first cs.getColor() in the if - clause gives the rgb value of the color back.
If you call the method cs.getColor() after the first cs.getColor() (so it will be: cs.getColor().getColor) than it will return its color ID.
In this case Color.WHITEis 6.
You can see all numbers here.


Note:
I'm sorry for my bad english and also sorry if this explanation isn't detailed too! I'm not really a java programmer, I just needed that for my school project, therefore sorry that I couldn't explained it more detailed, but I hope this answer can help some people! ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!