from machine import Pin
import time
# Constants
POMODORO_TIME = 25 * 60 * 1000 # 25 minutes in milliseconds
# Button pins
start_button = Pin(0, Pin.IN, Pin.PULL_DOWN)
reset_button = Pin(1, Pin.IN, Pin.PULL_DOWN)
# Global variables
is_running = False
start_time = 0
def start_timer():
global is_running, start_time
start_time = time.ticks_ms()
is_running = True
def reset_timer():
global is_running
is_running = False
while True:
if start_button.value() == 1 and not is_running:
start_timer()
elif reset_button.value() == 1:
reset_timer()
if is_running:
current_time = time.ticks_ms()
elapsed_time = time.ticks_diff(current_time, start_time)
# Check for overflow (if time difference exceeds max value)
if elapsed_time < 0:
elapsed_time = (time.ticks_max() + 1) - start_time + current_time
minutes = elapsed_time // (1000 * 60)
seconds = (elapsed_time // 1000) % 60
print(f"{minutes:02d}:{seconds:02d}", end="\r")
if elapsed_time >= POMODORO_TIME:
is_running = False
print("\nPomodoro finished!")
# Add a small delay to avoid rapid printing on reset