from machine import Pin, I2C
from time import sleep, ticks_ms, ticks_diff
from random import randint
from pico_i2c_lcd import I2cLcd
botao1 = Pin(13, Pin.IN, Pin.PULL_DOWN)
botao2 = Pin(14, Pin.IN, Pin.PULL_DOWN)
botao3 = Pin(15, Pin.IN, Pin.PULL_DOWN)
led1 = Pin(18, Pin.OUT)
led2 = Pin(17, Pin.OUT)
led3 = Pin(16, Pin.OUT)
i2c = I2C(1, sda=Pin(10), scl=Pin(11), freq=100000)
lcd = I2cLcd(i2c, 39, 2, 16)
contador_acertos = 0
contador_erros = 0
def atualizar_placar():
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Acertos: " + str(contador_acertos))
lcd.move_to(0, 1)
lcd.putstr("Erros: " + str(contador_erros))
atualizar_placar()
while True:
espera = randint(1, 3)
sleep(espera)
valor_sorteado = randint(1, 3)
if valor_sorteado == 1:
led1.on()
elif valor_sorteado == 2:
led2.on()
else:
led3.on()
inicio = ticks_ms()
botao_pressionado = 0
while ticks_diff(ticks_ms(), inicio) < 500:
if botao1.value() == 1:
botao_pressionado = 1
elif botao2.value() == 1:
botao_pressionado = 2
elif botao3.value() == 1:
botao_pressionado = 3
led1.off()
led2.off()
led3.off()
if botao_pressionado == valor_sorteado:
print("Certo!")
contador_acertos += 1
else:
print("Errado!")
contador_erros += 1
atualizar_placar()
sleep(1)