from machine import Pin
from time import sleep_ms, ticks_ms, ticks_diff
# ESTABLECER PINES DE ENTRADA Y DE SALIDA
PinsIN = [32, 33, 25, 26, 27, 14, 12]
# Pines para el display a-g: A=32, B=33, C= 25, D=26, E=27, F=14, G=12
# Arreglo con asignacion de pines como salidas.
Pins = [Pin(pin, Pin.OUT) for pin in PinsIN]
# Pines de salida para el cada display. 0 = activo - 1 = Inactivo
Pin_Cat_D1 = Pin(22, Pin.OUT) # Decenas
Pin_Cat_D2 = Pin(23, Pin.OUT) # Unidades
# Configuracion de pin de entrada para el boton
btn_set = Pin(4, Pin.IN) # Botón activo-en-bajo: sin pulsar=1, pulsado=0
btn_up = Pin(17, Pin.IN) # Botón activo-en-bajo: sin pulsar=1, pulsado=0
btn_down = Pin(16, Pin.IN) # Botón activo-en-bajo: sin pulsar=1, pulsado=0
btn_reset = Pin(21, Pin.IN) # Botón activo-en-bajo: sin pulsar=1, pulsado=0
# Definicion de los segmentos del display
Segmentos = [
[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, 1, 0, 1, 1], # 9
]
# ____________________FUNIONES ___________________
def ShowDigit(valor): # Escribe un digito
for i in range(7):
Pins[i].value(Segmentos[valor][i]) # Envia el valor al pin correspondiente
def ShowDec(): # Muestra las decenas
Pin_Cat_D1.value(1); Pin_Cat_D2.value(1) # Evita el parpadeo
ShowDigit(Decenas)
Pin_Cat_D1.value(0) # Inactiva display 1 para mandar numeros
Pin_Cat_D2.value(1) # Activa display 2
def ShowUnit(): # Muestra las unidades
Pin_Cat_D1.value(1); Pin_Cat_D2.value(1) # Evita el parpadeo
ShowDigit(Unidades)
Pin_Cat_D1.value(1) # Activa display 1 para mandar numeros
Pin_Cat_D2.value(0) # Inactiva display 2
def refresco():
global ahora, t_refresco, mostrar_dec
if ticks_diff(ahora, t_refresco) >= 3: # prueba con 3 ms
if mostrar_dec: # Muestra las decenas
ShowDec()
else: # Muestra las unidades
ShowUnit()
mostrar_dec = not mostrar_dec # Alterna la bandera
t_refresco = ahora # Reinicia el tiempo de refresco
def Show_Display():
global set_value, counter, Decenas, Unidades
if MODE == "SET":
value = set_value
else:
value = counter
Decenas = value // 10
Unidades = value % 10
# ACTUALIZA EL LA INFORMACION SEGUN EL BOTON QUE SE OPRIMA
def update_button(read, pack, now, rebote = 40, pressed = 500, repeat = 120):
if read != pack["last"]: # Comparo estado actual con estado anterior
pack["last"] = read # Actualiza el nuevo estado del boton
pack["t_cambio"] = now # Inicializa el tiempo de referencia de pulso
estable = ticks_diff(now, pack["t_cambio"]) >= rebote # Evaluar si esta estable despues de 40ms
if read == 0 and estable: # Condicion 1: Boton pulsado y estable
if not pack["counted"]: # Condicion 1.1: Bandera pulso no contado
pack["t_hold"] = now
pack["t_rep"] = now
pack["counted"] = True
return "PRESS"
if ticks_diff(now, pack["t_hold"]) >= pressed:
if ticks_diff(now, pack["t_rep"]) >= repeat:
pack["t_rep"] = now
return "HOLD"
if read == 1 and estable:
pack["counted"] = False
return "RELEASE"
return None
# ____________________Variables________________________
# ESTADOS
MODE = "SET" # "SET = 1, RUN = 0"
set_value = 0 # Valor del display en estado SET
counter = 0 # Valor del contador en estado RUN
# Contador y display
Decenas = 0 # Contador de las decenas
Unidades = 0 # Contador de las unidades
mostrar_dec = True # Bandera para mostrar decenas o unidades
# Boton
# estado por botón (uno por cada botón)
# last: Ultimo estado del boton
# t_cambio: Tiempo al momento del cambio de estado
# counted: Bandera pulso contabilizado
# t_hold: Tiempo de referencia del btn presionado
# t_repeat: Tiempo en que aumenta el contador cuando btn presionado
btn_up_state = {"last":1,"t_cambio":ticks_ms(),"counted":False,"t_hold":0,"t_rep":0}
btn_down_state = {"last":1,"t_cambio":ticks_ms(),"counted":False,"t_hold":0,"t_rep":0}
btn_set_state = {"last":1,"t_cambio":ticks_ms(),"counted":False,"t_hold":0,"t_rep":0}
btn_reset_state = {"last":1,"t_cambio":ticks_ms(),"counted":False,"t_hold":0,"t_rep":0}
# Tiempos
ahora = ticks_ms()
t_refresco = ticks_ms()
t_tick1 = 0
# Tiempos de blink
t_blink = ticks_ms()
blinking = 300
flag_blink = False
# ____________________Buble principal_____________
while True:
ahora = ticks_ms()
Show_Display()
refresco()
ev_set = update_button(btn_set.value(), btn_up_state, ahora)
ev_up = update_button(btn_up.value(), btn_down_state, ahora)
ev_down = update_button(btn_down.value(), btn_set_state, ahora)
ev_reset = update_button(btn_reset.value(),btn_reset_state, ahora)
# FSM: SET
if MODE == "SET":
if ev_up == "PRESS":
set_value = (set_value + 1) % 100
if ev_down == "PRESS":
set_value = (set_value - 1) % 100
if ev_reset == "PRESS":
set_value = 0
if ev_set == "PRESS":
counter = set_value
MODE = "RUN"
t_tick1 = ahora
# FSM: RUN
elif MODE == "RUN":
if ticks_diff(ahora, t_tick1) >= 1000:
# MODO TEMPORIZADOR
if counter > 0:
counter -= 1
t_tick1 = ahora
# MODO CRONOMETRO
#if crono >= set_value:
# crono += 1
# t_tick1 = ahora
if counter == 0:
set_value = 0
MODE = "SET"
if ev_reset == "PRESS":
counter = 0
set_value = 0
MODE = "SET"