from machine import Pin, PWM
from time import sleep
# Buzzer pin
buzzer = PWM(Pin(15))
# Mario melody notes (frequency in Hz)
MELODY = [
(659, 0.15), # E5
(659, 0.15),
(0, 0.15),
(659, 0.15),
(0, 0.15),
(523, 0.15), # C5
(659, 0.15),
(0, 0.15),
(784, 0.15), # G5
(0, 0.45),
(392, 0.15), # G4
(0, 0.45),
(523, 0.15),
(392, 0.15),
(330, 0.15),
(440, 0.15),
(494, 0.15),
(466, 0.15),
(440, 0.15),
(392, 0.15),
(659, 0.15),
(784, 0.15),
(880, 0.15),
(698, 0.15),
(784, 0.15),
(0, 0.15),
(659, 0.15),
(523, 0.15),
(587, 0.15),
(494, 0.15),
]
def play_tone(freq, duration):
if freq == 0:
buzzer.duty_u16(0)
else:
buzzer.freq(freq)
buzzer.duty_u16(30000) # Volume
sleep(duration)
buzzer.duty_u16(0)
sleep(0.02)
# Play melody
while True:
for note, duration in MELODY:
play_tone(note, duration)
sleep(2)