-1

I took an Adafruit code example ([Rotary encoder][1]) for a rotary encoder and changed it in a way that I can control the volume of my PC with a Raspberry Pi Pico as HID.

#
# SPDX-License-Identifier: MIT

import rotaryio
import board
from adafruit_hid.keycode import Keycode
from adafruit_hid.consumer_control_code import ConsumerControlCode

encoder = rotaryio.IncrementalEncoder(board.GP12, board.GP13)
last_position = None

while True:
    position = encoder.position
    
    if last_position is None or position != last_position:
        if position > last_position:
            cc.send(ConsumerControlCode.VOLUME_INCREMENT)
            time.sleep(0.01)
            print("Volume up")
        elif position < last_position:
            cc.send(ConsumerControlCode.VOLUME_DECREMENT)
            time.sleep(0.01)
            print("Volume down")
        else:
            print("No change")
            
    last_position = position


Unfortunately I get the above mentioned error. I found a similar error here on stackoverflow but it couldn't solve the problem for me.

[1]: https://docs.circuitpython.org/en/latest/shared-bindings/rotaryio/index.html
3
  • It means last_position is None, and 42 > None makes no sense. You need to handle that case differently and avoid such comparisons. Commented Jan 24 at 8:01
  • ok, I understand but why is: if last_position is None or position != last_position valid? Isn't it the same problem here? Commented Jan 24 at 8:05
  • No. You can tell that None is not the same as 42. Easy. But can you tell whether None is larger or smaller than 42? Mu. Commented Jan 24 at 8:20

1 Answer 1

0

Setting last_position to 0 solved the problem.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.