import random
from machine import Pin
from utime import sleep
# initialize leds
led = [1, 2, 3, 4, 5, 6, 7, 8]
g = Pin(4, Pin.OUT)
r = Pin(5, Pin.OUT)
# add port nos. according to your model
port = [6, 7, 8, 9, 10, 11, 12, 13]
# implement Pin function to your leds to make them blinkable
def conf_port():
for i in range (8):
led[i] = Pin(port[i], Pin.OUT)
# recieve one digit and convert to binary, returns the list
def dec_2_bin(n):
s = [0, 0, 0, 0, 0, 0, 0, 0]
for i in range (len(s)):
s[len(s)-1-i] = n % 2
n //= 2
return s
def is_prime(n):
for i in range (2, n):
if i % 2 == 0:
return False
return True
# call this to make leds start working
conf_port()
while True:
# generate random number
num = random.randint(0, 255)
# convert that number to binary
s = dec_2_bin(num)
# using loop turn on leds according to values in list "s"
for i in range (8):
led[i].value(s[i])
print(num,":",s)
if is_prime(num):
g.on()
sleep(1)
g.off()
else:
r.on()
sleep(1)
r.off()
sleep(4)