I had a quick go at reading a rotary encoder that I pulled out of a cheap mouse, with the center pin at +3v and the 2 outer pins to ext0 and ext1. Using the following python code, I can’t seem to read the inputs correctly.
# Welcome to Python on Pokitto! from umachine import * import upygame as upg import umachine as pok # Setup the screen. screen = upg.display.set_mode() pin0 = Pin( Pin.EXT0, Pin.IN ) pin1 = Pin( Pin.EXT1, Pin.IN ) olda = 0 a=0 b=0 x=0 while True: a = pin0.value() b = pin1.value() if olda != a: if b == 1: x+=1 else: x-=1 olda = a # Render screen screen.fill(0) pok.draw_text(0, 14, str(a), 1) pok.draw_text(0, 21, str(b), 1) pok.draw_text(x, 35, "=", 1) upg.display.flip()
I expected the inputs to be something like the following - 00,10,11,01,00,10,11,01 etc. however I’m only getting 00,10,11,00,10,11. It seems to be missing part of the rotation.
Has anyone doe this sort of thing before? Any clues as to if I’m reading the inputs correctly or not?