Light#
Initializing the Light Class#
A Light Sensor is created by using the following constructor:
Light(port)
This constructor uses one parameter:
Parameter |
Description |
---|---|
|
The 3-Wire Port that the Light Sensor is connected to, whether it’s a port on the Brain, or a 3-Wire Expander. |
A Brain or 3-Wire Expander must be created first before they can be used to create an object with the Light Class constructor.
# Create the Brain.
brain = Brain()
# Construct a Light Sensor "light" with the Light class.
light = Light(brain.three_wire_port.a)
This light
object will be used in all subsequent examples throughout this API documentation when referring to Light class methods.
Class Methods#
brightness()#
The brightness(units)
method returns the brightness level of light falling on the Light Sensor. The brightness of the Light Sensor is an estimation based on the raw value of the sensor.
A brightness of 0% is a raw value of 900 or greater. A brightness of 100% is a raw value of 0.
Parameters |
Description |
---|---|
units |
Optional. The only valid unit for brightness is |
Returns: The brightness level of the Light Sensor in the range of 0% - 100%.
# Get Light Sensor brightness in range of 0% - 100%.
value = light.brightness()
changed()#
The changed(callback, arg)
method registers a callback function for when the value of the Light Sensor changes.
This is a non-blocking command and allows the next command to run without delay.
Parameters |
Description |
---|---|
callback |
The callback function to be called when the value of the Light Sensor changes. |
arg |
Optional. A tuple of arguments to pass to the callback function. |
Returns: None.
# Define a function light_changed()
def light_changed():
# The Brain will print that the Light Sensor value changed
# on the Brain's screen.
brain.screen.print("Light Sensor value changed")
# Run light_changed when the value of this Light Sensor changes.
light.changed(light_changed)