from machine import Pin, PWM
import time
# --- CONFIGURACION LCD 16x2 ---
rs = Pin(0, Pin.OUT)
en = Pin(1, Pin.OUT)
d4 = Pin(2, Pin.OUT)
d5 = Pin(3, Pin.OUT)
d6 = Pin(4, Pin.OUT)
d7 = Pin(5, Pin.OUT)
def lcd_nibble(val):
d4.value((val >> 0) & 1)
d5.value((val >> 1) & 1)
d6.value((val >> 2) & 1)
d7.value((val >> 3) & 1)
en.value(1)
time.sleep_us(40)
en.value(0)
time.sleep_us(40)
def lcd_byte(val, mode):
rs.value(mode)
lcd_nibble(val >> 4)
lcd_nibble(val & 0x0F)
def lcd_init():
time.sleep_ms(50)
for _ in range(3):
lcd_nibble(0x03)
time.sleep_ms(5)
lcd_nibble(0x02)
lcd_byte(0x28, 0)
lcd_byte(0x0C, 0)
lcd_byte(0x06, 0)
lcd_clear()
def lcd_clear():
lcd_byte(0x01, 0)
time.sleep_ms(2)
def lcd_pos(col, row):
addr = 0x80 + col if row == 0 else 0xC0 + col
lcd_byte(addr, 0)
def lcd_print(texto):
for c in str(texto):
lcd_byte(ord(c), 1)
# --- HARDWARE ---
buzzer = PWM(Pin(6))
btn_play = Pin(7, Pin.IN, Pin.PULL_UP)
btn_stop = Pin(8, Pin.IN, Pin.PULL_UP)
leds = [Pin(10, Pin.OUT), Pin(11, Pin.OUT), Pin(12, Pin.OUT), Pin(13, Pin.OUT)]
# Frecuencias musicales (Hz)
SI4 = 494
MI5 = 659
RE5_S = 622
FA5_S = 740
SOL5 = 784
LA5 = 880
SI5 = 988
RE6 = 1175
MI6 = 1319
# Melodia: (Frecuencia, Duracion en ms, Nombre de nota, LED a encender)
melodia = [
(SI4, 300, "SI", 0), (MI5, 300, "MI", 1), (RE5_S, 300, "RE#", 0), (MI5, 300, "MI", 1),
(FA5_S, 300, "FA#", 2), (SOL5, 300, "SOL", 2), (FA5_S, 300, "FA#", 2), (SOL5, 300, "SOL", 2),
(LA5, 300, "LA", 3), (SI5, 750, "SI", 3), (RE6, 300, "RE", 3), (SI5, 750, "SI", 3),
(LA5, 300, "LA", 2), (SOL5, 300, "SOL", 2), (MI5, 800, "MI", 1),
(SOL5, 300, "SOL", 2), (MI5, 800, "MI", 1),
(SI4, 300, "SI", 0), (MI5, 300, "MI", 1), (RE5_S, 300, "RE#", 0), (MI5, 300, "MI", 1),
(FA5_S, 300, "FA#", 2), (SOL5, 300, "SOL", 2), (FA5_S, 300, "FA#", 2), (SOL5, 300, "SOL", 2),
(LA5, 300, "LA", 3), (SI5, 750, "SI", 3), (RE6, 300, "RE", 3), (SI5, 750, "SI", 3),
(LA5, 300, "LA", 2), (SOL5, 300, "SOL", 2), (MI5, 900, "MI", 1),
# Parte alta (Coro)
(MI6, 650, "MI6", 3), (RE6, 300, "RE6", 3), (SI5, 750, "SI", 2),
(RE6, 300, "RE6", 3), (SI5, 750, "SI", 2), (LA5, 300, "LA", 1), (SOL5, 300, "SOL", 1),
(MI5, 800, "MI", 0), (SOL5, 300, "SOL", 1), (MI5, 900, "MI", 0)
]
def apagar_leds():
for l in leds:
l.value(0)
lcd_init()
lcd_print("EL CONDOR PASA")
lcd_pos(0, 1)
lcd_print("Verde: Play")
while True:
# Esperar boton verde para iniciar la cancion
if btn_play.value() == 0:
lcd_clear()
lcd_pos(0, 0)
lcd_print("EL CONDOR PASA..")
print("Reproduciendo: El Condor Pasa")
for freq, dur, nombre, idx_led in melodia:
# Si presionas el boton rojo, detiene la cancion al instante
if btn_stop.value() == 0:
print("Cancion detenida")
break
apagar_leds()
leds[idx_led].value(1)
lcd_pos(0, 1)
lcd_print(f"Nota: {nombre} ")
buzzer.freq(freq)
buzzer.duty_u16(40000)
time.sleep_ms(dur)
# Pausa corta entre notas para que se escuche articulado
buzzer.duty_u16(0)
time.sleep_ms(40)
buzzer.duty_u16(0)
apagar_leds()
lcd_clear()
lcd_pos(0, 0)
lcd_print("EL CONDOR PASA")
lcd_pos(0, 1)
lcd_print("Verde: Play")
time.sleep_ms(300)
time.sleep_ms(50)