I had a 10k potentiometer lying around that I saved from some junked electronics it may have been a volume control or something, I can’t remember. Today I found a use for it when I found this excellent web site called Microbit Playground . This shows you how you can hook a pot straight up to a microbit and take readings with a bit of python. You connect the centre pin of the potentiometer to microbit pin 0, and the other 2 pins are connected to GND and 3v on the microbit. I just used some crocodile clip leads.
I found the code to change the brightness of a dot on the screen needed an int() adding to line 5 so it reads
brightness = int((pin0.read_analog() / 1023) * 9)I decided to play around with this and did the following:
First I made the display get dimmer or brighter depending on the position of the potentiometer:
Then I made a level graphic go up or down depending on its position in 10 increments:
Next I made a phone-style wedge that goes up & down when you wiggle the knob (just a little bit).
Finally with just ONE line of Python I added an analogue paddle controller to my totes awesomeKettle Run game to make… PotKettleRun! (See what I did there? I’m here all week.)
All Python code at the end of this post. If you make a 2-player Pong game with real physical paddles before me, let me know!
All the Code that’s Fit to PrintBrighter/dimmer screen:
from microbit import * img0 = Image("00000:" "00000:" "00000:" "00000:" "00000") img1 = Image("11111:" "11111:" "11111:" "11111:" "11111") img2 = Image("22222:" "22222:" "22222:" "22222:" "22222") img3 = Image("33333:" "33333:" "33333:" "33333:" "33333") img4 = Image("44444:" "44444:" "44444:" "44444:" "44444") img5 = Image("55555:" "55555:" "55555:" "55555:" "55555") img6 = Image("66666:" "66666:" "66666:" "66666:" "66666") img7 = Image("77777:" "77777:" "77777:" "77777:" "77777") img8 = Image("88888:" "88888:" "88888:" "88888:" "88888") img9 = Image("99999:" "99999:" "99999:" "99999:" "99999") img_list = [img0,img1,img2,img3,img4,img5,img6,img7,img8,img9] while True: brightness = int((pin0.read_analog() / 1023) * 9) display.show(img_list[brightness]) sleep(10)Levels going up & down
from microbit import * img0 = Image("00000:" "00000:" "00000:" "00000:" "00000") img1 = Image("00000:" "00000:" "00000:" "00000:" "55555") img2 = Image("00000:" "00000:" "00000:" "00000:" "99999") img3 = Image("00000:" "00000:" "00000:" "55555:" "99999") img4 = Image("00000:" "00000:" "00000:" "99999:" "99999") img5 = Image("00000:" "00000:" "55555:" "99999:" "99999") img5 = Image("00000:" "00000:" "99999:" "99999:" "99999") img6 = Image("00000:" "55555:" "99999:" "99999:" "99999") img7 = Image("00000:" "99999:" "99999:" "99999:" "99999") img8 = Image("55555:" "99999:" "99999:" "99999:" "99999") img9 = Image("99999:" "99999:" "99999:" "99999:" "99999") img_list = [img0,img1,img2,img3,img4,img5,img6,img7,img8,img9] while True: brightness = int((pin0.read_analog() / 1023) * 10) display.show(img_list[brightness]) sleep(10)Signal-strength type wedge display:
from microbit import * img0 = Image("00000:" "00000:" "00000:" "00000:" "00000") img1 = Image("00000:" "00000:" "00000:" "00000:" "50000") img2 = Image("00000:" "00000:" "00000:" "00000:" "90000") img3 = Image("00000:" "00000:" "00000:" "05000:" "95000") img4 = Image("00000:" "00000:" "00000:" "09000:" "99000") img5 = Image("00000:" "00000:" "00500:" "09500:" "99500") img5 = Image("00000:" "00000:" "00900:" "09900:" "99900") img6 = Image("00000:" "00050:" "00950:" "09950:" "99950") img7 = Image("00000:" "00090:" "00990:" "09990:" "99990") img8 = Image("00005:" "00095:" "00995:" "09995:" "99995") img9 = Image("00009:" "00099:" "00999:" "09999:" "99999") img_list = [img0,img1,img2,img3,img4,img5,img6,img7,img8,img9] while True: brightness = int((pin0.read_analog() / 1023) * 10) display.show(img_list[brightness]) sleep(10)PotKettleRun game
# PotKettleRun: guide your ship using a potentiometer # Guide your ship to home port and put the kettle on. # Can you do the Kettle Run in less than 12 parsecs? # Sound commands & buttons from original game commented out from microbit import * import random #from music import play, NYAN, POWER_UP, FUNERAL, JUMP_UP, JUMP_DOWN # edit terrain blocks to make game harder teacup1 = Image("00000:" "99999:" "99909:" "99999:" "99900") teacup2 = Image("05000:" "99999:" "99909:" "99999:" "99900") teacup3 = Image("00500:" "99999:" "99909:" "99999:" "99900") def teacup_animate(times): for x in range(times): display.show(teacup1) sleep(500) display.show(teacup2) sleep(500) display.show(teacup3) sleep(500) def build_terrain(length): canyon_lines=["00000:00005:00055:00555:00055:00005:", "50000:55000:55500:55550:55000:", "50005:55005:50055:50005:00005:", "00055:00555:00005:55000:55005:50005:"] terrainString = "00000:00000:00000:00000:" for i in range(length): z = random.randint(0,3) terrainString = terrainString + canyon_lines[z] terrainString = terrainString + "50005:55055" terrainImage = Image(terrainString) return terrainImage def game_run(): global level global crash global speed terrain = build_terrain(2+(level*2)) image_height = terrain.height() ship_height = 4 for i in range(image_height): ship_height = int((pin0.read_analog() / 1023) * 5) # if button_a.was_pressed(): # play(JUMP_UP, wait=False) # ship_height -= 1 # if ship_height < 0: # ship_height = 0 # if button_b.was_pressed(): # play(JUMP_DOWN, wait=False) # ship_height += 1 # if ship_height > 4: # ship_height = 4 if terrain.get_pixel(ship_height, 0) == 5: crash = True break terrain.set_pixel(ship_height, 0, 9) display.show(terrain) sleep(speed) terrain = terrain.shift_up(1) speed = 500 crash = False won = False level = 1 display.scroll('Kettle Run') while level < 6: # increase this number to add more levels display.show(str(level)) sleep(1000) game_run() if crash: # play(FUNERAL, wait=False) break else: # play(POWER_UP, wait=False) teacup_animate(2) display.scroll('Tea up!') level += 1 speed -= 20 # increase this number to speed up more on each level if not crash: # play(NYAN, wait=False) display.show('YOU WIN') sleep(1000) else: display.show('GAME OVER') sleep(1000) while True: display.scroll('Press reset to play again')