Work with color sensor, reading value in Red mode

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

Moderators: roger, gloomyandy, skoehler

User avatar
ProHacker
New User
Posts: 24
Joined: Mon Dec 25, 2017 8:50 am
Location: Russia

Work with color sensor, reading value in Red mode

Postby ProHacker » Tue Jan 02, 2018 8:51 am

Hello, guys! Happy New Year! :D
Thanks for gloomandy, that links were very helpful. Here's my code, it sets required mode (Red), but I can't understand how to read value from sensor.

Code: Select all

package my.pcg;

import lejos.hardware.Button;
import lejos.hardware.ev3.LocalEV3;
import lejos.hardware.lcd.LCD;
import lejos.hardware.port.*;
import lejos.hardware.sensor.*;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;

public class Sensors {
   public static boolean is_esc() {
      return (Button.readButtons()!=Button.ID_ESCAPE);
   }
   public static void main(String[] args) {
      Port port = LocalEV3.get().getPort("S2");
      SensorModes sensor = new EV3ColorSensor(port);
      SampleProvider pct = sensor.getMode("Red");
      int[] sample = new int[pct.sampleSize()];
      while(is_esc()) {
         LCD.drawInt(???, 2, 2); // ??? is a fragment of code that is had to be insert
         Delay.msDelay(50);
         LCD.clear();
      }
   }
}

Thanks for fast answers!
LeJOS rules! :D

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

Re: Work with color sensor, reading value in Red mode

Postby gloomyandy » Tue Jan 02, 2018 9:58 am

If you read the Wiki page on the Sensor Framework you will see that to read a value from a sensor you call the fetchSample method.

Code: Select all

      float[] sample = new int[pct.sampleSize()];
      while(is_esc()) {
          pct.fetchSample(sample, 0);
          LCD.drawString(Float.toString(sample[0]), 2, 2);


Note that sensor readings are provided as a float (not as an int as in your coe) and in the case of the colour sensor are in the range 0-1:
https://lejos.sourceforge.io/ev3/docs/l ... ensor.html

So if you want to display the value as an int you will need to scale an convert it (or display the flat value as shown above).
leJOS news https://lejosnews.wordpress.com/

User avatar
ProHacker
New User
Posts: 24
Joined: Mon Dec 25, 2017 8:50 am
Location: Russia

Re: Work with color sensor, reading value in Red mode

Postby ProHacker » Tue Jan 02, 2018 11:19 am

Thank you! :wink:
LeJOS rules! :D

User avatar
ProHacker
New User
Posts: 24
Joined: Mon Dec 25, 2017 8:50 am
Location: Russia

Re: Work with color sensor, reading value in Red mode

Postby ProHacker » Tue Jan 02, 2018 11:45 am

Oh, EV3 shows only 0.0, and it doesn't change!
LeJOS rules! :D

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

Re: Work with color sensor, reading value in Red mode

Postby gloomyandy » Tue Jan 02, 2018 12:39 pm

Sigh, then you are doing something wrong. Post all of your code. Does the sensor show a red light? It is the EV3 colour sensor you are using isn't it? Have you got it attached to the correct port?
leJOS news https://lejosnews.wordpress.com/

User avatar
ProHacker
New User
Posts: 24
Joined: Mon Dec 25, 2017 8:50 am
Location: Russia

Re: Work with color sensor, reading value in Red mode

Postby ProHacker » Tue Jan 02, 2018 1:13 pm

Andy, sensor is connected to S2 and lights red. Here's my code:

Code: Select all

package my.pcg;

import lejos.hardware.Button;
import lejos.hardware.ev3.LocalEV3;
import lejos.hardware.lcd.LCD;
import lejos.hardware.port.*;
import lejos.hardware.sensor.*;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;

public class Sensors {
   public static boolean is_esc() {
      return (Button.readButtons()!=Button.ID_ESCAPE);
   }
   public static void main(String[] args) {
      Port port = LocalEV3.get().getPort("S2");
      SensorModes sensor = new EV3ColorSensor(port);
      SampleProvider pct = sensor.getMode("Red");
      float[] sample = new float[pct.sampleSize()];
      while(is_esc()) {
         LCD.drawString(Float.toString(sample[0]), 2, 2);
         Delay.msDelay(50);
         LCD.clear();
      }
   }
}
LeJOS rules! :D

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

Re: Work with color sensor, reading value in Red mode

Postby gloomyandy » Tue Jan 02, 2018 1:16 pm

I don't see any call to fetchSample in that loop.
leJOS news https://lejosnews.wordpress.com/

User avatar
ProHacker
New User
Posts: 24
Joined: Mon Dec 25, 2017 8:50 am
Location: Russia

Re: Work with color sensor, reading value in Red mode

Postby ProHacker » Tue Jan 02, 2018 1:20 pm

Ouch, I sent u wrong code!
LeJOS rules! :D

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

Re: Work with color sensor, reading value in Red mode

Postby gloomyandy » Tue Jan 02, 2018 1:26 pm

Really, how many versions of that code do you have, it is different to the one you first posted. What you reported would be in line with not calling fetchSample.
leJOS news https://lejosnews.wordpress.com/

User avatar
ProHacker
New User
Posts: 24
Joined: Mon Dec 25, 2017 8:50 am
Location: Russia

Re: Work with color sensor, reading value in Red mode

Postby ProHacker » Tue Jan 02, 2018 1:32 pm

fetchSample() changed everything! It works!
Code:

Code: Select all

package my.pcg;

import lejos.hardware.Button;
import lejos.hardware.ev3.LocalEV3;
import lejos.hardware.lcd.LCD;
import lejos.hardware.port.*;
import lejos.hardware.sensor.*;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;

public class Sensors {
   public static boolean is_esc() {
      return (Button.readButtons()!=Button.ID_ESCAPE);
   }
   public static void main(String[] args) {
      Port port = LocalEV3.get().getPort("S2");
      SensorModes sensor = new EV3ColorSensor(port);
      SampleProvider pct = sensor.getMode("Red");
      float[] sample = new float[pct.sampleSize()];
      while(is_esc()) {
         pct.fetchSample(sample, 0);
         LCD.drawString(Float.toString(sample[0]), 2, 2);
         Delay.msDelay(50);
         LCD.clear();
      }
   }
}

But I think this way is too long, so I'll make a library. Please, help me with that. New topic is here.
Thanks for help!
LeJOS rules! :D


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 1 guest