/* * rcxTest2.java * * Created on September 24, 2003, 10:11 PM */ package RCX1; /** * * @author Ian Smith-Heisters */ import josx.platform.rcx.*; class RCXTest2 { public static int whiteValue; public static int blackValue; public static void main(String[] args) throws Exception { initialize(); TextLCD.print("DEBUG"); Thread.sleep(1000); Motor.A.forward(); TextLCD.print("AFor"); Button.PRGM.waitForPressAndRelease(); Motor.A.stop(); Motor.B.forward(); TextLCD.print("BFor"); Button.PRGM.waitForPressAndRelease(); Motor.B.stop(); Thread.sleep(1000); TextLCD.print("Wh"); Thread.sleep(2000); whiteValue = Sensor.S1.readValue(); // define white value. Sound.beep(); TextLCD.print("Bl"); Thread.sleep(3000); blackValue = Sensor.S1.readValue(); // define black value. Sound.beep(); if (whiteValue == blackValue){ TextLCD.print("OOPS"); }else{ TextLCD.print("GO"); } while (true){ int howFarFromWhite = Math.abs(Sensor.S1.readValue()-whiteValue); int howFarFromBlack = Math.abs(Sensor.S1.readValue()-blackValue); if ( howFarFromWhite < howFarFromBlack ){ Motor.A.forward(); Motor.B.stop(); } else { Motor.A.stop(); Motor.B.forward(); } //Thread.sleep(50); // to avoid too much jiggling. } } public static void initialize(){ Sensor.S1.setTypeAndMode(SensorConstants.SENSOR_TYPE_LIGHT, SensorConstants.SENSOR_MODE_PCT); Sensor.S1.activate(); Motor.A.setPower(7); Motor.B.setPower(7); } }