All pastes #1240523 Raw Edit

ColorSensor.java

public java v1 · immutable
#1240523 ·published 2008-10-30 11:58 UTC
rendered paste body
import lejos.nxt.*;/** * A sensor that is able to distinguish a black/dark surface * from a white/bright surface. *  * Light percent values from an active light sensor and a * threshold value calculated based on a reading over a  * black/dark surface and a reading over a light/bright  * surface is used to make the distinction between the two  * types of surfaces. *   * @author  Ole Caprani * @version 23.08.07 */public class ColorSensor {   private RCXLightSensor ls;    //private LightSensor ls;    private int blackLightValue;   private int whiteLightValue;   private int blueLightValue;   private int blackWhiteThreshold;   public final int Black = 0;   public final int White = 1;   public final int Blue = 2;         public ColorSensor(SensorPort p)   {	   ls = new RCXLightSensor(p); 	   // Use the light sensor as a reflection sensor	   //ls.setFloodlight(true);	   ls.activate();   }       public ColorSensor(SensorPort p,int black, int white)   {	   ls = new RCXLightSensor(p); 	   // Use the light sensor as a reflection sensor	   //ls.setFloodlight(true);	   ls.activate();	   	   blackLightValue = black;	   whiteLightValue = white;	   	   blackWhiteThreshold = (blackLightValue+whiteLightValue)/2;   }	      public ColorSensor(SensorPort p,int black, int white, int blue)   {	   ls = new RCXLightSensor(p); 	   // Use the light sensor as a reflection sensor	   //ls.setFloodlight(true);	   ls.activate();	   	   blackLightValue = black;	   whiteLightValue = white;	   blueLightValue = blue;	   	   blackWhiteThreshold = (blackLightValue+whiteLightValue)/2;   }        public void setColor(String color, int value)   {				if(color.equals("Black"))			blackLightValue = value;		else if(color.equals("White"))			whiteLightValue = value;		else if(color.equals("Blue"))			blueLightValue = value;					blackWhiteThreshold = (blackLightValue+whiteLightValue)/2;   }      public boolean black() {        return (ls.readValue()< blackWhiteThreshold);   }      public boolean white() {	   return (ls.readValue()> blackWhiteThreshold);   }      public boolean blue() {		int val = ls.readValue();			   //return ( ((blueLightValue + 2) > val) && (val > (blueLightValue - 2)) );	   return (  (val >= blueLightValue - 1) && (val <= blueLightValue + 1) );   }      public int light() { 	   return ls.readValue();   }   }