import machine
from machine import PWM
import utime
from tones import notes
# Define buzzer pin (connect the buzzer to this GPIO pin)
buzzer_pin = PWM(machine.Pin(15, machine.Pin.OUT))
# Function to play a note on the buzzer
def play_note(note, duration_ms):
buzzer_pin.freq(notes[note]) # Set buzzer frequency
buzzer_pin.duty_u16(1000) # Turn on the buzzer
utime.sleep_ms(duration_ms) # Play the note for the specified duration
buzzer_pin.duty_u16(0) # Turn off the buzzer
# Play a simple melody
def play_melody(melody):
for note, duration in melody:
play_note(note, duration)
# Define the "Happy Birthday" melody (note, duration in milliseconds)
happy_birthday = [
('C4', 400), ('C4', 400), ('D4', 400), ('C4', 800),
('F4', 400), ('E4', 400), ('C4', 800),
('F4', 400), ('E4', 400), ('C4', 800),
('C4', 400), ('C4', 400), ('D4', 400), ('C4', 800),
('G4', 400), ('F4', 400), ('C4', 800),
('G4', 400), ('F4', 400), ('C4', 800)
]
# Define the "Fight Song" melody (note, duration in milliseconds)
fight_song = [
('A4', 500), ('B4', 500), ('C5', 500), ('D5', 1000),
('A4', 500), ('A4', 500), ('B4', 500), ('C5', 1000),
('A4', 500), ('B4', 500), ('D5', 1000),
('D5', 500), ('E5', 500), ('F5', 1000),
('D5', 500), ('C5', 500), ('B4', 500), ('A4', 1000),
('A4', 500), ('A4', 500), ('B4', 500), ('C5', 1000),
('A4', 500), ('B4', 500), ('D5', 1000),
('D5', 500), ('E5', 500), ('F5', 1000),
('D5', 500), ('C5', 500), ('B4', 500), ('A4', 1000),
]
# Play the "Happy Birthday" melody
play_melody(happy_birthday)