package RCX4; import josx.platform.rcx.*; import josx.robotics.*; /** *Manages sensors. */ public class Senses{ public static int white, black; public static Sensor angleSensor, lightSensor; public static ProximitySensor proxSensor; /** *Initializes the sensors. */ public static void init(){ lightSensor = Sensor.S2; angleSensor = Sensor.S3; lightSensor.setTypeAndMode(SensorConstants.SENSOR_TYPE_LIGHT, SensorConstants.SENSOR_MODE_PCT); angleSensor.setTypeAndMode(SensorConstants.SENSOR_TYPE_ROT, SensorConstants.SENSOR_MODE_ANGLE); lightSensor.activate(); angleSensor.activate(); defineWhite(); defineBlack(); LightListener lightL = new LightListener(); lightSensor.addSensorListener(lightL); RotationListener rotL = new RotationListener(); angleSensor.addSensorListener(rotL); proxSensor = new ProximitySensor(Sensor.S1, 17); ProxListener proxL = new ProxListener(proxSensor); }//init /** *Deactivates sensors. proxSensor's Sensor.S1 must be reactivated manually. */ public static void deactivateSensors(){ lightSensor.passivate(); angleSensor.passivate(); Sensor.S1.passivate(); } /** *Defines what white is for the light sensor. Waits for user to press the *View button then sets and echoes the value. */ private static void defineWhite(){ try{ TextLCD.print("white"); Button.VIEW.waitForPressAndRelease(); white = Sensor.S2.readValue(); Sound.beep(); LCD.showNumber(white); Thread.sleep(750); LCD.clear(); }catch (InterruptedException ie){ Sound.buzz(); TextLCD.print("Fail"); } }//defineWhite /** *Defines what black is for the light sensor. Waits for user to press the *View button, then sets and echoes the value. */ private static void defineBlack(){ try{ TextLCD.print("black"); Button.VIEW.waitForPressAndRelease(); black = Sensor.S2.readValue(); Sound.beep(); LCD.showNumber(black); Thread.sleep(750); LCD.clear(); }catch (InterruptedException ie){ Sound.buzz(); TextLCD.print("Fail"); } }//defineBlack /** *Returns true if sensor is over white, black if over black. */ public static boolean getBlackOrWhite(){ final int TOLERANCE = 5; if (lightSensor.readValue() >= white-TOLERANCE){ return true; }else if (lightSensor.readValue() <= white-TOLERANCE){ return false; }else{return false;} } }