from machine import Pin,I2C,PWM
from pico_i2c_lcd import I2cLcd
from time import sleep_ms,time
i2c=I2C(0,scl=Pin(22),sda=Pin(21),freq=400000)
lcd=I2cLcd(i2c,0x27,2,16)
a_plus=Pin(18,Pin.IN,Pin.PULL_UP)
a_minus=Pin(5,Pin.IN,Pin.PULL_UP)
b_plus=Pin(17,Pin.IN,Pin.PULL_UP)
b_minus=Pin(16,Pin.IN,Pin.PULL_UP)
reset_btn=Pin(18,Pin.IN,Pin.PULL_UP)
start_btn=Pin(19,Pin.IN,Pin.PULL_UP)
buzz=PWM(Pin(23))
scoreA=0; scoreB=0
seconds=600
running=False
last=time()
def beep(ms=300):
buzz.freq(2000); buzz.duty_u16(20000)
sleep_ms(ms)
buzz.duty_u16(0)
def draw():
lcd.move_to(0,0)
lcd.putstr("A:{:02d} B:{:02d} ".format(scoreA,scoreB))
m=seconds//60; s=seconds%60
lcd.move_to(0,1)
lcd.putstr("{:02d}:{:02d} ".format(m,s))
lcd.clear(); draw()
locks={}
for n in ["ap","am","bp","bm","r","st"]: locks[n]=False
while True:
if running and time()-last>=1:
last=time()
if seconds>0:
seconds-=1; draw()
else:
running=False
beep(1000)
lcd.clear()
if scoreA>scoreB: lcd.putstr("VENCEDOR A")
elif scoreB>scoreA: lcd.putstr("VENCEDOR B")
else: lcd.putstr("EMPATE")
for pin,key in [(a_plus,"ap"),(a_minus,"am"),(b_plus,"bp"),(b_minus,"bm"),(reset_btn,"r"),(start_btn,"st")]:
if pin.value()==0 and not locks[key]:
locks[key]=True
if key=="ap": scoreA+=1
elif key=="am" and scoreA>0: scoreA-=1
elif key=="bp": scoreB+=1
elif key=="bm" and scoreB>0: scoreB-=1
elif key=="r":
scoreA=scoreB=0; seconds=600; running=False
elif key=="st":
running=not running; last=time()
draw()
elif pin.value()==1:
locks[key]=False
sleep_ms(20)