from microbit import * # program to test out a rotary encoder # when you rotate the knob the LED display will follow :-) # Jonathan Hare March 2017 # see www.creative-science.org.uk/bbcmicrobit.html # port 19 and port 20 for the A and B encoder connections # port 16 for the encoder push switch connection # also 0 and +3V to encoder # note extra pull-up resistors (e.g. 1k) required # program shows latest value of 'count' # which can be adjusted using the rotary encoder # pressing the encoder switch sets the counter to 5 aa = 0 bb = 0 vala = 0 valb = 0 count = 5 while True: aa = pin19.read_digital() if aa != vala: bb = pin20.read_digital() if bb != vala: count = count - 1 else: count = count + 1 if count < 0: count = 0 if count >9: count = 9 display.show(str(count)) vala = aa valb = bb z = pin16.read_digital() if z == 0: count = 5