Light Watcher Chooser Code



//

import josx.platform.rcx.Sensor;

/**
 * LightWatcher.java
 * A background thread that continuously reads input from the sensors updating the state variable of the JavaRCX
 * 
 * @author CS371 2002
 * @version 1.0
 * @since 1.0
 */

class LightWatcher extends Thread{
  /**
   * Coninuosly loops update the state of lightsensor.
   */
  public void run(){
	while(true){
	    /*We found that reading the sensors all up front and then executing the code below works better than
	      constantly reading inputs in each comparison below*/
	    int s1 = Sensor.S1.readValue();
	    int s2 = Sensor.S2.readValue();
	    int s3 = Sensor.S3.readValue();
	    if(s1 <= JavaRCX.DARK){
		if(s2 <= JavaRCX.DARK){
		    if(s3 <= LineFollower.DARK)
			JavaRCX.state = JavaRCX.ALL;
		    else JavaRCX.state = JavaRCX.LEFT_CENTER;
		}
		else{
		    if(s3 <= JavaRCX.DARK)
			JavaRCX.state = JavaRCX.LEFT_RIGHT;
		    else JavaRCX.state = JavaRCX.LEFT_ONLY;
		}
	    }
	    else{
		if(s2 <= JavaRCX.DARK){
		    if(s3 <= JavaRCX.DARK)
			JavaRCX.state = JavaRCX.CENTER_RIGHT;
		    else JavaRCX.state = JavaRCX.CENTER_ONLY;
		}
		else{
		    if(s3 <= JavaRCX.DARK)
			JavaRCX.state = JavaRCX.RIGHT_ONLY;
		    else JavaRCX.state = JavaRCX.NONE;
		}
	    }
	}
    }
}// LightWatcher

//