import machine
import utime
button = machine.Pin(15, machine.Pin.IN)
NOTE_C4 = 262
NOTE_G3 = 196
NOTE_A3 = 220
NOTE_B3 = 247
melody = [NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, NOTE_B3, NOTE_C4]
buzzer = machine.PWM(machine.Pin(20))
def tone(pin, frequency, duration):
pin.freq(frequency)
pin.duty_u16(30000)
utime.sleep_ms(duration)
pin.duty_u16(0)
while True:
if button.value() == 1:
print("You pressed the button!")
for note in melody:
tone(buzzer, note, 250)
utime.sleep_ms(150)