# Pulsar en el botón VERDE y LUEGO EN EL ROJO.
# Se encienden los leds : VERDE al pulsar al pulsar el VERDE
# y ROJO al pulsar en el ROJO
# Por consola se mostrará el intervalo de tiempo entre las dos
# pulsaciones ( en microsegundos)
# Pulsando en el AZUL se resetean los leds y los tiempos.
import machine
from machine import Pin, Timer
import utime
# Definir pines para las IRQ
pin_irq_1 = Pin(2, Pin.IN, Pin.PULL_UP)
pin_irq_2 = Pin(3, Pin.IN, Pin.PULL_UP)
# Pin RESET
pin_reset=Pin(8,Pin.IN,Pin.PULL_UP)
# SETEO DE LEDS
led_start=Pin(28,Pin.OUT)
led_end=Pin(21,Pin.OUT)
# Definir temporizadores
timer_start = Timer()
timer_end = Timer()
# Variables para almacenar los tiempos
start_time = 0
end_time = 0
print("start time =",start_time)
print("end time =",end_time)
# Función para capturar el tiempo de inicio
def capture_start_time(pin):
global start_time
start_time = utime.ticks_us() # Captura el tiempo actual en microsegundos
#print(start_time)
led_start.value(1)
# Función para capturar el tiempo de finalización
def capture_end_time(pin):
global end_time
end_time = utime.ticks_us() # Captura el tiempo actual en microsegundos
#print(end_time)
led_end.value(1)
def reset(rst): # RESETEA TIEMPOS Y APAGA LEDS
led_start.value(0)
led_end.value(0)
start_time = 0
end_time = 0
# Configurar IRQ para capturar el tiempo de inicio
pin_irq_1.irq(trigger=Pin.IRQ_FALLING, handler=capture_start_time)
# Configurar IRQ para capturar el tiempo de finalización
pin_irq_2.irq(trigger=Pin.IRQ_FALLING, handler=capture_end_time)
# IRQ RESET
pin_reset.irq(trigger=Pin.IRQ_FALLING, handler=reset)
# Lógica principal del programa (puedes ejecutar otras tareas aquí)
while True:
if start_time != 0 and end_time != 0:
# Calcular la diferencia de tiempo en microsegundos
interval = end_time - start_time
intervalo_seg= (end_time - start_time) / 1000000
print("Intervalo de tiempo entre interrupciones:", interval, "microsegundos")
print("Intervalo de tiempo entre interrupciones:", intervalo_seg, "segundos")
# Reiniciar los tiempos para la próxima medición
start_time = 0
end_time = 0
print("start time =",start_time)
print("end time =",end_time)
# Puedes realizar otras tareas aquí mientras esperas las interrupciones
utime.sleep_us(100) # Esperar un poco para reducir el uso de CPU