from machine import Pin, ADC, PWM, I2C
from ssd1306 import SSD1306_I2C
import time
# initialisation
button = Pin(3, Pin.IN, Pin.PULL_UP)
pot = ADC(26)
led = PWM(Pin(15))
led.freq(1000)
i2c = I2C(0, scl=Pin(1), sda=Pin(0))
oled = SSD1306_I2C(128, 64, i2c)
chrono_running = False
start_time = 0
elapsed_ms = 0
last_button = 1
def read_pot_percent():
value = pot.read_u16()
percent = int((value / 65535) * 100)
return percent
def set_led(state):
if state:
led.duty_u16(65535)
else:
led.duty_u16(0)
def format_time(ms):
"""Convertit millisecondes en MM:SS:ms"""
total_seconds = ms // 1000
minutes = total_seconds // 60
seconds = total_seconds % 60
millis = ms % 1000
return f"{minutes:02d}:{seconds:02d}:{millis:03d}"
def display_chrono(ms):
oled.fill(0)
oled.text("Chronometre", 0, 10)
oled.text(format_time(ms), 0, 30)
oled.show()
def reset_chrono():
global elapsed_ms, chrono_running, start_time
elapsed_ms = 0
chrono_running = False
set_led(False)
print("CHRONO RESET TO 0")
def start_chrono():
global chrono_running, start_time, elapsed_ms
if not chrono_running:
chrono_running = True
start_time = time.ticks_ms() - elapsed_ms
set_led(True)
print("CHRONO START")
def pause_chrono():
global chrono_running, elapsed_ms
if chrono_running:
# Sauvegarder le temps écoulé
elapsed_ms = time.ticks_ms() - start_time
chrono_running = False
set_led(False)
print("CHRONO PAUSED")
while True:
# BOUTON pour démarrer si à 0 ou arrêté, pause si en cours
if button.value() == 0 and last_button == 1:
if elapsed_ms == 0:
start_chrono()
elif chrono_running:
pause_chrono()
else:
start_chrono()
time.sleep(0.3)
last_button = button.value()
pot_percent = read_pot_percent()
if pot_percent >= 99:
reset_chrono()
if chrono_running:
elapsed_ms = time.ticks_ms() - start_time
display_chrono(elapsed_ms)
time.sleep(0.01) # 10ms pour bonne précision