lejos.nxt
Class LightSensor

java.lang.Object
  extended by lejos.nxt.LightSensor
All Implemented Interfaces:
SensorConstants, LampLightDetector, LightDetector

public class LightSensor
extends Object
implements LampLightDetector, SensorConstants

This class is used to obtain readings from a LEGO NXT light sensor. The light sensor can be calibrated to low and high values.


Field Summary
 
Fields inherited from interface lejos.nxt.SensorConstants
MODE_ANGLESTEP, MODE_BOOLEAN, MODE_CELSIUS, MODE_FARENHEIT, MODE_PCTFULLSCALE, MODE_PERIODCOUNTER, MODE_RAW, MODE_TRANSITIONCNT, TYPE_ANGLE, TYPE_COLORBLUE, TYPE_COLORFULL, TYPE_COLORGREEN, TYPE_COLORNONE, TYPE_COLORRED, TYPE_CUSTOM, TYPE_HISPEED, TYPE_LIGHT_ACTIVE, TYPE_LIGHT_INACTIVE, TYPE_LOWSPEED, TYPE_LOWSPEED_9V, TYPE_NO_SENSOR, TYPE_REFLECTION, TYPE_SOUND_DB, TYPE_SOUND_DBA, TYPE_SWITCH, TYPE_TEMPERATURE
 
Constructor Summary
LightSensor(ADSensorPort port)
          Create a light sensor object attached to the specified port.
LightSensor(ADSensorPort port, boolean floodlight)
          Create a light sensor object attached to the specified port, and sets floodlighting on or off.
 
Method Summary
 void calibrateHigh()
          call this method when the light sensor is reading the high value - used by readValue
 void calibrateLow()
          call this method when the light sensor is reading the low value - used by readValue
 Colors.Color getFloodlight()
          Returns the color of the floodlight, including Color.NONE.
 int getHigh()
          return the normalized value corresponding to readValue() = 100%
 int getLightValue()
          Returns the calibrated and normalized brightness of the white light detected.
 int getLow()
          return the normalized value corresponding to readValue() = 0%
 int getNormalizedLightValue()
          Get the normalized light reading
 boolean isFloodlightOn()
          Checks if the floodlight is currently on.
 int readNormalizedValue()
          Get the normalized light reading
 int readValue()
          Get the light reading
 void setFloodlight(boolean floodlight)
          Turns the default LED light on or off.
 boolean setFloodlight(Colors.Color color)
          Used to turn on or off the floodlight by color.
 void setHigh(int high)
          set the normalized value corresponding to readValue() = 100%
 void setLow(int low)
          set the normalized value corresponding to readValue() = 0%
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LightSensor

public LightSensor(ADSensorPort port)
Create a light sensor object attached to the specified port. The sensor will be set to floodlit mode, i.e. the LED will be turned on.

Parameters:
port - port, e.g. Port.S1

LightSensor

public LightSensor(ADSensorPort port,
                   boolean floodlight)
Create a light sensor object attached to the specified port, and sets floodlighting on or off.

Parameters:
port - port, e.g. Port.S1
floodlight - true to set floodit mode, false for ambient light.
Method Detail

setFloodlight

public void setFloodlight(boolean floodlight)
Description copied from interface: LampLightDetector
Turns the default LED light on or off. If the sensor has more than one lamp color, this will control the red LED.

Specified by:
setFloodlight in interface LampLightDetector
Parameters:
floodlight - true to turn on lamp, false for off (ambient light only).

setFloodlight

public boolean setFloodlight(Colors.Color color)
Description copied from interface: LampLightDetector
Used to turn on or off the floodlight by color. If the sensor has multiple light colors, you can control which color is turned on or off. If the color does not exist, it does nothing and returns false.

Specified by:
setFloodlight in interface LampLightDetector
Parameters:
color - Use lejos.robotics.Color enumeration constant to control lamp colors.
Returns:
True if lamp changed, false if lamp color doesn't exist for this sensor.

getLightValue

public int getLightValue()
Description copied from interface: LightDetector
Returns the calibrated and normalized brightness of the white light detected.

Specified by:
getLightValue in interface LightDetector
Returns:
A brightness value between 0 and 100%, with 0 = darkness and 100 = intense sunlight

readValue

public int readValue()
Get the light reading

Returns:
the light level

readNormalizedValue

public int readNormalizedValue()
Get the normalized light reading

Returns:
the raw light level

getNormalizedLightValue

public int getNormalizedLightValue()
Get the normalized light reading

Specified by:
getNormalizedLightValue in interface LightDetector
Returns:
normalized raw value (0 to 1023) LEGO NXT light sensor values typically range from 145 (dark) to 890 (sunlight).

calibrateLow

public void calibrateLow()
call this method when the light sensor is reading the low value - used by readValue


calibrateHigh

public void calibrateHigh()
call this method when the light sensor is reading the high value - used by readValue


setLow

public void setLow(int low)
set the normalized value corresponding to readValue() = 0%

Parameters:
low - the low value

setHigh

public void setHigh(int high)
set the normalized value corresponding to readValue() = 100%

Parameters:
high - the high value

getLow

public int getLow()
return the normalized value corresponding to readValue() = 0%

Specified by:
getLow in interface LightDetector
Returns:

getHigh

public int getHigh()
return the normalized value corresponding to readValue() = 100%

Specified by:
getHigh in interface LightDetector
Returns:

getFloodlight

public Colors.Color getFloodlight()
Description copied from interface: LampLightDetector
Returns the color of the floodlight, including Color.NONE.

Specified by:
getFloodlight in interface LampLightDetector
Returns:
An enumeration of the current color.

isFloodlightOn

public boolean isFloodlightOn()
Description copied from interface: LampLightDetector
Checks if the floodlight is currently on.

Specified by:
isFloodlightOn in interface LampLightDetector
Returns:
true if on, false if off.