Search code examples
javalego-mindstormslejos-nxj

Measuring EV3 Color Sensor Reflected Light Intensity via leJos


I have a question:

Via leJos, I want to control my ev3 color sensor which is connected to port 2.

But actually when I looked at the EV3ColorSensor class, I couldn't find any method which I can measure the reflected light intensity of my colorsensor connected port 2.

Or I couldn't understand from the names of methods whether there is such a method which can do what I want.

Could you help me for this problem?

Thank you.


Solution

  • Can not test this code at the very moment, but it might need just minimal tweaks:

    import java.util.Arrays;
    
    import lejos.hardware.sensor.EV3ColorSensor;
    import lejos.hardware.port.Port;
    import lejos.hardware.port.SensorPort;
    import lejos.robotics.SampleProvider;
    
    public class ColorSensorTest {
    
        // Modes and samples are explained in LeJOS wiki:
        // http://sourceforge.net/p/lejos/wiki/Sensor%20Framework/
        private static Port colorSensorPort = SensorPort.S2;
        private static EV3ColorSensor colorSensor;
        private static SampleProvider sampleProvider;
        private static int sampleSize;
    
        private static float[] getSample() {
            // Initializes the array for holding samples
            float[] sample = new float[sampleSize];
    
            // Gets the sample an returns it
            sampleProvider.fetchSample(sample, 0);
            return sample;
        }
    
        public static void main(String[] args) {
            // Initializes the sensor & sensor mode
            colorSensor = new EV3ColorSensor(colorSensorPort);
            sampleProvider = colorSensor.getRedMode();
            sampleSize = sampleProvider.sampleSize();
    
            // Takes some samples and prints them
            for (int i = 0; i < 4; i++) {
                float[] sample = getSample();
                System.out.println("N=" + i + " Sample=" + Arrays.toString(sample));
            }
        }
    
    }