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.