from machine import Pin
from time import sleep_ms, ticks_ms
# Pines de segmentos (a, b, c, d, e, f, g)
pines = [26, 27, 14, 12, 13, 25, 23]
segmentos = [Pin(pin, Pin.OUT) for pin in pines]
uni = Pin(19, Pin.OUT)
dec = Pin(18, Pin.OUT)
boton_up = Pin(5, Pin.IN, Pin.PULL_DOWN) # Incrementar
boton_down = Pin(4, Pin.IN, Pin.PULL_DOWN) # Decrementar
boton_crono = Pin(2, Pin.IN, Pin.PULL_DOWN) # Cronómetro
led = Pin(15, Pin.OUT)
matriz = [
[0, 0, 0, 0, 0, 0, 1], # 0
[1, 0, 0, 1, 1, 1, 1], # 1
[0, 0, 1, 0, 0, 1, 0], # 2
[0, 0, 0, 0, 1, 1, 0], # 3
[1, 0, 0, 1, 1, 0, 0], # 4
[0, 1, 0, 0, 1, 0, 0], # 5
[0, 1, 0, 0, 0, 0, 0], # 6
[0, 0, 0, 1, 1, 1, 1], # 7
[0, 0, 0, 0, 0, 0, 0], # 8
[0, 0, 0, 0, 1, 0, 0], # 9
]
contador = 0
anterior_up = 0
anterior_down = 0
anterior_crono = 0
last_press_up = 0
last_press_down = 0
last_press_crono = 0
cronometro_activo = False
contador_crono = 0
objetivo = 0
ultimo_incremento = 0
def mostrar(digito, display):
uni.value(0)
dec.value(0)
for i in range(7):
segmentos[i].value(matriz[digito][i])
if display == 0:
uni.value(1)
else:
dec.value(1)
def mostrar_numero(numero):
unidades = numero % 10
decenas = numero // 10
mostrar(unidades, 0)
sleep_ms(3)
mostrar(decenas, 1)
sleep_ms(3)
while True:
ahora = ticks_ms()
lectura_up = boton_up.value()
if lectura_up and not anterior_up and ahora - last_press_up > 200:
if not cronometro_activo:
contador = (contador + 1) % 100
last_press_up = ahora
anterior_up = lectura_up
lectura_down = boton_down.value()
if lectura_down and not anterior_down and ahora - last_press_down > 200:
if not cronometro_activo:
contador = (contador - 1) % 100
last_press_down = ahora
anterior_down = lectura_down
lectura_crono = boton_crono.value()
if lectura_crono and not anterior_crono and ahora - last_press_crono > 200:
if not cronometro_activo:
objetivo = contador
contador_crono = 0
cronometro_activo = True
ultimo_incremento = ahora
else:
cronometro_activo = False
contador = contador_crono
last_press_crono = ahora
anterior_crono = lectura_crono
if cronometro_activo:
if ahora - ultimo_incremento >= 1000:
if contador_crono < objetivo:
contador_crono += 1
ultimo_incremento = ahora
else:
cronometro_activo = False
contador = contador_crono
led.value(1)
sleep_ms(3000)
led.value(0)
if cronometro_activo:
mostrar_numero(contador_crono)
else:
mostrar_numero(contador)