from machine import Pin, PWM, Timer
from utime import sleep
from tones import tones
buzzer = PWM(Pin(15))
button = Pin(16, Pin.IN, Pin.PULL_DOWN)
song = ["E5","G5","A5","P","E5","G5","B5","A5","P","E5","G5","A5","P","G5","E5"]
def playtone(frequency):
print("Playing tone:", frequency) # Add this line
# Exercise 1 : Duty cycle
buzzer.duty_u16(100)
buzzer.freq(frequency)
def bequiet():
print("Be quiet")
# Exercise 1 : Duty cycle
buzzer.duty_u16(0)
def playsong(mysong):
print("Playing song")
for i in range(len(mysong)):
if (mysong[i] == "P"):
bequiet()
else:
playtone(tones[mysong[i]])
sleep(0.3)
bequiet()
# Exercise 2 : Control through GPIO IN
while True:
button_state = button.value()
if button_state == 1:
print("Button is pressed (ON)")
playsong(song)
else:
print("Button is released (OFF)")
bequiet()
# Exercise 3 : Timer based alarm
# def interruption_handler(timer):
# playsong(song)
# # Figure out the period based on the length of the song
# # Switch b/w one-shot or periodic
# if __name__ == "__main__":
# soft_timer = Timer(mode=Timer.PERIODIC, period=5000, callback=interruption_handler)