from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import time
# === Variables globales ===
evento = False
contador_tiempo = 0
contador_boton = 0
# === Inicialización de pantalla SSD1306 en GPIO 4 (SDA) y 5 (SCL)
i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
# === ISR para el botón (GPIO 15) ===
def isr_boton(pin):
global evento
evento = True
# Configurar botón como entrada con pull-up interno
boton = Pin(15, Pin.IN, Pin.PULL_UP)
boton.irq(trigger=Pin.IRQ_FALLING, handler=isr_boton)
# === Bucle principal ===
while True:
# Actualiza el contador de tiempo cada segundo
contador_tiempo += 1
# Si ocurrió un evento por botón
if evento:
contador_boton += 1
evento = False
# Mostrar en OLED
oled.fill(0)
oled.text("Contador:", 0, 0)
oled.text(str(contador_tiempo), 90, 10)
oled.text("Boton presionado:", 0, 20)
oled.text(str(contador_boton), 105, 30)
oled.show()
time.sleep(1)