adafruit / Adafruit_CircuitPython_ADXL34x

A CircuitPython driver for the ADXL34x family of accelerometers
MIT License
37 stars 15 forks source link

Trouble setting range #21

Closed AromaticSugar closed 4 years ago

AromaticSugar commented 4 years ago

Hi,

I'm trying to set the range using the library, but I'm running some challenges.

I have: import adafruit_adxl34x i2c = busio.I2C(board.SCL, board.SDA) accelerometer = adafruit_adxl34x.ADXL345(i2c)

When using: accelerometer.range = Range.RANGE_16_G

I get this error:

Traceback (most recent call last): File "/home/pi/accelerometer.py", line 23, in accelerometer.range = Range.RANGE_16_G NameError: name 'Range' is not defined

I'm obviously doing something wrong, but there is no examples on how to set the range.

FoamyGuy commented 4 years ago

@AromaticSugar The Range object is inside of adafruit_adxl34x Try adding this line to your imports:

from adafruit_adxl34x import Range

Or if you prefer you can leave the imports the way they are and instead use a fully qualified name in-front of Range like this:

accelerometer.range = adafruit_adxl34x.Range.RANGE_16_G

Personally I prefer the from something import something approach because it makes the code later on smaller by not requiring you to put the fully qualified name.

AromaticSugar commented 4 years ago

Thank you so much! This works beautifully!

FoamyGuy commented 4 years ago

Excellent! glad you got it working. I'm going to close this issue for now.You can make a new issue if you start having other troubles. Also the #help-with-circuitpython channel in the Adafruit Discord: http://adafru.it/discord is another great place to get help with things like this, feel free to join and post questions in there if you like.