from machine import Pin, PWM
from utime import sleep
import machine
from random import randrange
print("started!")
# sleep time
st = 0.4
leds = [15,14,13,12,27,26,25]
button = Pin(18, Pin.IN)
# defining buzzer !!SET DUTY TO 0!!
buzzer = PWM(Pin(33, Pin.IN), duty = 0)
# piezo = Pin(33, Pin.OUT)
# dict with positions of leds in the "leds" list
numbers = {
# need at least 2 or python gets angry
1:(3,3),
2:(0,6),
3:(0,3,6),
4:(0,2,4,6),
5:(0,2,3,4,6),
6:(0,1,2,4,5,6,)
}
def roll(rolls, on_off = 1):
# choose a tuple from numbers
chosen = numbers[rolls]
for i in range(rolls):
led = Pin(leds[chosen[i]], Pin.OUT)
led.value(on_off)
# turn off all leds
def off(led_num = len(leds)):
for i in range(led_num):
led = Pin(leds[i], Pin.OUT)
led.value(0)
while True:
if button.value() == True:
# clear previous number
off()
for i in range(6):
rand = randrange(1,7)
roll(rand)
# the actualy freq getting played
print(50 * rand)
# sets note frequency according to chosen number
buzzer.freq(50 * rand)
# turns buzzer on
buzzer.duty(1)
sleep(st)
buzzer.duty(0)
off()
# wait again so the sound doesn't deafen you
sleep(0.1)
# rolls final number
rand = randrange(1,7)
roll(rand)
# play a jingle :)
buzzer.duty(1)
for i in range(1,5):
buzzer.freq(200*i)
sleep(0.5/i)
buzzer.duty(0)
print(f"rolled {rand}")
# unnecessary sleep?
sleep(2)
continue