from microbit import * # program to test out a rotary encoder # when you rotate the knob the LEDs 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 # also 0 and +3V to encoder # note may need extra pull-up resistors (e.g. 1k) # led dot definitions a = Image("00900:00000:00000:00000:00000") b = Image("00090:00000:00000:00000:00000") c = Image("00009:00000:00000:00000:00000") d = Image("00000:00009:00000:00000:00000") e = Image("00000:00000:00009:00000:00000") f = Image("00000:00000:00000:00009:00000") g = Image("00000:00000:00000:00000:00009") h = Image("00000:00000:00000:00000:00090") i = Image("00000:00000:00000:00000:00900") j = Image("00000:00000:00000:00000:09000") k = Image("00000:00000:00000:00000:90000") l = Image("00000:00000:00000:90000:00000") m = Image("00000:00000:90000:00000:00000") n = Image("00000:90000:00000:00000:00000") o = Image("90000:00000:00000:00000:00000") p = Image("09000:00000:00000:00000:00000") aa = 0 bb = 0 vala = 0 valb = 0 count = 8 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 >16: count = 16 if count == 0: display.show(a) if count == 1: display.show(b) if count == 2: display.show(c) if count == 3: display.show(d) if count == 4: display.show(e) if count == 5: display.show(f) if count == 6: display.show(g) if count == 7: display.show(h) if count == 8: display.show(i) if count == 9: display.show(j) if count == 10: display.show(k) if count == 11: display.show(l) if count == 12: display.show(m) if count == 13: display.show(n) if count == 14: display.show(o) if count == 15: display.show(p) if count == 16: display.show(a) vala = aa valb = bb