from machine import Pin, time_pulse_us
import time
import tm1637
# === Broches ===
trig = Pin(5, Pin.OUT)
echo = Pin(18, Pin.IN)
trig1 = Pin(19, Pin.OUT)
echo1 = Pin(4, Pin.IN)
push_button = Pin(23, Pin.IN, Pin.PULL_DOWN)
led = Pin(33, Pin.OUT)
tm = tm1637.TM1637(clk=Pin(12), dio=Pin(13))
# === Paramètres ===
threshold = 50 # Distance de détection (cm)
cooldown_ms = 30000 # Pause entre deux cycles
min_stop_delay = 30000 # Délai avant arrêt possible (2 s)
display_time_ms = 20000 # Temps d'affichage (20 s)
# === Variables ===
timer_running = False
ready_to_stop = False
start_ticks = 0
last_finish_ticks = 0
display_start_ticks = 0
displaying_result = False
tm.show("0000")
# === Fonction de mesure distance ===
def mesure_distance(trig, echo, samples=3):
total = 0
valid = 0
for _ in range(samples):
trig.off()
time.sleep_us(2)
trig.on()
time.sleep_us(10)
trig.off()
try:
duree = time_pulse_us(echo, 1, 50000)
distance = (duree / 2) / 29.1
if distance < 500:
total += distance
valid += 1
except OSError:
pass
time.sleep_ms(1)
return total / valid if valid > 0 else 999
# === Fonction Reset global ===
def reset_all():
global timer_running, ready_to_stop, displaying_result, last_finish_ticks
timer_running = False
ready_to_stop = False
displaying_result = False
last_finish_ticks = 0 # Supprime le cooldown
tm.show("0000")
print("🔄 RESET COMPLET — chrono et cooldown remis à zéro")
# === Boucle principale ===
while True:
current_ticks = time.ticks_ms()
distance = mesure_distance(trig, echo)
distance1 = mesure_distance(trig1, echo1)
print("dist : ",distance )
print("dist1 : ",distance1 )
reset = push_button.value()
time.sleep_ms(50)
# --- LED ON si objet détecté ---
if (distance < threshold) or (distance1 < threshold):
led.on()
else:
led.off()
# === Action RESET bouton ===
if reset == 1:
reset_all()
time.sleep_ms(300) # anti-rebond
continue
# === Si on affiche le résultat (pendant 20 s) ===
if displaying_result:
if time.ticks_diff(current_ticks, display_start_ticks) >= display_time_ms:
print("⏱️ Fin affichage - remise à zéro")
reset_all()
continue
# === Démarrage du chrono ===
if not timer_running:
# pas de cooldown si reset effectué
if (last_finish_ticks == 0) or (time.ticks_diff(current_ticks, last_finish_ticks) > cooldown_ms):
if (distance < threshold) or (distance1 < threshold):
timer_running = True
ready_to_stop = False
start_ticks = current_ticks
print("▶️ Chrono démarré")
# === Autorisation d’arrêt après 2 s ===
if timer_running and not ready_to_stop:
if time.ticks_diff(current_ticks, start_ticks) >= min_stop_delay:
ready_to_stop = True
print("🟢 Arrêt autorisé")
# === Arrêt automatique ===
if timer_running and ready_to_stop:
if (distance < threshold) or (distance1 < threshold):
elapsed_ms = time.ticks_diff(current_ticks, start_ticks)
elapsed_s = elapsed_ms / 1000.0
valeur = "{:04d}".format(int(elapsed_s * 100))
tm.show(valeur)
print("⏹ Temps final:", elapsed_s, "s")
displaying_result = True
display_start_ticks = current_ticks
timer_running = False
ready_to_stop = False
last_finish_ticks = current_ticks # pour cooldown (sauf reset)
continue
# === Affichage en temps réel pendant chrono ===
if timer_running:
elapsed_ms = time.ticks_diff(current_ticks, start_ticks)
elapsed_s = elapsed_ms / 1000.0
tm.show("{:04d}".format(int(elapsed_s * 100)))