sorter marshin EV3 lejos

Post your EV3 projects, project ideas, etc here!

Moderators: roger, gloomyandy, skoehler

New User
Posts: 8
Joined: Mon Jan 04, 2016 5:02 pm

sorter marshin EV3 lejos

Postby tim1234 » Mon Jan 04, 2016 5:20 pm

sorry for my bad englisch im from germany
im a beginner it is my first project
hi my name is tim and i must programm an sorter maschin in school we work with the new EV3.
the soter marschin shod be able to grap a ball scan ant then soter in different colors (down i will post the quellcode)
now ther is the problem i dont now how to import the color sensor morower i dont knew the commands to read out the information of the color sensor
in the queelcode you can see how i have startet sofar it is working but i dont know how to work with the colorsensor. ( im programmin a grap arm thats the reason for the degrinumbers
thank you

// Start der Programmierung

// Ziele die erfüllt werden müssen:
// 1. Rechts drehen, bis der Tastsensor ausschlägt.
// 2. Objekt aufnehmen ( Motor nach unten fahren / Greifmotor aus bzw. einfahren/ Motor wieder hochfahren)
// 3. 180 Grad Drehung nach links
// 4. Greifer nach unten zum Farbsensor
// 5. Farbe einscannen und erkennen
// 6. 45/90/135 Grad Drehung entsprechend der Farbe (Rot 45 Grün 90 Gelb 135)
// 7. Kugel fallen lassen ( Kiste sollte vorhanden sein)
// 8. Zurück auf Standardposition (

// Motorendeklaration:
// Motor A = Greifer auf/zu
// Motor B = Drehung links/rechts
// Motor C = Greifer auf / ab

// Sensor 1 = Taster ( Drehung nach rechts )
// Sensor 2 = Farbsensor

package sortiermaschine_30_12_15;

import lejos.hardware.lcd.LCD;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.motor.Motor;
import lejos.hardware.port.MotorPort;
import lejos.hardware.port.SensorPort;
import lejos.utility.Delay;
import lejos.hardware.sensor.*;
import lejos.robotics.ColorDetector;
import lejos.robotics.SampleProvider;
import java.awt.color.*;

public class Sortiermaschine_30_12_15 {

private static SensorModes sensor1;
private static EV3LargeRegulatedMotor c;
private static EV3LargeRegulatedMotor b;
private static EV3LargeRegulatedMotor a;
private static EV3ColorSensor Sensor2;

public static void main(String[] args) {
// TODO Auto-generated method stub

a = new EV3LargeRegulatedMotor (MotorPort.A);
b = new EV3LargeRegulatedMotor (MotorPort.B);
c = new EV3LargeRegulatedMotor (MotorPort.C);

Sensor2 = new EV3ColorSensor (SensorPort.S2);

sensor1 = new EV3TouchSensor(SensorPort.S1);

// Greifer nach oben


// Greifarm soweit drehen, bis der Tastsensor ausschlägt

SampleProvider touch = sensor1.getMode("Touch");
float pressed =0;
float[] sample = new float[touch.sampleSize()];
while (pressed == 0){
touch.fetchSample(sample, 0);
pressed = sample[0];


LCD.drawString("Step1", 20, -20); //greifer öffnen'

LCD.drawString("Step2", 20, -20); //greifer auf kugel drücken'

LCD.drawString("Step3", 20, -20); //schliesen des greifers gradzahl muss je nach gröse dae balls angepasst werden'

LCD.drawString("Step4", 20, -20); //greifer mit kugel wird angehoben'

LCD.drawString("Step5", 20, -20); //zum farbsensor rotieren'

LCD.drawString("Step6", 20, -20); //kugel wird auf fabsensohr getrückt'



New User
Posts: 8
Joined: Mon Jan 04, 2016 5:02 pm

Re: sorter marshin EV3 lejos

Postby tim1234 » Mon Jan 04, 2016 5:34 pm

LCD.drawString("Step5", 20, -20); thise commands are just for me to see where the robot is with step..

leJOS Team Member
Posts: 312
Joined: Tue Apr 26, 2011 9:18 pm
Location: Netherlands

Re: sorter marshin EV3 lejos

Postby Aswin » Mon Jan 04, 2016 8:28 pm

Hi Tim,

The color sensor can be used in different ways (we call it modes). You need to tell it how to you want to use it by asking for the right mode. You want to recognize some specific colors. You can use the color ID mode for that. Here is how to do it

Code: Select all

Sensor2   = new EV3ColorSensor (SensorPort.S2);
Sampleprovider colorSensor = Sensor2.getColorIDMode();

You can find this in the JavaDocfor the color sensor.

Before you can get information from the sensor you must do some preparations. You need to have a variable to store the information from the sensor in. All the leJOS sensors store information in the same way. They store it in a small array. The size of this array depends on the sensor and the mode. You can ask the sensor how large the array should be. Here is how:

Code: Select all

int size = colorSensor.sampleSize();
float[] sample= new float[size];

The last step is to actually ask the sensor what it "sees" and to use the information it returns. This is how.

Code: Select all


if (sample[0]== 1) b.rotate(45);
if (sample[0]== 2) b.rotate(90);
//und so weiter

You need to repeat this last block every time you give a the sensor something to look at. The other blocks must be executed one time only, at the start of your program.

If you want to know more about using sensors with leJOS you can read this Wiki article.

Viel Spass,

My NXT blog:

New User
Posts: 8
Joined: Mon Jan 04, 2016 5:02 pm

Re: sorter marshin EV3 lejos

Postby tim1234 » Mon Jan 04, 2016 10:16 pm

Thank you thats was very helpful

Return to “EV3 Projects”

Who is online

Users browsing this forum: No registered users and 2 guests