from machine import Pin, PWM from time import sleep_ms from music import Music class Song: def __init__(self, sig_pin): self.buzzer = PWM(Pin(sig_pin), duty_u16=0) self.tones = Music.tones() def test(): # Create a list of notes for your song. Use the letter P to represent pauses in the music return ["E5","G5","A5","P","E5","G5","B5","A5","P","E5","G5","A5","P","G5","E5"] # Function called playtone that will take any frequency and play it at full volume def playtone(self, frequency, duty): self.buzzer.duty_u16(duty) self.buzzer.freq(frequency) # Function called bequiet that will silence the buzzer by change duty_u16 to 0 def bequiet(self): self.buzzer.duty_u16(0) def play_song(self, song, wait=150, duty=32767): for i in range(len(song)): if (song[i] == "P"): self.bequiet() else: self.playtone(self.tones[song[i]], duty) sleep_ms(wait) self.bequiet() def play_song_test(self): song = ["E5","G5","A5","P","E5","G5","B5","A5","P","E5","G5","A5","P","G5","E5"] self.play_song(song) def play_song_jingle(self): song = [ 'E7', 'E7', 'E7', "P", 'E7', 'E7', 'E7', "P", 'E7', 'G7', 'C7', 'D7', 'E7', "P", 'F7', 'F7', 'F7', 'F7', 'F7', 'E7', 'E7', 'E7', 'E7', 'D7', 'D7', 'E7', 'D7', "P", 'G7', "P", 'E7', 'E7', 'E7', "P", 'E7', 'E7', 'E7', "P", 'E7', 'G7', 'C7', 'D7', 'E7', "P", 'F7', 'F7', 'F7', 'F7', 'F7', 'E7', 'E7', 'E7', 'G7', 'G7', 'F7', 'D7', 'C7', "P" ] self.play_song(song) def play_song_mario(self): song = [ 'E7', 'E7', "P", 'E7', "P", 'C7', 'E7', "P", 'G7', "P", "P", "P", 'G6', "P", "P", "P", 'C7', "P", "P", 'G6', "P", "P", 'E6', "P", "P", 'A6', "P", 'B6', "P", 'AS6', 'A6', "P", 'G6', 'E7', "P", 'G7', 'A7', "P", 'F7', 'G7', "P", 'E7', "P", 'C7', 'D7', 'B6', "P", "P", 'C7', "P", "P", 'G6', "P", "P", 'E6', "P", "P", 'A6', "P", 'B6', "P", 'AS6', 'A6', "P", 'G6', 'E7', "P", 'G7', 'A7', "P", 'F7', 'G7', "P", 'E7', "P", 'C7', 'D7', 'B6', "P", "P", ] self.play_song(song)