from machine import Pin,PWM
from utime import sleep
import neopixel
import random

buzzer = PWM(Pin(33))
buzzer.duty_u16(0)
button = Pin(18,Pin.IN)
RGB_pin = Pin(25, Pin.OUT)
np = neopixel.NeoPixel(RGB_pin, n=4, bpp=3, timing=1)

Tone = [0, 392, 440, 494, 523, 587, 659, 698, 784]        

Song = [ Tone[3], Tone[3], Tone[3], Tone[3], Tone[3], Tone[3],
         Tone[3], Tone[5], Tone[1], Tone[2], Tone[3],
         Tone[4], Tone[4], Tone[4], Tone[4], Tone[4], Tone[3], Tone[3], Tone[3],
         Tone[5], Tone[5], Tone[4], Tone[2], Tone[1], Tone[8] ]


Beat =[0.25, 0.25, 0.5, 0.25, 0.25, 0.5,
        0.25, 0.25, 0.375, 0.125, 1, 0.25,
        0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
        0.25, 0.25, 0.25, 0.5, 0.5];

def setup():
    for i in range(0,4):
        np[i] = (0,0,0)
    np.write()
    
def loop():
    while button.value() == 1:
        pass
    else:
        while True:
            for i in range(0, len(Song)):
                buzzer.duty_u16(2000)
                buzzer.freq(Song[i])
                sleep(Beat[i])
                buzzer.duty_u16(0)
                for n in range(0,4):
                    np[n] = (random.randint(0,256),random.randint(0,256),random.randint(0,256))
                    np.write()
                sleep(0.01)
if __name__ == '__main__':
    setup()
    loop()
