-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reaction2P.ino
125 lines (123 loc) · 3.31 KB
/
Reaction2P.ino
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
int buttonLeft1P = 12;
int buttonRight1P = 8;
int buttonLeft2P = 7;
int buttonRight2P = 4;
int LEDLeft = 11;
int LEDRight = 9;
boolean LEDLeftIndicator;
boolean LEDRightIndicator;
boolean buttonPressed1P;
boolean buttonPressed2P;
int LEDDelay;
int chooseLED;
double startTimer;
double endTimer1P;
double endTimer2P;
double singleTimer1P;
double singleTimer2P;
double totalTimer1P;
double totalTimer2P;
double averageTimer1P;
double averageTimer2P;
int score1P;
int score2P;
int counter;
void setup(){
Serial.begin(9600);
pinMode(buttonLeft1P, INPUT);
pinMode(buttonLeft2P, INPUT);
pinMode(buttonRight1P, INPUT);
pinMode(buttonRight2P, INPUT);
pinMode(LEDLeft, OUTPUT);
pinMode(LEDRight, OUTPUT);
LEDLeftIndicator = false;
LEDRightIndicator = false;
totalTimer1P = 0;
totalTimer2P = 0;
singleTimer1P = 0;
singleTimer2P = 0;
averageTimer1P = 0;
averageTimer2P = 0;
score1P = 0;
score2P = 0;
chooseLED = 0;
counter = 1;
}
void loop(){
LEDDelay = random(2000, 6000);
chooseLED = random(1, 3);
startTimer = 0;
endTimer1P = 0;
endTimer2P = 0;
buttonPressed1P = false;
buttonPressed2P = false;
delay(LEDDelay);
startTimer = millis();
if(chooseLED == 1){
digitalWrite(LEDLeft, HIGH);
LEDLeftIndicator = true;
while(buttonPressed1P != true || buttonPressed2P != true){
if(digitalRead(buttonLeft1P) == LOW && buttonPressed1P == false){
endTimer1P = millis();
singleTimer1P = endTimer1P - startTimer;
totalTimer1P = totalTimer1P + singleTimer1P;
averageTimer1P = totalTimer1P / counter;
buttonPressed1P = true;
}
if(digitalRead(buttonLeft2P) == LOW && buttonPressed2P == false){
endTimer2P = millis();
singleTimer2P = endTimer2P - startTimer;
totalTimer2P = totalTimer2P + singleTimer2P;
averageTimer2P = totalTimer2P / counter;
buttonPressed2P = true;
}
}
digitalWrite(LEDLeft, LOW);
}
if(chooseLED == 2){
digitalWrite(LEDRight, HIGH);
LEDRightIndicator = true;
while(buttonPressed1P != true || buttonPressed2P != true){
if(digitalRead(buttonRight1P) == LOW && buttonPressed1P == false){
endTimer1P = millis();
singleTimer1P = endTimer1P - startTimer;
totalTimer1P = totalTimer1P + singleTimer1P;
averageTimer1P = totalTimer1P / counter;
buttonPressed1P = true;
}
if(digitalRead(buttonRight2P) == LOW && buttonPressed2P == false){
endTimer2P = millis();
singleTimer2P = endTimer2P - startTimer;
totalTimer2P = totalTimer2P + singleTimer2P;
averageTimer2P = totalTimer2P / counter;
buttonPressed2P = true;
}
}
digitalWrite(LEDRight, LOW);
}
counter++;
if(singleTimer1P<singleTimer2P){
Serial.println("1P Wins");
Serial.println(singleTimer1P/1000);
Serial.println("\n");
score1P++;
}
else{
Serial.println("2P Wins");
Serial.println(singleTimer2P/1000);
Serial.println("\n");
score2P++;
}
if(counter%6==0){
Serial.print("Current score is: ");
Serial.print(score1P);
Serial.print(":");
Serial.print(score2P);
Serial.print("\n");
delay(5000);
Serial.print("Player 1 average time: ");
Serial.println(averageTimer1P/1000);
Serial.print("Player 2 average time: ");
Serial.println(averageTimer2P/1000);
}
}