# from machine import Pin
# import utime
# import random
# PORT = [7, 6, 5, 4, 3, 2, 1, 0] # port connections
# DICE_NO = [0, 0x08, 0x22, 0x2A, 0x55, 0x5D, 0x77]
# L = [0]*8
# Button = Pin(15, Pin.IN)
# # #
# # # This function configures the LED ports as outputs
# # #
# def Configure_Port():
# for i in range(0, 8):
# L[i] = Pin(PORT[i], Pin.OUT)
# # #
# # # This function sends 8-bit data (0 to 255) to the PORT
# # #
# def Port_Output(x):
# b = bin(x) # convert into binary
# b = b.replace("0b", "") # remove leading "0b"
# diff = 8 - len(b) # find the length
# for i in range (0, diff):
# b = "0" + b # insert leading os
# for i in range (0, 8):
# if b[i] == "1":
# L[i].value(1)
# else:
# L[i].value(0)
# return
# # #
# # # The program jumps here after the button is pressed
# # #
# def DICE():
# n = random.randint(1, 6) # generate a random number
# pattern = DICE_NO[n] # find the pattern
# Port_Output(pattern) # turn ON required LEDs
# utime.sleep(0.5) # wait for 3 seconds
# Port_Output(0) # turn OFF all LEDs
# return
# # #
# # # Configure PORT to all outputs
# # #
# Configure_Port()
# # #
# # # Main program loop, check if Button is pressed
# # #
# while True:
# if Button.value() == 0: # Button pressed?
# DICE() # Call DICE
# pass # Do nothing
# SAME PROGRAM TRIED IN OTHER WAY
# from machine import Pin
# from utime import sleep
# import random
# LED1=Pin(0,Pin.OUT)
# LED2=Pin(1,Pin.OUT)
# LED3=Pin(2,Pin.OUT)
# LED4=Pin(3,Pin.OUT)
# LED5=Pin(4,Pin.OUT)
# LED6=Pin(5,Pin.OUT)
# LED7=Pin(6,Pin.OUT)
# button=Pin(28,Pin.IN,Pin.PULL_DOWN)
# Dice=[0X00,0X08,0X22,0X2A,0X55,0X5D,0X77]
# def db(hexanumber):
# binaryString='{0:07b}'.format(hexanumber)
# LED1.value(int(binaryString[0]))
# LED2.value(int(binaryString[1]))
# LED3.value(int(binaryString[2]))
# LED4.value(int(binaryString[3]))
# LED5.value(int(binaryString[4]))
# LED6.value(int(binaryString[5]))
# LED7.value(int(binaryString[6]))
# while True:
# if button.value()==1:
# number=random.randint(1,6)
# num1=Dice[number]
# db(num1)
# print(number,'is blinking')
# sleep(0.5)
#another method
from machine import Pin
from utime import sleep
import random
LED1=Pin(0, Pin.OUT)
LED2=Pin(1, Pin.OUT)
LED3=Pin(2, Pin.OUT)
LED4=Pin(3, Pin.OUT)
LED5=Pin(4, Pin.OUT)
LED6=Pin(5, Pin.OUT)
LED7=Pin(6, Pin.OUT)
button=Pin(15, Pin.IN, Pin.PULL_DOWN)
Dice=[0X00,0X08,0X22,0X2A,0X55,0X5D,0X77]
def db(hexanumber):
binaryString='{0:07b}'.format(hexanumber)
LED1.value(int(binaryString[0]))
LED2.value(int(binaryString[1]))
LED3.value(int(binaryString[2]))
LED4.value(int(binaryString[3]))
LED5.value(int(binaryString[4]))
LED6.value(int(binaryString[5]))
LED7.value(int(binaryString[6]))
while True:
if button.value()==1:
number=random.randint(1,6)
num1=Dice[number]
db(num1)
print("Number on the dice is: ", number)
sleep(1)