from machine import I2C, Pin
import pico_i2c_lcd
import utime
import time
# Configura el bus I2C
# Reemplaza 2 y 3 con los pines de SDA y SCL
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# La dirección I2C del LCD puede variar, este es un valor común.
lcd_address = 0x27
pac_abierto = [
0b01110,
0b10001,
0b10000,
0b10001,
0b01110,
0b00000,
0b00000,
0b00000]
pac_cerrado = [
0b01110,
0b10001,
0b10001,
0b10001,
0b10001,
0b01110,
0b00000,
0b00000]
lcd = pico_i2c_lcd.I2cLcd(i2c, lcd_address, 2, 16) # Asume un LCD de 2x16
lcd.custom_char(0, pac_abierto)
lcd.custom_char(1, pac_cerrado)
# Limpia el LCD
lcd.clear()
## Botones
dis=Pin(26,Pin.IN,Pin.PULL_UP)
inc=Pin(25,Pin.IN,Pin.PULL_UP)
start=Pin(33,Pin.IN,Pin.PULL_UP)
stop=Pin(32,Pin.IN,Pin.PULL_UP)
led=Pin(15,Pin.OUT)
## Variables Globales
numero=60
numero1=0
numero2=0
n_guardado=0
v_inc=0
v_dis=0
v_start=0
v_stop=0
flag=False
last_time=0
last_time2=0
tempo=200
tempo2=500
string=0
while True:
if time.ticks_ms()-last_time2>=tempo2:
last_time2=time.ticks_ms()
numero1=numero//60
numero2=numero%60
#LCD
lcd.clear()
string = f"{numero1:02}:{numero2:02}"
lcd.putstr(string)
## Logica
if time.ticks_ms()-last_time>=tempo:
last_time=time.ticks_ms()
#Suma
if v_inc!=0 and inc.value()!=1 :
v_inc=0
numero=numero+10
elif inc.value()==1:
v_inc=1
#Resta
if v_dis!=0 and dis.value()!=1 :
v_dis=0
numero=numero-10
if numero==0:
numero=60
elif dis.value()==1:
v_dis=1
#Start
if v_start!=0 and start.value()!=1 and flag==False:
flag=True
n_guardado=numero
tempo=1000
elif start.value()==1:
v_start=1
#Flag
if flag==True:
numero-=1
if numero==-1:
led.value(1)
time.sleep_ms(1000)
led.value(0)
numero=n_guardado
#Stop
if v_stop!=0 and stop.value()!=1 and flag==True:
flag=False
numero=n_guardado
tempo=200
elif stop.value()==1:
v_stop=1