from machine import Pin
import urandom
import time
# 7-segment pins: a b c d e f g
segments = [
Pin(2, Pin.OUT), # a
Pin(3, Pin.OUT), # b
Pin(4, Pin.OUT), # c
Pin(5, Pin.OUT), # d
Pin(6, Pin.OUT), # e
Pin(7, Pin.OUT), # f
Pin(8, Pin.OUT) # g
]
button = Pin(16, Pin.IN, Pin.PULL_UP)
# Correct dice number patterns (COMMON CATHODE)
dice = [
[0,1,1,0,0,0,0], # 1 -> b, c
[1,1,0,1,1,0,1], # 2
[1,1,1,1,0,0,1], # 3
[0,1,1,0,0,1,1], # 4
[1,0,1,1,0,1,1], # 5
[1,0,1,1,1,1,1] # 6
]
while True:
if button.value() == 0:
num = urandom.randint(0, 5)
for i in range(7):
segments[i].value(dice[num][i])
print("Dice:", num + 1)
time.sleep(0.4) # debounce