-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton_timer.py
More file actions
executable file
·132 lines (121 loc) · 3.68 KB
/
Copy pathbutton_timer.py
File metadata and controls
executable file
·132 lines (121 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#! /usr/bin/python3
import random
import RPi.GPIO as GPIO
import time
import os
#constants used for testing reflexes
lightOn = 1
lightOff = 2
BuzzerOn = 3
BuzzerOff = 4
piNum = 22 #The number of the Pi being used in the lab
BUTTON = 2
LED = 3
BUZZER = 21
user = ""
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED, GPIO.OUT) # LED
GPIO.setup(BUZZER, GPIO.OUT) # Buzzer
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
tests = [1,1,1,2,2,2,3,3,3,4,4,4]
def determineFilename(user):
directory = "/home/pi/"
target = "record" + str(piNum) + user + "_"
if not os.path.exists(directory):
os.makedirs(directory)
listFiles = os.listdir(directory)
relFiles = [k for k in listFiles if target in k]
if relFiles:
num = 1
while(target + str(num) + ".txt" in relFiles):
relFiles.remove(target + str(num) + ".txt")
num += 1
f = open(directory + "/" + target + str(num) + ".txt", "w+")
else:
f = open(directory + "/" + target + "1.txt", "w+")
return f
def startTest(test):
if test == 1:
print("starting up - press the button when the LED turns on")
for i in range(3):
GPIO.output(LED, GPIO.HIGH)
time.sleep(0.25)
GPIO.output(LED, GPIO.LOW)
time.sleep(0.25)
elif test == 2:
print("starting up - press the button when the LED turns off")
for i in range(3):
GPIO.output(LED, GPIO.LOW)
time.sleep(0.25)
GPIO.output(LED, GPIO.HIGH)
time.sleep(0.25)
elif test == 3:
print("starting up - press the button when the buzzer turns on")
for i in range(3):
GPIO.output(BUZZER, GPIO.HIGH)
time.sleep(0.25)
GPIO.output(BUZZER, GPIO.LOW)
time.sleep(0.25)
elif test == 4:
print("starting up - press the button when the buzzer turns off")
for i in range(3):
GPIO.output(BUZZER, GPIO.LOW)
time.sleep(0.25)
GPIO.output(BUZZER, GPIO.HIGH)
time.sleep(0.25)
def endTest(test):
if test == 1:
GPIO.output(LED, GPIO.LOW)
elif test == 2:
GPIO.output(LED, GPIO.LOW)
elif test == 3:
GPIO.output(BUZZER, GPIO.LOW)
elif test == 4:
GPIO.output(BUZZER, GPIO.LOW)
def endWait(test):
if test == 1:
GPIO.output(LED, GPIO.HIGH)
elif test == 2:
GPIO.output(LED, GPIO.LOW)
elif test == 3:
GPIO.output(BUZZER, GPIO.HIGH)
elif test == 4:
GPIO.output(BUZZER, GPIO.LOW)
def testReaction(test, user):
startTest(test)
time.sleep(random.random() * 2 + 2)
if (GPIO.input(BUTTON) == 0 and user != 'warren'): # pressing the button goes low
print("cheater... wait until the signal to press the button")
print("restarting")
endTest(test)
test = random.choice(tests)
testReaction(test)
return
endWait(test)
start = time.clock()
while (GPIO.input(BUTTON) == 1 and user != 'warren'):
pass
final = time.clock()
GPIO.output(LED, GPIO.LOW)
GPIO.output(BUZZER, GPIO.LOW)
result = final - start
print('your response time was {0} seconds!'.format(result))
tests.remove(test)
if test == 1:
test = "LED_ON"
elif test == 2:
test = "LED_OFF"
elif test == 3:
test = "BUZZ_ON"
elif test == 4:
test = "BUZZ_OFF"
f.write("%s,%s,%s,%s\n" % (piNum, user, test, result))
user = input("please enter your name: ")
f = determineFilename(user)
f.write("board,user,test,time-elapsed\n")
while len(tests):
test = random.choice(tests)
testReaction(test, user)
print("testing complete")
GPIO.cleanup()
exit()