I have an Inventor 2040 W with an Encoder Breakout wired up to i2c.
When I turn the knob going from position (count) 5 to 6, it doesn’t register it, it stays at 5.
Then on the way to position 12, it adds one back in? The count goes from 10 to 12 when going from position 11 to 12.
I don’t think its my code?
import time
from pimoroni_i2c import PimoroniI2C
from breakout_encoder import BreakoutEncoder
i2c = PimoroniI2C(sda=(4), scl=(5))
import ssd1306
steps_per_rev = 24
enc = BreakoutEncoder(i2c)
enc.set_brightness(1.0)
display = ssd1306.SSD1306_I2C(128, 32, i2c)
# enc.set_direction(BreakoutEncoder.DIRECTION_CCW) # Uncomment this to flip the direction
# From CPython Lib/colorsys.py
def hsv_to_rgb(h, s, v):
if s == 0.0:
return v, v, v
i = int(h * 6.0)
f = (h * 6.0) - i
p = v * (1.0 - s)
q = v * (1.0 - s * f)
t = v * (1.0 - s * (1.0 - f))
i = i % 6
if i == 0:
return v, t, p
if i == 1:
return q, v, p
if i == 2:
return p, v, t
if i == 3:
return p, q, v
if i == 4:
return t, p, v
if i == 5:
return v, p, q
def count_changed(count):
print("Count: ", count, sep="")
h = ((count % steps_per_rev) * 360.0) / steps_per_rev # Convert the count to a colour hue
r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] # rainbow magic
enc.set_led(r, g, b)
display.fill(0)
display.show()
display.text("Encoder", 0, 0, 1)
display.text("{:3.0f}" .format(count), 75, 0, 1)
display.text("Position:", 0, 20, 1)
display.text("{:3.0f}" .format(count * 15), 75, 20, 1)
display.show()
count = 0
count_changed(count)
enc.clear_interrupt_flag()
display.fill(0)
display.show()
display.text("Encoder", 0, 0, 1)
display.text("{:3.0f}" .format(count), 75, 0, 1)
display.text("Position:", 0, 20, 1)
display.text("{:3.0f}" .format(count * 15), 75, 20, 1)
display.show()
while True:
if enc.get_interrupt_flag():
count = enc.read()
enc.clear_interrupt_flag()
while count < 0:
count += steps_per_rev
count_changed(count)
'''
display.fill(0)
display.show()
display.text("Encoder: ", 0, 0, 1)
#display.text(count, sep"", 0, 16, 1)
display.text("{:0.0f}" .format(count), 64, 0, 1)
#graphics.text("{:0.0f}°C" .format(temperature), 0, 2, scale=1)
#print("Count: ", count, sep="")
display.show()
time.sleep(1)
'''
Here is a link to a video that shows what’s happening.
It’s my Public OneDrive.