ColorSensor

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

moo
New User
Posts: 23
Joined: Thu Apr 06, 2017 2:43 am

ColorSensor

Postby moo » Fri Apr 28, 2017 1:19 am

I have a trouble getting color sensor to work. It seems to work when I use EV3Control but not in my program. Additionally, light changes from RED to blue/green using EV3Control but light stays red when I run my program.

Same problem with ambient color (works in EV3Control but not in my program). Strangely, RGB always returns 0,0,0 even in EV3Control.

Code: Select all

package AutoPilot2;
import lejos.hardware.lcd.LCD;
import lejos.hardware.motor.*;
import lejos.hardware.port.*;
import lejos.hardware.sensor.*;
import lejos.hardware.Button;
import lejos.hardware.Sound;
import lejos.robotics.Color;
import lejos.robotics.ColorAdapter;
import lejos.robotics.RangeFinderAdapter;
import lejos.robotics.SampleProvider;
import lejos.robotics.navigation.*;
import lejos.utility.Delay;


public class MyVehicle  {

   public static void main(String[] args) throws InterruptedException {
      

      EV3ColorSensor colorSensor=new EV3ColorSensor(SensorPort.S3);
      for(int i=0;i<10;i++)
      {

   SampleProvider sp = colorSensor.getColorIDMode();        
   float[] sample = new float[sp.sampleSize()];

    System.out.println("COLOR: "+sample[0]);;

    Delay.msDelay(10*1000);
      }


   }
   


}

moo
New User
Posts: 23
Joined: Thu Apr 06, 2017 2:43 am

Re: ColorSensor

Postby moo » Fri Apr 28, 2017 1:20 am

needless to say, the output is always 0

moo
New User
Posts: 23
Joined: Thu Apr 06, 2017 2:43 am

Re: ColorSensor

Postby moo » Fri Apr 28, 2017 2:19 am

updated to lejos 0.9.1 beta and same result was obtained.

moo
New User
Posts: 23
Joined: Thu Apr 06, 2017 2:43 am

Re: ColorSensor

Postby moo » Fri Apr 28, 2017 2:39 am

one more question. How do I convert int type to Color enum? I am a java newbie

User avatar
gloomyandy
leJOS Team Member
Posts: 5867
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: ColorSensor

Postby gloomyandy » Fri Apr 28, 2017 2:47 am

That code is not actually reading from the sensor! You need to call fetchSample to read from the device...
https://sourceforge.net/p/lejos/wiki/Se ... Framework/

Oh and there is no need to have the following lines inside of your loop:

Code: Select all

   SampleProvider sp = colorSensor.getColorIDMode();         
   float[] sample = new float[sp.sampleSize()];


You just need to fetchSample and the println in there.
leJOS news https://lejosnews.wordpress.com/

User avatar
gloomyandy
leJOS Team Member
Posts: 5867
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: ColorSensor

Postby gloomyandy » Fri Apr 28, 2017 2:56 am

You can also obtain the color id by simply calling the getColorID method. What color enum are you talking about? The leJOS color class is just a set of int constants...
https://sourceforge.net/p/lejos/ev3/cod ... Color.java
leJOS news https://lejosnews.wordpress.com/

moo
New User
Posts: 23
Joined: Thu Apr 06, 2017 2:43 am

Re: ColorSensor

Postby moo » Tue May 02, 2017 1:30 am

It was extremely helpful. Thank you so much!


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 2 guests