-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcatchPhrase.ino
229 lines (198 loc) · 5.78 KB
/
catchPhrase.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
#include <LiquidCrystal.h> // include the library code:
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
//counting vars
int newGame = 0;
int timerCounter = 0;
int timerMaxCount = 60;
int teamOneReadScore = 0;
int teamTwoReadScore = 0;
int teamOneWins = 0;
int teamTwoWins = 0;
//setting up the buzzer
const int buzzerPin = 10; // the number of the buzzer pin
int buzzerState = LOW; // buzzerState used to set the buzzer
long previousMillis = 0; // will store last time buzzer was updated
long buzzerInterval = 300; // buzzerInterval (milliseconds)
// setting up start button
int startButtonPin = 9;
int startButtonPushCounter = 0; // counter for the number of button presses
int startButtonState = 0; // current state of the button
int startButtonLastestState = 0; // previous state of the button
// setting up next button
int nextButtonPin = 8;
int nextButtonPushCounter = 0;
int nextButtonState = 0;
int nextButtonLastestState = 0;
// setting up teamOne button
int teamOneButtonPin = 7;
int teamOneButtonPushCounter = 0;
int teamOneButtonState = 0;
int teamOneButtonLastestState = 0;
// setting up teamTwo button
int teamTwoButtonPin = 6;
int teamTwoButtonPushCounter = 0;
int teamTwoButtonState = 0;
int teamTwoButtonLastestState = 0;
String words[] = {
"Mona Lisa","kitchen","wig","tree house","java script","grover","orange","fun","asha","hotdog","canada","wrist","red","apple","ape","mom","love","fart","kortina","lcd"}; // an array of words for catchphrase
int maxSize = sizeof(words)/sizeof(String);
int interval = 0;
void setup() {
lcd.begin(2,16);
shuffle();
pinMode(startButtonPin, INPUT); // button as input
digitalWrite(startButtonPin, HIGH); // turns on pull-up resistor after input
pinMode(nextButtonPin, INPUT);
digitalWrite(nextButtonPin, HIGH);
pinMode(teamOneButtonPin, INPUT);
digitalWrite(teamOneButtonPin, HIGH);
pinMode(teamTwoButtonPin, INPUT);
digitalWrite(teamTwoButtonPin, HIGH);
pinMode(buzzerPin, OUTPUT); //buzzer setup
lcd.print("Press Start!");
}
void loop() {
printScore();
timerTone();
startButtonState = digitalRead(startButtonPin);
if (startButtonState != startButtonLastestState) {
if(digitalRead(startButtonPin) == LOW){
if(timerCounter == 0){
startGame();
}
else {
digitalWrite(buzzerPin, LOW);
timerCounter = 0;
}
}
}
startButtonLastestState = startButtonState;
nextButtonState = digitalRead(nextButtonPin);
if (nextButtonState != nextButtonLastestState) {
if(digitalRead(nextButtonPin) == LOW){
printWord();
}
}
nextButtonLastestState = nextButtonState;
teamOneButtonState = digitalRead(teamOneButtonPin);
if (teamOneButtonState != teamOneButtonLastestState) {
if(digitalRead(teamOneButtonPin) == LOW){
teamOneScore();
}
}
teamOneButtonLastestState = teamOneButtonState;
teamTwoButtonState = digitalRead(teamTwoButtonPin);
if (teamTwoButtonState != teamTwoButtonLastestState) {
if(digitalRead(teamTwoButtonPin) == LOW){
teamTwoScore();
}
}
teamTwoButtonLastestState = teamTwoButtonState;
}
// shuffle the list of words
void shuffle(){
for (int a=0; a<maxSize; a++){
int r = random(a+1);
String temp = words[a];
words[a] = words[r];
words[r] = temp;
}
}
//start the game
void startGame() {
lcd.clear();
lcd.print(words[interval]);
interval++;
newGame++;
timerCounter ++;
if (interval == maxSize) {
interval = 0;
shuffle();
}
}
//print the word
void printWord() {
if (timerCounter !=0) {
lcd.clear();
lcd.print(words[interval]);
interval++;
if (interval == maxSize) {
interval = 0;
shuffle();
}
}
}
//timer with buzzer
void timerTone() {
unsigned long currentMillis = millis();
if (timerCounter > 0) {
if (timerCounter < timerMaxCount) {
if(currentMillis - previousMillis > buzzerInterval) {
previousMillis = currentMillis;
if (buzzerState == LOW){
buzzerState = HIGH;
}
else{
buzzerState = LOW;
}
digitalWrite(buzzerPin, buzzerState);
timerCounter++;
}
}
}
if (timerCounter == timerMaxCount) {
digitalWrite(buzzerPin, LOW);
timerCounter = 0;
}
}
//teamOne score
void teamOneScore() {
if (newGame != 0) {
teamOneReadScore++;
lcd.setCursor(0, 1); // bottom left
lcd.print("One: ");
lcd.print(teamOneReadScore);
if (teamOneReadScore > 10) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Team One wins!");
newGame = 0;
teamOneReadScore = 0;
teamTwoReadScore = 0;
teamOneWins = 1;
timerCounter = 0;
digitalWrite(buzzerPin, LOW);
}
}
}
//teamTwo score
void teamTwoScore() {
if (newGame != 0) {
teamTwoReadScore++;
lcd.setCursor(9, 1); // bottom right
lcd.print("Two: ");
lcd.print(teamTwoReadScore);
if (teamTwoReadScore > 10) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Team Two wins!");
newGame = 0;
teamOneReadScore = 0;
teamTwoReadScore = 0;
teamTwoWins = 1;
timerCounter = 0;
digitalWrite(buzzerPin, LOW);
}
}
}
//print score
void printScore() {
if (newGame > 0) {
lcd.setCursor(0, 1); // bottom left
lcd.print("One: ");
lcd.print(teamOneReadScore);
lcd.setCursor(9, 1); // bottom right
lcd.print("Two: ");
lcd.print(teamTwoReadScore);
}
}