from machine import *
from utime import *
from neopixel import NeoPixel
np = NeoPixel(Pin(22), 16)
for i in range(16):
np[i] = 255, 0, 0
np.write()
NOTES = ('a', 'a#', 'b', 'c', 'c#', 'd', 'd#', 'e', 'f', 'f#', 'g', 'g#')
buz_pin = 33
def freq(note):
if len(note) == 3:
octave = int(note[2])
else:
octave = int(note[1])
key_no = NOTES.index(note[:-1])
if key_no < 3:
key_no = key_no + 12 + ((octave - 1) * 12) + 1
else:
key_no = key_no + ((octave - 1) * 12) + 1
return 440 * (2 ** ((key_no - 49) / 12))
def tone(pin, freq, time):
buzzer = PWM(Pin(pin), freq=freq, duty=512)
sleep_ms(time)
buzzer.deinit()
song = ['d3', 'd3', 'e3', 'd3', 'f#3']
for note in song:
tone(buz_pin, int(freq(note)), 300)
sleep_ms(100)