from machine import Pin, PWM, ADC, Timer
import utime
# Configurar PWM para los LEDs
led1 = PWM(Pin(0)) # GPIO15 para LED1
led2 = PWM(Pin(1)) # GPIO14 para LED2
led1.freq(100)
led2.freq(100)
# Inicializar con ciclo de trabajo de 0%
duty1 = 0
duty2 = 0
led1.duty_u16(duty1)
led2.duty_u16(duty2)
# Configurar ADCs para el joystick
joy_x = ADC(0) # GPIO26
joy_y = ADC(1) # GPIO27
# Configurar botón con interrupción
boton = Pin(16, Pin.IN, Pin.PULL_UP) # GPIO16
# Límites y paso
MAX_DUTY = 65000
MIN_DUTY = 0
PASO = 5000
TERCIO = 65535 // 3
DOS_TERCIOS = TERCIO * 2
# Función interrupción para botón
def boton_callback(pin):
global duty1, duty2
duty1 = 0
duty2 = 0
led1.duty_u16(duty1)
led2.duty_u16(duty2)
print("Botón presionado: LEDs apagados")
boton.irq(trigger=Pin.IRQ_FALLING, handler=boton_callback)
# Función periódica del temporizador
def leer_joystick(timer):
global duty1, duty2
x_val = joy_x.read_u16()
y_val = joy_y.read_u16()
# LED1 - Eje X
if x_val > DOS_TERCIOS and duty1 + PASO <= MAX_DUTY:
duty1 += PASO
elif x_val < TERCIO and duty1 - PASO >= MIN_DUTY:
duty1 -= PASO
led1.duty_u16(duty1)
# LED2 - Eje Y
if y_val > DOS_TERCIOS and duty2 + PASO <= MAX_DUTY:
duty2 += PASO
elif y_val < TERCIO and duty2 - PASO >= MIN_DUTY:
duty2 -= PASO
led2.duty_u16(duty2)
# Temporizador que se ejecuta cada 500ms
timer = Timer()
timer.init(freq=2, mode=Timer.PERIODIC, callback=leer_joystick)
# Bucle principal vacío
while True:
utime.sleep(1)