from machine import Pin, PWM
from utime import sleep, ticks_ms
SPEAKER_PIN = 22 # pasa a través de un altavoz y conecta el otro extremo a GND
speaker = PWM(Pin(SPEAKER_PIN))
builtin_led = Pin(25, Pin.OUT)
# Conectar estos pines GP a través de un botón al riel de 3.3 voltios
button_pin_1 = Pin(13, Pin.IN, Pin.PULL_DOWN)
button_pin_2 = Pin(14, Pin.IN, Pin.PULL_DOWN)
button_pin_3 = Pin(15, Pin.IN, Pin.PULL_DOWN)
button_pin_4 = Pin(16, Pin.IN, Pin.PULL_DOWN)
button_pin_5 = Pin(17, Pin.IN, Pin.PULL_DOWN)
# Constantes de frecuencia de notas musicales
NOTE_C3 = 131
NOTE_CS3 = 139
NOTE_D3 = 147
NOTE_DS3 = 156
NOTE_E3 = 165
NOTE_F3 = 175
NOTE_FS3 = 185
NOTE_G3 = 196
NOTE_GS3 = 208
NOTE_A3 = 220
NOTE_AS3 = 233
NOTE_B3 = 247
NOTE_C4 = 262
NOTE_CS4 = 277
NOTE_D4 = 294
NOTE_DS4 = 311
NOTE_E4 = 330
NOTE_F4 = 349
NOTE_FS4 = 370
NOTE_G4 = 392
NOTE_GS4 = 415
NOTE_A4 = 440
NOTE_AS4 = 466
NOTE_B4 = 494
NOTE_C5 = 523
NOTE_CS5 = 554
NOTE_D5 = 587
NOTE_DS5 = 622
NOTE_E5 = 659
NOTE_F5 = 698
NOTE_FS5 = 740
NOTE_G5 = 784
NOTE_GS5 = 831
NOTE_A5 = 880
NOTE_AS5 = 932
NOTE_B5 = 988
NOTE_C6 = 1047
NOTE_CS6 = 1109
NOTE_D6 = 1175
NOTE_DS6 = 1245
NOTE_E6 = 1319
NOTE_F6 = 1397
NOTE_FS6 = 1480
NOTE_G6 = 1568
NOTE_GS6 = 1661
NOTE_A6 = 1760
NOTE_AS6 = 1865
NOTE_B6 = 1976
NOTE_C7 = 2093
NOTE_CS7 = 2217
NOTE_D7 = 2349
NOTE_DS7 = 2489
NOTE_E7 = 2637
NOTE_F7 = 2794
NOTE_FS7 = 2960
NOTE_G7 = 3136
NOTE_GS7 = 3322
NOTE_A7 = 3520
NOTE_AS7 = 3729
NOTE_B7 = 3951
NOTE_C8 = 4186
NOTE_CS8 = 4435
NOTE_D8 = 4699
NOTE_DS8 = 4978
REST = 0
def playtone(frequency):
speaker.duty_u16(1000) # ajustar el ciclo de trabajo PWM al 50%
speaker.freq(frequency)
def bequiet():
speaker.duty_u16(0) # apagar el PWM del altavoz
while True:
if button_pin_1.value() == 1:
playtone(587) # D5
elif button_pin_2.value() == 1:
playtone(494) # B4
elif button_pin_3.value() == 1:
playtone(440) # A4
elif button_pin_4.value() == 1:
playtone(349) # f4
elif button_pin_5.value() == 1:
playtone(294) # D4
else:
bequiet()