from machine import Pin # Import Pin library for GPIO programming
from machine import PWM # Import PWM library for PWM programming
from utime import sleep_ms
from tones import tones # To get frequencies for various musical notes
# GPIO pin the buzzer is connected to
BUZZER_PIN = 15
# Exercise 1.1: FILL CODE HERE to use PWM() to enable PWM on GPIO output pin: BUZZER_PIN
buzzer = PWM(Pin(BUZZER_PIN, Pin.OUT))
# A list of musical notes for the song, where tuple represents the note and its duration in ms
song = [
('C4', 400), ('E4', 200), ('G4', 400), ('A4', 400),
('G4', 400), ('E4', 200), ('D4', 400), ('C4', 400),
('G4', 400), ('A4', 200), ('C5', 400), ('E5', 400),
('D5', 400), ('C5', 200), ('A4', 400), ('G4', 400),
('C4', 400), ('E4', 200), ('G4', 400), ('A4', 400),
('G4', 400), ('E4', 200), ('D4', 400), ('C4', 400),
]
# Programs the PWM parameters: duty cycle and frequency for the given musical note
def playtone(frequency, duration):
# Uncomment below to debug
#print("Playing tone:", frequency)
# The duty cycle is adjusted based on the frequency
# to maintain a balance between pitch and loudness.
buzzer.duty_u16(int(frequency))
# Exercise 1.2: FILL CODE HERE use PWM's freq API to set the frequency
buzzer.freq(frequency)
# Use sleep as a nop to play the note for the specified duration
sleep_ms(duration)
# Turn off the buzzer by programming the duty cycle
def bequiet():
# Uncomment below to debug
# print("Be quiet")
# Exercise 1.3: FILL CODE HERE: Use duty_u16() API to turn off Buzzer
buzzer.duty_u16(0)
# Play the melody
def playsong(mysong):
print("Playing song")
for note, duration in mysong:
if (note == "P"):
bequiet()
else:
playtone(tones[note], duration)
bequiet()
# Exercise 1: Enable the below code to play the song on every run
playsong(song)
# #Exercise 2: Timer based alarm
# from machine import Timer
# def interruption_handler(timer):
# playsong(song)
# # Make sure the period timer is beyond the length of the song
# # Switch b/w one-shot or periodic
# if __name__ == "__main__":
# periodic_timer = Timer(mode=Timer.PERIODIC, period=8000, callback=interruption_handler)
# one_shot_timer = Timer(mode=Timer.ONE_SHOT, period=5000, callback=interruption_handler)