-
Notifications
You must be signed in to change notification settings - Fork 2
/
LEDGAME.ino
215 lines (180 loc) · 4.5 KB
/
LEDGAME.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
#include <stdio.h>
int i = 0;
int gameState = 1;
int score = 0;
int noButtPress = 0;
int buttInit[4] = {4, 7, 10, 12} ;
int ledInit[4] = {3, 9, 5, 2} ;
int butt[4];
#define GAME_SET_N_START (1)
#define GAME_RUNNING (2)
#define GAME_END (3)
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
for (int s = 0; s < 4; s++)
{
pinMode(ledInit[s], OUTPUT);
pinMode(buttInit[s], INPUT);
}
}
class Timer
{
public:
unsigned long newTimeValue;
unsigned long limitValue;
unsigned long oldTimeValue;
void updateTimer()
{
newTimeValue = millis();
}
void setTimer(unsigned long timeToSet)
{
oldTimeValue = millis();
limitValue = timeToSet;
}
bool isTimerRunning()
{
newTimeValue = millis();
if ( (newTimeValue - oldTimeValue) <= limitValue)
return true;
else
return false;
}
};
Timer timer;
Timer ledTimer;
Timer waitTimer;
//generating random number for led
int randomGenLed()
{
int num = rand() % 3 + 1;
return num;
}
//generating random time for led
int randomGenTime()
{
int randomtime = (rand() % 9 + 3) * 100;
return randomtime;
}
//turn on random led
int randomLedOn(int num)
{
digitalWrite(ledInit[num], HIGH);
}
//turn off all leds
void ledOff(int num)
{
digitalWrite(ledInit[num], LOW);
}
//буфер с нажатыми кнопками
int WhichButtPressed()
{
for (int c = 1; c < 4; c++)
{
if (digitalRead(buttInit[c]) == HIGH)
{
butt[c] = c;
i = 1;
}
}
}
int Logic(int num)
{
if (butt[num] == num && i == 1)
{
score += 1;
Serial.print(" CORRECT! \n");
//correct
}
else if (butt[num] != num && i == 1)
{
Serial.print(" INCORRECT( \n");
//incorrect
}
else if (ledTimer.isTimerRunning() == false && i == 0)
{
Serial.print(" NO BUTTON PRESS DETECTED \n");
}
}
//clearing butt bufer
void clearButt()
{
butt[0] = 1;
for (int i = 1; i < 4; i++)
{
butt[i] = 0;
}
}
//waiting for start button
void startButtonWait()
{
while (butt[0] == 0)
{
if(digitalRead(buttInit[0]) == HIGH)
{
butt[0] = 1;
}
}
}
void PrintScore()
{
Serial.print(" your score is ");
Serial.print(score);
Serial.print("\n \n");
}
void loop()
{
int b = randomGenLed();
int t = randomGenTime();
int timeleft = timer.limitValue - (timer.newTimeValue - timer.oldTimeValue);
switch (gameState)
{
case GAME_SET_N_START: // the game start
Serial.print(" Please press Start Button!!!\n\n");
startButtonWait(); //waiting for start button
timer.setTimer(15000); //set how long game will last
timer.updateTimer(); // init timer
gameState = GAME_RUNNING;
break;
case GAME_RUNNING: // the game
timer.updateTimer(); // updating timer value
randomLedOn(b); //turn on random led
ledTimer.setTimer(t); //setting time for leds
if ( (timeleft) <= 999)
{
t = timeleft;
gameState = GAME_END;
}
i = 0;
while (ledTimer.isTimerRunning() == true )
{
WhichButtPressed();
}
Logic(b);
clearButt(); //clearing butt bufer
ledOff(b); //turn off led
PrintScore();
waitTimer.setTimer(t);
while ( waitTimer.isTimerRunning() == true )
{
if ( waitTimer.isTimerRunning() == false )
{
break;
}
}
break;
case GAME_END: //game over
Serial.print(" GAME OVER\n");
Serial.print(" your final score is ");
Serial.print(score);
Serial.print(" !!! \n\n\n");
gameState = GAME_SET_N_START;
score = 0;
butt[0] = 0;
break;
default:
break;
}
}