from machine import Pin, PWM
import time
buzzer_pin = Pin(11)
buzzer_pwm = PWM(buzzer_pin)
melody = [
392, 523, 392, 440, 494, 330, 330,
440, 392, 349, 392, 523, 523,
294, 294, 330, 349, 349, 392, 440, 494, 523, 587,
523, 494, 523, 494, 440, 392,
523, 494, 440, 494, 330, 330,
440, 392, 349, 392, 523, 523,
523, 494, 440, 392, 494, 523, 587,
523, 494, 440, 392, 494, 523, 392, 392, 494, 523, 587,
523, 494, 440, 392, 440, 494, 330, 330, 494, 523, 587,
523, 494, 440, 494, 523, 587, 494, 523, 523, 698,
698, 659, 587, 523, 587, 659, 523, 523,
587, 523, 494, 440, 494, 523, 440, 440,
587, 523, 494, 440, 523, 440, 494, 523, 587
]
note_durations = [
8, 4, 6, 16, 4, 8, 8,
4, 6, 16, 4, 8, 8,
4, 8, 8, 4, 8, 8, 4, 8, 8, 2,
4, 6, 16, 4, 8, 8,
4, 6, 16, 4, 8, 8,
4, 6, 16, 4, 6, 16,
4, 6, 16, 8, 8, 8, 8,
2, 8, 8, 8, 8, 3, 8, 8, 8, 8, 8,
2, 8, 8, 8, 8, 3, 8, 8, 8, 8, 8,
4, 6, 16, 4, 6, 16, 4, 8, 8, 2,
2, 8, 8, 8, 8, 3, 8, 2,
2, 8, 8, 8, 8, 3, 8, 2,
4, 6, 16, 4, 4, 2, 4, 4, 1
]
def play_tone(frequency, duration):
if frequency > 0:
buzzer_pwm.freq(frequency)
buzzer_pwm.duty_u16(32768) # 50% duty cycle
time.sleep_ms(duration)
buzzer_pwm.duty_u16(0) # turn off the buzzer
def setup():
for i in range(len(melody)):
note_duration = 1000 // note_durations[i] # Tempo reduzido pela metade
play_tone(melody[i], note_duration)
pause_between_notes = int(note_duration * 1.30)
time.sleep_ms(pause_between_notes)
setup()