-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM.ino
306 lines (256 loc) · 7.4 KB
/
MMM.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//i2c Master Code(UNO)
#include <Wire.h>
// the number of the pushbutton pins
const int buttonPin = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
const int buttonPin6 = 7;
const int startButton = 8;
// the number of the LED pin
const int ledPin = 13;
// the timer LED pin
const int timerT = 11;
// state of the timer LED (on/off)
int ledState = LOW;
// current state of the buttons (pressed/unpressed)
int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int startButtonState = 0;
//Keeps track of who's turn it is
bool playerone = 0;
bool playertwo = 0;
bool playerthree = 0;
// first player of the game
int firstplayer;
// game time
int gameTime = 0;
// state of the game (in progress/ended)
boolean gameState = false;
// determines whether the game has ended or not
boolean endGame = false;
// determines if the ending message has been printed
boolean printMsg = false;
// previous time (for blinking an LED)
unsigned long previousMillis = 0;
// regular blinking time per second
const long interval = 1000;
// faster blinking time
const long faster = 300;
void setup()
{
Wire.begin();
Serial.begin(9600);
// initalize LEDS as outputs:
pinMode(ledPin, OUTPUT);
pinMode(timerT, OUTPUT);
// initialize the pushbuttons as inputs:
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(buttonPin5, INPUT_PULLUP);
pinMode(buttonPin6, INPUT_PULLUP);
pinMode(startButton, INPUT_PULLUP);
// method to help "randomly" pick a first player
randomSeed(analogRead(0));
}
// sets up the beginning of the game
void game() {
// allows printing to the Serial
while(!Serial);
// determines a random amount of time (btwn 20-30 seconds) for the round
gameTime = random(20,30) * 1000;
// if start button was pressed, randomly select the first player
if (startButtonState == 1) {
firstplayer = random(1,4);
// turn on the selected first player's LED:
if(firstplayer == 1) {
Wire.beginTransmission(5);
playerone = 1;
buttonState3 = HIGH;
buttonState = LOW;
buttonState2 = LOW;
Wire.endTransmission();
}
if(firstplayer == 2) {
Wire.beginTransmission(5);
playertwo = 1;
buttonState = HIGH;
buttonState3 = LOW;
buttonState2 = LOW;
Wire.endTransmission();
}
if(firstplayer == 3) {
Wire.beginTransmission(5);
playerthree = 1;
buttonState2 = HIGH;
buttonState = LOW;
buttonState3 = LOW;
Wire.endTransmission();
}
}
// print out the starting player info on Serial
Serial.print("Starting Player: ");
Serial.print(firstplayer);
Serial.println();
}
void loop()
{
// current time in milliseconds
unsigned long currentMillis = millis();
// ------------------------------------------//
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
buttonState6 = digitalRead(buttonPin6);
startButtonState = digitalRead(startButton);
// ------------------------------------------//
// start the game once start button has been pushed
if(gameState == false && startButtonState == 1) {
game();
gameState = true;
}
// restart game if start button has been pushed again at the end of a previous game
if(gameTime <= 0 && startButtonState == 1) {
reset();
game();
endGame = false;
printMsg = false;
gameState = true;
}
// if game has ended, determine & display the loser of the round
if(gameTime == 0 && gameState == true) {
int loser = 0;
if(playerone == 1) {
loser = 1;
} else if(playertwo == 1) {
loser = 2;
} else if(playerthree == 1) {
loser = 3;
}
// print out ending game message & loser
if(printMsg == false) {
Serial.println ("PLAYERS! TIME'S UP!");
Serial.print ("PLAYER ");
Serial.print(loser);
Serial.println(" , YOU LOSE!");
}
// leave timer LED on for 3 seconds before turning off
delay(3000);
digitalWrite(timerT, LOW);
// message has been printed & game has ended
printMsg = true;
endGame = true;
}
// keep timer LED (blue) on while game is in progress & above 6 seconds
if(gameTime !=0 && gameTime >= 6000) {
// check if it is time to blink the LED again
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; //save the last time you blinked the LED
gameTime -= 1000; // decrement the game time by 1 second
ledState = HIGH; // leave LED on
digitalWrite(timerT, ledState);
}
}
// blink fast to signal end of game is near (under 6 seconds)
if(gameTime !=0 && gameTime <= 6000) {
if (currentMillis - previousMillis >= faster) {
//save the last time you blinked the LED
previousMillis = currentMillis;
gameTime -= 1000;
Serial.println(gameTime/1000);
// blink the LED
if(ledState == HIGH) {
ledState = LOW;
} else {
ledState = HIGH;
}
digitalWrite(timerT, ledState);
}
}
// Buttons Presses
if(endGame != true) {
if(playerone == 1 || startButtonState == HIGH){
// First Player, Right (Second LED)
if (buttonState == HIGH) {
Wire.beginTransmission(5);
Wire.write('M');
Wire.write('B');
Wire.endTransmission();
playertwo = 1;
playerone = 0;
}
// First Player, Left (Third LED)
else if (buttonState4 == HIGH) {
Wire.beginTransmission(5);
Wire.write('K');
Wire.write('B');
Wire.endTransmission();
playerthree = 1;
playerone = 0;
}
}
if(playertwo == 1 ||startButtonState == HIGH){
// Second Player, Right (Third LED)
if (buttonState2 == HIGH) {
Wire.beginTransmission(5);
Wire.write('E');
Wire.write('D');
Wire.endTransmission();
playerthree = 1;
playertwo = 0;
}
// Second Player, Left (First LED)
else if(buttonState5 == HIGH) {
Wire.beginTransmission(5);
Wire.write('G');
Wire.write('D');
Wire.endTransmission();
playerone = 1;
playertwo = 0;
}
}
if(playerthree == 1 ||startButtonState == HIGH){
// Third Player, Right (First LED)
if (buttonState3 == HIGH) {
Wire.beginTransmission(5);
Wire.write('A');
Wire.write('F');
Wire.endTransmission();
playerone = 1;
playerthree = 0;
}
// Third Player, Left (Second LED)
else if (buttonState6 == HIGH) {
Wire.beginTransmission(5);
Wire.write('J');
Wire.write('F');
Wire.endTransmission();
playertwo = 1;
playerthree = 0;
}
}
delay(20);
}
}
// resets everything back to 0
void reset(){
playerone = 0;
playertwo = 0;
playerthree = 0;
buttonState = LOW;
buttonState2 = LOW;
buttonState3 = LOW;
buttonState4 = LOW;
buttonState5 = LOW;
buttonState6 = LOW;
}