-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflexGame.ino
More file actions
187 lines (143 loc) · 4.51 KB
/
reflexGame.ino
File metadata and controls
187 lines (143 loc) · 4.51 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
int player1ButtonPin = 5;
int player2ButtonPin = 6;
int player3ButtonPin = 7;
int resetButtonPin = 11;
int piezoPin = A0;
int player1LedPin = 2;
int player2LedPin = 3;
int player3LedPin = 4;
int gameStatusLedPin1 = 12;
int gameStatusLedPin2 = 13;
int winner = 0;
boolean player1disq = false;
boolean player2disq = false;
boolean player3disq = false;
boolean gameIsPlaying = true;
boolean ballHasHitTable = false;
double voltageThreshold = 0.05;
//============================================================================
void setup() {
// Because there are no resistors used with the buttons, I use the port's internal pullup resistor with INPUT_PULLUP
pinMode(player1ButtonPin, INPUT_PULLUP);
pinMode(player2ButtonPin, INPUT_PULLUP);
pinMode(player3ButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(piezoPin, INPUT);
pinMode(gameStatusLedPin1, OUTPUT);
pinMode(gameStatusLedPin2, OUTPUT);
pinMode(player1LedPin, OUTPUT);
pinMode(player2LedPin, OUTPUT);
pinMode(player3LedPin, OUTPUT);
Serial.begin(9600);
}
//============================================================================
void loop() {
if (gameIsPlaying) {
// Read the value of the Piezo
int piezoADC = analogRead(piezoPin);
float piezoV = piezoADC / 1023.0 * 5.0;
// If the value exceeds the threshold, the ball has hit the table
if (!ballHasHitTable && piezoV >= voltageThreshold) {
ballHasHitTable = true;
String voltage = "Hit detected with voltage: " + String(piezoV);
Serial.println(voltage);
}
// check if any player's buttons have been pressed yet
int player1Read = digitalRead(player1ButtonPin);
int player2Read = digitalRead(player2ButtonPin);
int player3Read = digitalRead(player3ButtonPin);
// Determine if any players have won or been disqualified
if (ballHasHitTable) {
checkForWinner( player1Read, player2Read, player3Read);
}
else {
checkForEarlyButtonPresses( player1Read, player2Read, player3Read);
}
}
// Display the winning lights if there is a winner
showWinnerLEDifNeeded();
// Check if the reset button has been pressed
int resetButtonRead = digitalRead(resetButtonPin);
if (resetButtonRead == LOW) {
resetGame();
}
showGameStatusLEDs();
delay(50);
}
//============================================================================
void showGameStatusLEDs() {
if (gameIsPlaying) {
if (ballHasHitTable) {
digitalWrite(gameStatusLedPin1, HIGH);
digitalWrite(gameStatusLedPin2, LOW);
}
else {
digitalWrite(gameStatusLedPin1, LOW);
digitalWrite(gameStatusLedPin2, HIGH);
}
}
else {
digitalWrite(gameStatusLedPin1, LOW);
digitalWrite(gameStatusLedPin2, LOW);
}
}
//============================================================================
void showWinnerLEDifNeeded() {
if (winner == 1) {
digitalWrite(player1LedPin, HIGH);
}
else if (winner == 2) {
digitalWrite(player2LedPin, HIGH);
}
else if (winner == 3) {
digitalWrite(player3LedPin, HIGH);
}
else {
digitalWrite(player1LedPin, LOW);
digitalWrite(player2LedPin, LOW);
digitalWrite(player3LedPin, LOW);
}
}
//============================================================================
void checkForEarlyButtonPresses(int player1Read, int player2Read, int player3Read) {
if (player1Read == LOW && !player1disq) {
player1disq = true;
Serial.println("player 1 too early");
}
if (player2Read == LOW && !player2disq) {
player2disq = true;
Serial.println("player 2 too early");
}
if (player3Read == LOW && !player3disq) {
player3disq = true;
Serial.println("player 3 too early");
}
}
//============================================================================
void checkForWinner(int player1Read, int player2Read, int player3Read) {
if (player1Read == LOW && !player1disq && winner == 0) {
winner = 1;
gameIsPlaying = false;
Serial.println("player 1 wins");
}
else if (player2Read == LOW && !player2disq && winner == 0) {
winner = 2;
gameIsPlaying = false;
Serial.println("player 2 wins");
}
else if (player3Read == LOW && !player3disq && winner == 0) {
winner = 3;
gameIsPlaying = false;
Serial.println("player 3 wins");
}
}
//============================================================================
void resetGame() {
winner = 0;
gameIsPlaying = true;
ballHasHitTable = false;
player1disq = false;
player2disq = false;
player3disq = false;
Serial.println("reset game");
}