import time
import random
from machine import Pin, PWM
# print("hello")
push_btn = Pin(2, Pin.IN, Pin.PULL_UP)
switch = Pin(27, Pin.IN, Pin.PULL_UP)
red = Pin(6, Pin.OUT)
blue = Pin(8, Pin.OUT)
green = PWM(Pin(7))
green.freq(1000)
while True:
if switch.value() == 0:
red.value(0)
time1 = random.randint(1, 5)
time.sleep(time1)
if switch.value() != 0:
continue
if push_btn.value() != 1:
print("No cheating! Don't hold the button early.")
red.value(1)
time.sleep(2)
red.value(0)
continue
blue.value(1)
start_time = time.ticks_ms()
while push_btn.value() == 1:
if switch.value() != 0:
break
if switch.value() != 0:
blue.value(0)
continue
end_time = time.ticks_ms()
reaction_time = time.ticks_diff(end_time, start_time)
blue.value(0)
print(f"your reaction time is {reaction_time} ms")
green.duty_u16(65535)
time.sleep(2)
green.duty_u16(0)
else:
print("Switched is off")
red.value(1)
time.sleep(1)