I2C encoder (#4991) & seesaw - when do values wrap?

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
loneboat
 
Posts: 4
Joined: Fri Jun 11, 2021 12:38 pm

I2C encoder (#4991) & seesaw - when do values wrap?

Post by loneboat »

I have successfully wired up and am talking to the rotary encoder from here: https://learn.adafruit.com/adafruit-i2c ... ry-encoder

I am using the seesaw library to retrieve the value, and I notice that as I cycle through the values, I get something like this:

Code: Select all

Position: 0
Position: -1
Position: -2
Position: 0
Position: 2
Position: 4
Position: 5
Position: 6
Position: 4
Position: 3
Position: 2
Position: 1
Position: -1
Position: -2
Position: -3
Position: -5
Position: -6
Position: -7
Position: -6
...
Position: -401
Position: -402
Position: -403
Position: -404
Position: -405
Position: -406
...
I'm wondering at what point this value will wrap over back into positive numbers?

User avatar
mikeysklar
 
Posts: 14180
Joined: Mon Aug 01, 2016 8:10 pm

Re: I2C encoder (#4991) & seesaw - when do values wrap?

Post by mikeysklar »

I think the code has been updated in the last 8 days to make the clockwise rotation a positive value.

Are you running the latest example?

https://github.com/adafruit/Adafruit_Ci ... eopixel.py

Code: Select all

# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT

"""I2C rotary encoder NeoPixel color picker and brightness setting example."""
import board
from adafruit_seesaw import seesaw, neopixel, rotaryio, digitalio

try:
    import _pixelbuf
except ImportError:
    import adafruit_pypixelbuf as _pixelbuf

# For use with the STEMMA connector on QT Py RP2040
# import busio
# i2c = busio.I2C(board.SCL1, board.SDA1)
# seesaw = seesaw.Seesaw(i2c, 0x36)

seesaw = seesaw.Seesaw(board.I2C(), 0x36)

encoder = rotaryio.IncrementalEncoder(seesaw)
seesaw.pin_mode(24, seesaw.INPUT_PULLUP)
switch = digitalio.DigitalIO(seesaw, 24)

pixel = neopixel.NeoPixel(seesaw, 6, 1)
pixel.brightness = 0.5

last_position = -1
color = 0  # start at red

while True:

    # negate the position to make clockwise rotation positive
    position = -encoder.position

    if position != last_position:
        print(position)

        if switch.value:
            # Change the LED color.
            if position > last_position:  # Advance forward through the colorwheel.
                color += 1
            else:
                color -= 1  # Advance backward through the colorwheel.
            color = (color + 256) % 256  # wrap around to 0-256
            pixel.fill(_pixelbuf.colorwheel(color))

        else:  # If the button is pressed...
            # ...change the brightness.
            if position > last_position:  # Increase the brightness.
                pixel.brightness = min(1.0, pixel.brightness + 0.1)
            else:  # Decrease the brightness.
                pixel.brightness = max(0, pixel.brightness - 0.1)

    last_position = position

User avatar
loneboat
 
Posts: 4
Joined: Fri Jun 11, 2021 12:38 pm

Re: I2C encoder (#4991) & seesaw - when do values wrap?

Post by loneboat »

mikeysklar wrote:I think the code has been updated in the last 8 days to make the clockwise rotation a positive value.

Are you running the latest example?

https://github.com/adafruit/Adafruit_Ci ... eopixel.py
...
Oh this isn't about the direction vs the sign of the values (and yes, it works as you stated). My question was about when does a rotation in to -1234, -1235, -1236... wrap back into some positive value (overflow/underflow). I.e. surely I can't get down to negative 9999999^999999?

User avatar
mikeysklar
 
Posts: 14180
Joined: Mon Aug 01, 2016 8:10 pm

Re: I2C encoder (#4991) & seesaw - when do values wrap?

Post by mikeysklar »

@loneboat,

CircuitPython uses unsigned int by default so 65536'ish would be your rotational range unless you want to define a shorter range to reset at.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”