首页 > 解决方案 > 如何使用 LED、按钮命令和时序进行适当的反应时间模拟

问题描述

因此,对于我的反应时间分配,我遇到了多个问题,例如计时器结束时,红灯不会亮起,也不会打印出用户失败的消息,当我在计时器结束后按 Enter 时,我可以仍然按下按钮,它会点亮绿色 LED 并打印出消息。任务的重点是设置 5-10 秒的随机计时器,在黄色 LED 用完之前按下它,并给出用户按下按钮直到它用完的总时间,我想知道如何正确地做到这一点。

import physense_emu
import time
import random

sensor = physense_emu.Sensor()

#Greeting user to press "enter" to start the reaction time simulator
strtTime = "Start"
#Setting up simulator to start when pressing "enter"
butbucket = input("Press Enter")
strtTime = time.time()
randVal = random.randint(5, 11)
sensor.output("yled", "on")
time.sleep(5)
sensor.output("yled", "off")

stpTime = time.time()
btnStp = "Button_3"

#Setting up reaction time system to light up "green" if user is fast enough to push a button, but if not, then it'll light up "red"
while True:
 if (sensor.input(btnStp) == "pressed"):
     totalTime = stpTime - strtTime
     if totalTime < 10:
         sensor.output("gled", "on")
         time.sleep(3)
         sensor.output("gled","off")
         print("Congrats, you passed!")
     else:
         sensor.output("rled", "on")
         time.sleep(3)
         sensor.output("rled", "off")
         print("I'm sorry, but you failed to react quick enough!")
         break

标签: pythonpython-3.x

解决方案


推荐阅读