from machine import I2C, Pin
from pico_i2c_lcd import I2cLcd
import time

# Configure I2C
i2c = I2C(1, sda=Pin(6), scl=Pin(7), freq=400000)
button = Pin(26, Pin.IN, Pin.PULL_UP)

devices = i2c.scan()
if not devices:
    print("No I2C device found!")
else:
    lcd = I2cLcd(i2c, devices[0], 2, 16)
    
    while True:
        lcd.clear()
        lcd.putstr("Timer Started!")
        start_time = time.ticks_ms()
        
        while True:
            # 1. Calculate total elapsed seconds
            elapsed = time.ticks_diff(time.ticks_ms(), start_time) // 1000
            
            # 2. Break down into H:M:S
            hours = (elapsed // 3600)
            minutes = (elapsed % 3600) // 60
            seconds = elapsed % 60
            
            # 3. Display formatted time (00:00:00)
            lcd.move_to(0, 1)
            # :02d ensures 2 digits (e.g., 05 instead of 5)
            lcd.putstr(f"Time: {hours:02d}:{minutes:02d}:{seconds:02d}")
            
            # Check for Reset Button
            if button.value() == 0:
                time.sleep(0.2) # Simple debounce
                break 
                
            time.sleep(0.1)

