I'm currently writing on a program about detecting different colored balls and sorting them with the Lego NXT and its color sensor. At the beginning it worked quite good but now, the color sensor always returnes the color ID "7" (white), no matter what i do.
On the documentation page i found something about a calibration (calibrateHigh() and calibrateLow()). Does anybody know how to use this calibration or is my color sensor broken?
I tried it with this code:
package com.mydomain;
import lejos.nxt.*;
public class HelloWorld {
public static void main(String[] args) throws Exception {
ColorSensor color = new ColorSensor(SensorPort.S2);
while (true) {
LCD.drawInt(color.getColorID(), 1, 1, 1);
}
}
}
First you have to be aware of these conditions:
- The color sensor should be about 1cm over the color.
- 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.WHITE
is 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! ;)
来源:https://stackoverflow.com/questions/37641882/how-to-calibrate-the-lego-nxt-color-sensor-with-lejos-nxj