AnalogIn#
Initializing the AnalogIn Class#
An AnalogIn object is created by using the following constructor:
AnalogIn(port)
This constructor uses one parameter:
Parameter |
Description |
---|---|
|
The 3-Wire Port that the AnalogIn 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 AnalogIn Class constructor.
# Create the Brain.
brain = Brain()
# Construct an AnalogIn "analog_in" with the
# AnalogIn class.
analog_in = AnalogIn(brain.three_wire_port.a)
This analog_in
object will be used in all subsequent examples throughout this API documentation when referring to AnagloIn class methods.
Class Methods#
changed()#
The changed(callback, arg)
method registers a function to be called when the value of the analog input changes.
Parameters |
Description |
---|---|
callback |
A function that will be called when the value of analog input changes. |
arg |
Optional. A tuple that is used to pass arguments to the callback function. |
Returns: An instance of the Event class.
# Define the function analogIn_changed.
def analogIn_changed():
# The Brain will print that the analog input
# changed on the Brain's screen.
brain.screen.print("analog input changed")
# Run analogIn_changed when the value of the
# analog input changes.
analog_in.changed(analogIn_changed)
# Get the value of analog input in range 0 - 4095.
value = analog_in.value()
# Get the value of analog input in range 0 - 1023.
value = analog_in.value(AnalogUnits.TENBIT)