from machine import Pin, SoftI2C, PWM
import ssd1306
import time
# Pines
start_button_pin = 32
reset_button_pin = 33
buzzer_pin = 18
# Configuración de botones
start_button = Pin(start_button_pin, Pin.IN, Pin.PULL_UP)
reset_button = Pin(reset_button_pin, Pin.IN, Pin.PULL_UP)
# Configuración del buzzer
buzzer = PWM(Pin(buzzer_pin))
# Configuración de la pantalla SSD1306
i2c = SoftI2C(scl=Pin(22), sda=Pin(21)) # Pines i2c para esp32
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# Variables del Pomodoro
pomodoro_time = int(1.2 * 60) # 25 minutos en segundos
time_left = pomodoro_time
running = False
start_time = 0
buzzer.duty(0)
def display_time(seconds):
minutes = seconds // 60
seconds = seconds % 60
oled.fill(0)
oled.text("{:02d}:{:02d}".format(minutes, seconds), 0, 0)
oled.show()
def sound_buzzer():
for _ in range(10):
buzzer.freq(1000)
buzzer.duty(512)
time.sleep_ms(200)
buzzer.duty(0)
time.sleep_ms(200)
while True:
if start_button.value() == 0:
time.sleep_ms(50) # Debounce
if start_button.value() == 0:
if not running:
running = True
start_time = time.ticks_ms()
if reset_button.value() == 0:
time.sleep_ms(50) # Debounce
if reset_button.value() == 0:
running = False
time_left = pomodoro_time
if running:
elapsed_time = (time.ticks_ms() - start_time) // 1000
time_left = pomodoro_time - elapsed_time
if time_left <= 0:
running = False
time_left = 0
sound_buzzer()
display_time(time_left)
time.sleep_ms(100)