from machine import Pin
from utime import sleep_ms, ticks_ms
import random
led = Pin(28, Pin.OUT)
button1 = Pin(3, Pin.IN)
button2 = Pin(2, Pin.IN)
roubaram = False
dict_botoes = {"🔴 Vermelho ganhou!": button2, "🔵 Azul ganhou!": button1, "⚪ Empate!": button1}
def esperar_botoes(num):
while True:
if button1.value() == num and button2.value() == num:
break
sleep_ms(10)
def enviar_resultados(inicio, ganhador):
tempo_ganhador = (ticks_ms() - inicio - demora)/1000
while True:
sleep_ms(1)
if dict_botoes[ganhador].value() == 1:
tempo_perdedor = (ticks_ms() - inicio - demora)/1000
break
diferença = tempo_perdedor - tempo_ganhador
print(ganhador)
print("================================")
print(f"📟 Em {tempo_ganhador:.2f} segundos")
if ganhador != "⚪ Empate!":
print(f"🏆 {diferença:.3f} segundos mais rápido")
def finalizar_led():
while True:
led.value(0)
sleep_ms(500)
led.value(1)
sleep_ms(500)
esperar_botoes(1)
random.seed(ticks_ms())
demora = random.randint(2000,15001)
esperar_botoes(0)
inicio = ticks_ms()
print("⌚ Preste atenção...")
while roubaram == False and ticks_ms()-inicio < demora:
sleep_ms(1)
if button1.value() == 1 and button2.value() == 1:
print("❎⚪ Ambos clicaram antes!")
roubaram = True
elif button1.value() == 1:
print("❎🔴 Vermelho clicou antes!")
roubaram = True
elif button2.value() == 1:
print("❎🔵 Azul clicou antes!")
roubaram = True
if roubaram == False:
led.on()
print("🚦 LED Aceso!\n")
ganhador = False
while not ganhador:
sleep_ms(1)
if button1.value() == 1 and button2.value() == 1:
ganhador = "⚪ Empate!"
elif button1.value() == 1:
ganhador = "🔴 Vermelho ganhou!"
elif button2.value() == 1:
ganhador = "🔵 Azul ganhou!"
if ganhador:
enviar_resultados(inicio, ganhador)
finalizar_led()