4 * Abstraction for a NXT light sensor.
\r
5 * The light sensor can be calibrated to low and high values.
\r
7 public class LightSensor implements SensorConstants
\r
10 private int _zero = 1023;
\r
11 private int _hundred = 0;
\r
14 * Create a light sensor object attached to the specified port.
\r
15 * The sensor will be set to floodlit mode, i.e. the LED will be turned on.
\r
16 * @param port port, e.g. Port.S1
\r
18 public LightSensor(ADSensorPort port)
\r
21 port.setTypeAndMode(TYPE_LIGHT_ACTIVE,
\r
26 * Create a light sensor object attached to the specified port,
\r
27 * and sets floodlighting on or off.
\r
28 * @param port port, e.g. Port.S1
\r
29 * @param floodlight true to set floodit mode, false for ambient light.
\r
31 public LightSensor(ADSensorPort port, boolean floodlight)
\r
34 port.setTypeAndMode(
\r
35 (floodlight ? TYPE_LIGHT_ACTIVE
\r
36 : TYPE_LIGHT_INACTIVE),
\r
43 * Set floodlighting on or off.
\r
44 * @param floodlight true to set floodit mode, false for ambient light.
\r
46 public void setFloodlight(boolean floodlight)
\r
48 port.setType((floodlight ? TYPE_LIGHT_ACTIVE
\r
49 : TYPE_LIGHT_INACTIVE));
\r
53 * Read the current sensor value.
\r
54 * Use calibrateLow() to set the zero level, and calibrateHigh to set the 100 level.
\r
55 * @return Value as a percentage of difference between the low and high calibration values.
\r
57 public int readValue()
\r
59 // return ((1023 - port.readRawValue()) * 100/ 1023);
\r
60 if(_hundred == _zero)return 1023;
\r
61 return 100*(port.readRawValue() - _zero)/(_hundred - _zero);
\r
65 * Read the current sensor normalized value. Allows more accuracy
\r
66 * than readValue(). For LEGO sensor, values typically range from
\r
67 * 145 (dark) to 890 (sunlight).
\r
68 * @return Value as raw normalized (0 to 1023)
\r
70 public int readNormalizedValue() {
\r
71 return 1023 - port.readRawValue();
\r
75 * call this method when the light sensor is reading the low value - used by readValue
\r
77 public void calibrateLow()
\r
79 _zero = port.readRawValue();
\r
82 *call this method whtn the light sensor is reading the high value - used by reaeValue
\r
84 public void calibrateHigh()
\r
86 _hundred = port.readRawValue();
\r