电位器#

初始化电位器类#

使用以下构造函数创建电位器:

Potentiometer(port)

此构造函数使用一个参数:

范围

描述

port

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

# Create the Brain.
brain = Brain()
# Construct a Potentiometer "pot" with the Potentiometer class.
pot = Potentiometer(brain.three_wire_port.a)

This pot object will be used in all subsequent examples throughout this API documentation when referring to Potentiometer class methods.

类方法#

angle()#

The angle(units) method returns the angle measured by the Potentiometer.

参数

描述

units

Optional. A valid RotationUnits type or PERCENT. The default is DEGREES.

**返回:**电位器测量的角度

# Print the current angle of the Potentiometer to the
# Brain's screen.
brain.screen.print(pot.angle())

changed()#

The changed(callback, arg) method registers a callback function for when the value measured by the Potentiometer changes.

参数

描述

callback

当电位器测量的值发生变化时调用的回调函数。

arg

**可选。**传递给回调函数的参数元组。

**返回:**无。

# Define a function potentiometer_changed().
def potentiometer_changed():
    # The Brain will print that the Potentiometer value changed
    # on the Brain's screen.
    brain.screen.print("potentiometer changed")

# Run potentiometer_changed when the value measured by 
# the Potentiometer changes.
pot.changed(potentiometer_changed)