/*** * TinyEdgeFollower * is a LeJOS robot that follows the edge of a black/white line. * * This code is a stripped-down version of what's in * the JimsRobot directory, to show that there's more than one way * to do it, and to give you an example of a less object-oriented style. * * To use it * You'll need a robot with 2 motors (on ports A,B) * and one light sensor (on port S3). * Compile and download the code as usual: * lejosc TinyEdgeFollower.java * The turn on lego brick, aim at IR tower. * lejos TinyEdgeFollower * * Put the robot over the white color, and push "Run". * When it beeps, move it to the black color. * It should then follow the border. * * @version 1.0 Oct 25 2002 * @author Jim Mahoney * ****/ import josx.platform.rcx.*; class TinyEdgeFollower { public static int whiteValue; public static int blackValue; public static void main(String[] args) throws Exception { initialize(); TextLCD.print("Wh"); Thread.sleep(1000); whiteValue = Sensor.S3.readValue(); // define white value. Sound.beep(); TextLCD.print("Bl"); Thread.sleep(2000); blackValue = Sensor.S3.readValue(); // define black value. Sound.beep(); TextLCD.print("--"); while (true){ int howFarFromWhite = Math.abs(Sensor.S3.readValue()-whiteValue); int howFarFromBlack = Math.abs(Sensor.S3.readValue()-blackValue); if ( howFarFromWhite < howFarFromBlack ){ Motor.A.backward(); Motor.B.stop(); } else { Motor.A.stop(); Motor.B.forward(); } Thread.sleep(50); // to avoid too much jiggling. } // end while } // end main public static void initialize(){ Sensor.S3.setTypeAndMode(SensorConstants.SENSOR_TYPE_LIGHT, SensorConstants.SENSOR_MODE_PCT); Sensor.S3.activate(); Motor.A.setPower(7); Motor.B.setPower(7); } }