0
import lejos.hardware.lcd.LCD;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.EV3UltrasonicSensor;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;

public class NewUl {
    private EV3UltrasonicSensor ev3UltrasonicSensor;
    public NewUl() {
        // TODO Auto-generated constructor stub
        ev3UltrasonicSensor=new EV3UltrasonicSensor(SensorPort.S4);
    }
    public void getData() {
        SampleProvider sampleProvider=ev3UltrasonicSensor.getDistanceMode();
        float[] sample=new float[sampleProvider.sampleSize()];
        sampleProvider.fetchSample(sample, 0);
        LCD.clear();
        LCD.drawString(String.valueOf(sample[0]),0,3);
        LCD.refresh();
        Delay.msDelay(3000);
        LCD.clear();
        LCD.refresh();
    }
    public void close() {
        ev3UltrasonicSensor.close();
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        NewUl newUl=new NewUl();
        newUl.getData();
        newUl.close();  
    }

}

This is a simple lejos program about EV3UltrasonicSensor. The 13th line of code is:

ev3UltrasonicSensor=new EV3UltrasonicSensor(SensorPort.S4);

The 31th line of code is:

NewUl newUl=new NewUl();

The exception thrown is:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid sensor mode at lejos.hardware.sensor.UARTSensor.<init>(UARTSensor.java:62)
at lejos.hardware.sensor.EV3UltrasonicSensor.<init>(EV3UltrasonicSensor.java:75)
at control.NewUl.<init>(NewUl.java:13) at control.NewUl.main(NewUl.java:31)

Such a simple program also can appear mistake? It make me confused.

Could you give me some help or some tips? Any help is appreciated and if you need to know anything more feel free to ask.

Sufian
  • 6,405
  • 16
  • 66
  • 120
  • Exception in thread "main" java.lang.IllegalArgumentException: Invalid sensor mode at lejos.hardware.sensor.UARTSensor.(UARTSensor.java:62) at lejos.hardware.sensor.EV3UltrasonicSensor.(EV3UltrasonicSensor.java:75) at control.NewUl.(NewUl.java:13) at control.NewUl.main(NewUl.java:31) – user6395872 May 29 '16 at 02:32
  • The above statement is error tips.Could you help me? – user6395872 May 29 '16 at 02:34
  • Read [this thread on the LeJOS forums](http://www.lejos.org/forum/viewtopic.php?t=7660), it seems to be similar to your problem. To summarize, are you sure that you are running the most up to date version of LeJOS on *both* the computer and the EV3, are you sure that you have the correct sensor (and not the IR distance sensor), and are you sure that it is plugged in properly to the right port and the cable is good? – Gliderman May 31 '16 at 01:45

1 Answers1

0

From the Exception messages, it looks like that the creation of the instance of lejos.hardware.sensor.EV3UltrasonicSensor is not correct. It may be that the value SensorPort.S4 you passed to the constructor is not proper or you may have to set values to a few more properties of the instance in question.

Sanjeev Saha
  • 2,632
  • 1
  • 12
  • 19