from machine import Pin, ADC
import time
# ---------------- LEDS ----------------
leds = [
Pin(6, Pin.OUT),
Pin(7, Pin.OUT),
Pin(8, Pin.OUT),
Pin(9, Pin.OUT),
Pin(10, Pin.OUT),
Pin(11, Pin.OUT),
Pin(12, Pin.OUT),
Pin(13, Pin.OUT),
Pin(14, Pin.OUT),
Pin(15, Pin.OUT)
]
# ---------------- DISPLAY ----------------
seg = [
Pin(16, Pin.OUT), # a
Pin(17, Pin.OUT), # b
Pin(18, Pin.OUT), # c
Pin(19, Pin.OUT), # d
Pin(20, Pin.OUT), # e
Pin(21, Pin.OUT), # f
Pin(22, Pin.OUT) # g
]
numeros = [
[1,1,1,1,1,1,0], # 0
[0,1,1,0,0,0,0], # 1
[1,1,0,1,1,0,1], # 2
[1,1,1,1,0,0,1], # 3
[0,1,1,0,0,1,1], # 4
[1,0,1,1,0,1,1], # 5
[1,0,1,1,1,1,1], # 6
[1,1,1,0,0,0,0], # 7
[1,1,1,1,1,1,1], # 8
[1,1,1,0,0,1,1] # 9
]
# ---------------- POT ----------------
pot = ADC(Pin(28))
# ---------------- SWITCH ----------------
sw1 = Pin(2, Pin.IN, Pin.PULL_DOWN)
sw2 = Pin(3, Pin.IN, Pin.PULL_DOWN)
sw3 = Pin(4, Pin.IN, Pin.PULL_DOWN)
sw4 = Pin(5, Pin.IN, Pin.PULL_DOWN)
# -------- VELOCIDAD --------
def obtener_delay():
valor = pot.read_u16()
return 0.05 + (valor / 65535) * 0.9 # de rápido a lento
# -------- DISPLAY --------
contador = 9
ultimo_cambio = time.ticks_ms()
def actualizar_display():
global contador, ultimo_cambio
d = obtener_delay() * 1000
ahora = time.ticks_ms()
if time.ticks_diff(ahora, ultimo_cambio) > d:
contador -= 1
if contador < 0:
contador = 9
for i in range(7):
seg[i].value(numeros[contador][i])
ultimo_cambio = ahora
# -------- ESPERA INTELIGENTE --------
def esperar():
d = obtener_delay()
pasos = int(d / 0.01)
for _ in range(pasos):
actualizar_display()
time.sleep(0.01)
# -------- MODOS --------
def modo1():
for i in range(len(leds)-1, -1, -1):
leds[i].value(1)
esperar()
leds[i].value(0)
def modo2():
for i in range(len(leds)):
leds[i].value(1)
esperar()
leds[i].value(0)
def modo3():
for i in range(len(leds)-1, -1, -1):
leds[i].value(1)
esperar()
leds[i].value(0)
for i in range(len(leds)):
leds[i].value(1)
esperar()
leds[i].value(0)
def modo4():
for i in range(5):
leds[4 - i].value(1)
leds[5 + i].value(1)
esperar()
leds[4 - i].value(0)
leds[5 + i].value(0)
for i in range(4, -1, -1):
leds[4 - i].value(1)
leds[5 + i].value(1)
esperar()
leds[4 - i].value(0)
leds[5 + i].value(0)
# -------- SWITCH --------
def leer_switch():
if sw1.value(): return 1
elif sw2.value(): return 2
elif sw3.value(): return 3
elif sw4.value(): return 4
else: return 0
switch_case = {
1: modo1,
2: modo2,
3: modo3,
4: modo4
}
# -------- LOOP --------
while True:
opcion = leer_switch()
actualizar_display() # siempre activo
if opcion in switch_case:
switch_case[opcion]()
else:
for led in leds:
led.value(0)
time.sleep(0.01)