Skip to content

Instantly share code, notes, and snippets.

@lbxa
Created January 8, 2018 01:33
Show Gist options
  • Select an option

  • Save lbxa/f9b02d981036048fd9605750fd66d3b2 to your computer and use it in GitHub Desktop.

Select an option

Save lbxa/f9b02d981036048fd9605750fd66d3b2 to your computer and use it in GitHub Desktop.
'''
Microbit Micropython | Binary Digit LED's
Mini project for NCSS challenge that involved some silly hardcoding
Lucas Barbosa | 8.01.2018 | NCSS Summer School | (C)
'''
from microbit import *
import random
import music
ON = "1"
OFF = "0"
pins = [pin0, pin6, pin7, pin9]
dec_bin = {0: "0000", 1: "1000", 2: "0100", 3: "1100", 4: "0010", 5: "1010",
6: "0110", 7: "1110", 8: "0001", 9: "1001", 10: "0101", 11: "1101",
12: "0011", 13: "1011", 14: "0111", 15: "1111"}
display.off()
while True:
if button_a.was_pressed():
rand_num = random.randint(0, 15)
print(str(rand_num))
for dec, binary in dec_bin.items():
if rand_num == dec:
# turn the binary strings into iterable lists
binary = list(binary)
binary.reverse()
# enumerate to use index for pin output signal
for index, digit in enumerate(binary):
if digit == ON:
pins[index].write_digital(True)
elif digit == OFF:
pins[index].write_digital(False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment