import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPin;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.PinPullResistance;
public class TestDistance
{
public static void main(String args[]){
final GpioController gpio = GpioFactory.getInstance();
//range sensor pins
GpioPinDigitalOutput sensor_trigger = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_28,
"Sensor Trigger", PinState.LOW);
GpioPinDigitalInput sensor_echo = gpio.provisionDigitalInputPin(RaspiPin.GPIO_25,
"Sensor Echo", PinPullResistance.PULL_DOWN);
// Create the range sensor
Distance rangesensor = new Distance(sensor_trigger, sensor_echo);
int i = 0;
do {
i++;
// Get the range
double distance = rangesensor.getDistCM();
System.out.println("RangeSensorresult =" + distance + "cm");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (i < 1000);
gpio.unprovisionPin(gpio.getProvisionedPins().toArray(new GpioPin[]{sensor_trigger, sensor_echo}));
}
}