# Creado por: ROSARIO HERNANDEZ JESUS ANTONIO
from machine import Pin, PWM, Timer, ADC
tempo = Timer()
led_rojo = PWM(Pin(0), freq=100, duty_u16=0)
led_azul = PWM(Pin(1), freq=100, duty_u16=0)
boton_joystick = Pin(16, Pin.IN, Pin.PULL_UP)
joystick_x = ADC(0)
joystick_y = ADC(1)
MAX_CICLO_TRABAJO = 65000
INCREMENTO = 500
def fun_timer(tempo):
valor_x = joystick_x.read_u16()
valor_y = joystick_y.read_u16()
if valor_x > (MAX_CICLO_TRABAJO * 2/3):
if led_rojo.duty_u16() + INCREMENTO <= MAX_CICLO_TRABAJO:
led_rojo.duty_u16(led_rojo.duty_u16() + INCREMENTO)
elif valor_x < (MAX_CICLO_TRABAJO * 1/3):
if led_rojo.duty_u16() - INCREMENTO >= 0:
led_rojo.duty_u16(led_rojo.duty_u16() - INCREMENTO)
if valor_y > (MAX_CICLO_TRABAJO * 2/3):
if led_azul.duty_u16() + INCREMENTO <= MAX_CICLO_TRABAJO:
led_azul.duty_u16(led_azul.duty_u16() + INCREMENTO)
elif valor_y < (MAX_CICLO_TRABAJO * 1/3):
if led_azul.duty_u16() - INCREMENTO >= 0:
led_azul.duty_u16(led_azul.duty_u16() - INCREMENTO)
def fun_boton(boton_joystick):
led_rojo.duty_u16(0)
led_azul.duty_u16(0)
boton_joystick.irq(trigger=Pin.IRQ_FALLING, handler=fun_boton)
tempo.init(period=500, mode=Timer.PERIODIC, callback=fun_timer)
while True:
pass