第一篇文章。我有一个搜索周围,没有找到很多关于此的信息,希望有人在这里可以提供帮助。
对于背景,我正在建立一个进站计时系统,以便在比赛期间使用他们的轮枪/反应来辅助机械师。 PIt Stop Rig。希望你能在图片中看到,有四个轴,每个都有一个LED,每个都切换。
我正在编写一个脚本来运行一个基本的反应计时器,使用Rpi,Piface Digital 2并用Python驱动它。我有第一次迭代工作,因为它读取数字输入,并驱动数字输出。我现在想做到这一点,它通过5次运行scriput,读取数字输入并将它们存储在变量中。如果有人可以使用Python,我会非常感激。
#!/usr/bin/env python3
# Pit Stop Reaction Timer
from time import sleep
from timeit import default_timer as timer
import random
from random import randrange, uniform
import pifacedigitalio as pifacedigital
import sys
pifacedigital.init()
leds = [1, 2, 3, 4]
randomNumber1 = [0, 3 , 2, 1, 1, 2, 3, 0, 0, 2, 3, 1, 1, 3, 0, 2]
randomNumber2 = [2, 4, 6, 8, 4, 3, 8, 12, 10, 11, 9, 8, 6, 7, 10, 5]
randomLED = random.choice(randomNumber1)
randomFlash = random.choice(randomNumber2)
sleepTime = 0.1 # the time in seconds
rounds = 8
button4 = 0
button5 = 1
button6 = 2
button7 = 3
def ledFlash(times, interval):
i = 0
# repeat times times ...
while i < times:
# turn on LED 1 to 4
for led in leds:
pifacedigital.digital_write(0, 0)
pifacedigital.digital_write(1, 0)
pifacedigital.digital_write(2, 0)
pifacedigital.digital_write(3, 0)
sleep(interval)
# turn off LED 0 to 4 one at a time with interval intervals ...
for led in leds:
pifacedigital.digital_write(0, 1)
pifacedigital.digital_write(1, 1)
pifacedigital.digital_write(2, 1)
pifacedigital.digital_write(3, 1)
sleep(interval)
i += 1
print("Menu:")
print("=====")
print("External Button: Start Pit Stop Sim")
print()
print("Select the correct hub with the gun to stop the timer")
while True:
if pifacedigital.digital_read(0):
ledFlash(randomFlash, sleepTime)
pifacedigital.digital_write(randomLED, 0)
start = timer()
print("Random LED result", randomLED)
print("Random number of LED flashes result", randomFlash)
elif pifacedigital.digital_read(4) and button4 == randomLED:
end = timer()
print(end-start)
sys.exit()
elif pifacedigital.digital_read(5) and button5 == randomLED:
end = timer()
print(end-start)
sys.exit()
elif pifacedigital.digital_read(6) and button6 == randomLED:
end = timer()
print(end-start)
sys.exit()
elif pifacedigital.digital_read(7) and button7 == randomLED:
end = timer()
print(end-start)
sys.exit()
任何关于使用循环来使其迭代5次的指针,以便我可以测量并取平均值。
非常感谢
乔。