import time
from machine import Pin, ADC, Timer
# Joystick y LED
joy_x = ADC(Pin(26))
joy_y = ADC(Pin(27))
joy_sw = Pin(16, Pin.IN, Pin.PULL_UP)
led_estado = Pin(17, Pin.OUT)
# Segmentos del display
segmentos_pins = [
Pin(28, Pin.OUT), Pin(10, Pin.OUT), Pin(11, Pin.OUT),
Pin(22, Pin.OUT), Pin(21, Pin.OUT), Pin(20, Pin.OUT), Pin(19, Pin.OUT)
]
# Dígitos para multiplexación
digitos_pin = [
Pin(12, Pin.OUT), Pin(13, Pin.OUT), Pin(14, Pin.OUT), Pin(15, Pin.OUT)
]
# VARIABLES
val_x = 0
val_y = 0
patron_segmento = [
(1,1,1,1,1,1,0), (0,1,1,0,0,0,0), (1,1,0,1,1,0,1), (1,1,1,1,0,0,1), (0,1,1,0,0,1,1),
(1,0,1,1,0,1,1), (1,0,1,1,1,1,1), (1,1,1,0,0,0,0), (1,1,1,1,1,1,1), (1,1,1,1,0,1,1)
]
# FUNCIONES
def leer_sensores(t):
global val_x, val_y
# Escalar de 0-65535 a 0-99 ----- 65535 / 99 = 661.9
val_x = int(joy_x.read_u16() / 661)
val_y = int(joy_y.read_u16() / 661)
if val_x > 99: val_x = 99
if val_y > 99: val_y = 99
# Inicializar Timer de 300ms
tim = Timer()
tim.init(period=300, mode=Timer.PERIODIC, callback=leer_sensores)
# BUCLE PRINCIPAL
while True:
led_estado.value(not joy_sw.value())
# Preparar los 4 números para el display
datos_display = [
val_x // 10, val_x % 10, # Posición X
val_y // 10, val_y % 10 # Posición Y
]
for i in range(4):
for d in digitos_pin: d.value(1)
patron = patron_segmento[datos_display[i]]
for j in range(7):
segmentos_pins[j].value(patron[j])
# Activar el dígito correspondiente
digitos_pin[i].value(0)
time.sleep(0.004)