/* * IntelliBrainGP2D12 * * Copyright 2004 by RidgeSoft, LLC., PO Box 482, Pleasanton, CA 94566, U.S.A. * www.ridgesoft.com * * RidgeSoft grants you the right to use, modify, make derivative works and * redistribute this source file provided you do not remove this copyright notice. */ import com.ridgesoft.intellibrain.IntelliBrain; import com.ridgesoft.io.Display; import com.ridgesoft.robotics.RangeFinder; import com.ridgesoft.robotics.sensors.SharpGP2D12; /** * This class provides an example of the Sharp GP2D12 infrared * range finder attached to the IntelliBrain controller. * * The range finder must be connected to analog input A1. */ public class IntelliBrainGP2D12 { public static void main(String args[]) { // Use a try-catch block around all of the code to catch and report any exceptions. try { Display display = IntelliBrain.getLcdDisplay(); display.print(0, "GP2D12"); display.print(1, ""); // Instantiate the range finder object, giving the input port and the power // control port. In this case power is always on, so the power control is // set to null. RangeFinder rangeFinder = new SharpGP2D12(IntelliBrain.getAnalogInput(1), null); // Loop forever. int noDataCount = 0; while (true) { rangeFinder.ping(); // Tell the sensor to sample float distance = rangeFinder.getDistanceInches(); // Read the distance measured by the range finder if (distance < 0.0f) display.print(1, "---"); // < 0.0 indicates no data or out of range else display.print(1, Integer.toString((int)(distance + 0.5)) + '"'); // display the distance Thread.sleep(500); } } catch (Throwable t) { t.printStackTrace(); } } }