from machine import Pin import time # Initialize the horn on pin 5 horn = Pin(5, Pin.OUT) # Define the notes and their corresponding durations (in seconds) notes = { 'C': 0.5, 'D': 0.5, 'E': 0.5, 'F': 0.5, 'G': 0.5, 'A': 0.5, 'B': 0.5, 'C5': 0.5 } # Define the melody for "Happy Birthday" melody = [ 'C', 'C', 'D', 'C', 'F', 'E', 'C', 'C', 'D', 'C', 'G', 'F', 'C', 'C', 'C5', 'A', 'F', 'E', 'D', 'A#', 'A#', 'A', 'F', 'G', 'F' ] # Define the duration of each note (in seconds) durations = [ 0.5, 0.5, 1, 1, 1, 2, 0.5, 0.5, 1, 1, 1, 2, 0.5, 0.5, 1, 1, 1, 1, 2, 0.5, 0.5, 1, 1, 1, 2 ] # Function to play a note def play_tone(note, duration): horn.on() time.sleep(duration) horn.off() time.sleep(0.1) # Short pause between notes # Play the melody in an infinite loop while True: for note, duration in zip(melody, durations): play_tone(note, duration)