Analog#

Initializing the Analog Class#

An Analog object is created by using the following constructor:

Analog(port)

This constructor uses one parameter:

Parameter

Description

port

The 3-Wire Port that the Analog device 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 Analog Class constructor.

# Create the Brain.
brain = Brain()
# Construct an Analog "analog" with the
# Analog class.
analog = Analog(brain.three_wire_port.a)

This analog object will be used in all subsequent examples throughout this API documentation when referring to Analog 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.

analog = Analog(brain.three_wire_port.a)

# Define the function analog_changed.
def analog_changed():
    # The Brain will print that the analog input 
    # changed on the Brain's screen.
    brain.screen.print("analog changed")
    brain.screen.next_row()
# Run analog_changed when the value of the
# analog input changes.
analog.changed(analog_changed)

value()#

The value() method returns the value of the analog device.

analog = Analog(brain.three_wire_port.a)

# Display the analog values
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Default:", analog.value())
    brain.screen.next_row()
    brain.screen.print("TENBIT:", analog.value(AnalogUnits.TENBIT))
    brain.screen.next_row()
    wait(.5, SECONDS)