from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import time
import random
button = Pin(15, Pin.IN, Pin.PULL_UP)
count=1
dice=0
WIDTH =128
HEIGHT= 64
#Create the I2C connection
i2c=I2C(0,scl=Pin(17),sda=Pin(16),freq=200000)
#Initialize the OLED display
oled = SSD1306_I2C(WIDTH,HEIGHT,i2c)
while True:
if button.value() == 0:
dice=random.randint(1,6)
print("Dice: ",dice)
print("Button pressed Count is : ",count)
count+=1
while button.value() == 0:
time.sleep(0.1)
dc=str(dice)
x= (WIDTH-(len(dc)*8))//2
y= (HEIGHT-8)//2
oled.fill(0)
#oled.text(dc,x,y)
#define outline square of the dice
oled.hline(35, 2, 60, 1)
oled.vline(35, 2, 60, 1)
oled.vline(95, 2, 60, 1)
oled.hline(35, 62, 60, 1)
if dc == "1":
oled.fill_rect(2, 2, 8, 8, 1)
elif dc == "2":
oled.fill_rect(10, 2, 8, 8, 1)
elif dc == "3":
oled.fill_rect(20, 8, 8, 8, 1)
elif dc == "4":
oled.fill_rect(30, 8, 8, 8, 1)
elif dc == "5":
oled.fill_rect(40, 4, 8, 8, 1)
elif dc == "6":
oled.fill_rect(50, 10, 8, 8, 1)
else:
oled.fill(0)
oled.text(ERROR,x,y)
#oled.fill_rect(x, y, 8, 8, 1)
#oled.fill_rect(37, 4, 8, 8, 1)
oled.show()
time.sleep(2)
oled.fill(0)
oled.show()