How to use the color sensor?

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

Moderators: roger, gloomyandy, skoehler

How to use the color sensor?

Postby vbelenky » Sun Dec 15, 2013 3:43 pm

I found this class EV3ColorSensor. I wrote this code:

Code: Select all
LocalUARTPort port = new LocalUARTPort();
EV3ColorSensor colorSensor = new EV3ColorSensor(port);


However, on the 2nd line it throws the following exception:

Code: Select all
Set mode 0
Exception in thread "main" java.lang.IllegalArgumentException: Invalid sensor mode
        at lejos.nxt.UARTSensor.<init>(UARTSensor.java:35)
        at lejos.nxt.UARTSensor.<init>(UARTSensor.java:23)
        at lejos.nxt.EV3ColorSensor.<init>(EV3ColorSensor.java:33)
        at EV3FirstProgram.main(EV3FirstProgram.java:19)


Has anybody used the color sensor?
vbelenky
New User
 
Posts: 18
Joined: Sun Dec 01, 2013 4:08 am

Re: How to use the color sensor?

Postby nzelot » Fri Dec 20, 2013 2:58 pm

Hi

you just have to write:

Code: Select all
//Color Sensor at Port 2
EV3ColorSensor colSensor = new EV3ColorSensor(SensorPort.S2);


and then use:
Code: Select all
//To get the Color
colSensor.getColorID();

//To get the reflection/ambient:
float[] buf = new float[colSensor.getMode(/* the mode number/string here */).sampleSize()];
colSensor.getMode(/* the mode number/string here */).fetchSample(buf, 0);

//Now there should be, depending on your choosen mode, some values in your array just use them


Hope this helps.

Works fine for me.

Good Luck and Merry Christmas :D

nZeloT
nzelot
New User
 
Posts: 4
Joined: Mon Oct 21, 2013 3:13 pm

Re: How to use the color sensor?

Postby gloomyandy » Fri Dec 20, 2013 3:59 pm

Please be careful when trying to help others. The code you have posted is for the latest git version of leJOS. The code posted by the OP was for the 0.4.0-alpha version. Your code will not work with the 0.4.0-alpha SD card (at least not without you using a different set of class libs).


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

Re: How to use the color sensor?

Postby vbelenky » Sat Dec 21, 2013 1:16 pm

Hi Andy,

So from what I understand I have 2 choices:
(1) I should just wait for a future release of leJOS
(2) I should learn how to get the latest code from git and create the sdcard with that

Is this correct?
vbelenky
New User
 
Posts: 18
Joined: Sun Dec 01, 2013 4:08 am

Re: How to use the color sensor?

Postby gloomyandy » Sat Dec 21, 2013 5:45 pm

Please see the sticky post about using the alpha firmware.

I'm afraid I don't have the time to comment on every post that you make. Your previously posted code is just incorrect. You need to specify which sensor port you are using. Please take a little time to look at the sample code we provide or browse the leJOS source code this will help you understand how to use the classes. At the moment things are changing too fast for us to be able to write tutorial documents, so if you are not able to work things out from the source then you may have to wait until things settle down a little. There is a sample that makes use of the color sensor the source is here:
https://sourceforge.net/p/lejos/ev3/ci/ ... rTest.java
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3901
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK


Return to EV3 Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff