import josx.platform.rcx.*; public class wander { public static void main(String[] args) throws InterruptedException { TextLCD.print("Seek"); Motor.A.forward(); Motor.C.forward(); //S1 and S3 are each actually a light sensor and a touch sensor //I found a trick to just pretend they're light sensors, since the touch //sensor will return "100%" when it is activated, otherwise the light's //reading is used. This way, I can have 4 sensors on two ports! // 3 is the code for light sensor and 0x80 is the code for % // 1 is touch and 0x20 would be boolean Sensor.S1.setTypeAndMode (3, 0x80); Sensor.S1.activate(); Sensor.S1.addSensorListener (new SensorListener() { public void stateChanged (Sensor src, int oldValue, int newValue) { // Will be called whenever sensor value changes if(newValue==100) // if we were just bumped { //back up Motor.A.reverseDirection(); Motor.C.reverseDirection(); try { Thread.sleep (1000); } catch (InterruptedException e) {} //turn Motor.A.reverseDirection(); try { Thread.sleep (1000); } catch (InterruptedException e) {} //go forward again Motor.C.reverseDirection(); } else { //for now, lets just print the value -- this is where //some complicated looking for light code might go LCD.showNumber(newValue); try { Thread.sleep (100); } catch (InterruptedException e) {} } } }); Sensor.S3.setTypeAndMode (3, 0x80); Sensor.S3.activate(); Sensor.S3.addSensorListener (new SensorListener() { public void stateChanged (Sensor src, int oldValue, int newValue) { if(newValue==100) // if we were just bumped { // back up! Motor.A.reverseDirection(); Motor.C.reverseDirection(); try { Thread.sleep (1000); } catch (InterruptedException e) {} // turn Motor.C.reverseDirection(); try { Thread.sleep (1000); } catch (InterruptedException e) {} // go forward again Motor.A.reverseDirection(); } else { //for now, just print the value -- this is where some //compliciated code for searching for light would go LCD.showNumber(newValue); try { Thread.sleep (100); } catch (InterruptedException e) {} } } }); //For now, keep running until told to stop Button.RUN.waitForPressAndRelease(); } }