from machine import Pin, PWM
import time
from time import sleep_ms
class BUZZER:
def __init__(self, sig_pin):
self.pwm = PWM(Pin(sig_pin, Pin.OUT))
def play(self, melodies, wait, duty):
for note in melodies:
if note == 0:
self.pwm.duty(0)
else:
self.pwm.freq(note)
self.pwm.duty(duty)
sleep_ms(wait)
self.pwm.duty(0)
#notes and its equivalent frequency
A4 = 400
CS5 = 550
E5 = 660
A5 = 880
pattern = [A4, CS5, E5, A5, A5, E5, CS5, A4]
b = BUZZER(23)
wait = 500
b.play(pattern, wait, 512)
btn1 = Pin(14, Pin.IN, Pin.PULL_UP)
while True:
if btn1.value() == 0:
if wait==500:
wait = 250
else:
wait = 500
b.play(pattern, wait, 512)
time.sleep_ms(100)