-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.ino
150 lines (119 loc) · 2.81 KB
/
game.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
#include <stdlib.h>
#include <time.h>
#define ledRed 13
#define ledRedBtn 6
#define ledWhite 12
#define ledWhiteBtn 5
#define ledGreen 11
#define ledGreenBtn 4
#define ledYellow 10
#define ledYellowBtn 3
#define ledBlue 9
#define ledBlueBtn 2
#define GameStartBtn 8
int idleLED = 13;
byte currentOutput = HIGH;
void turnOffAllLEDs() {
digitalWrite(ledRed, LOW);
digitalWrite(ledWhite, LOW);
digitalWrite(ledBlue, LOW);
digitalWrite(ledGreen, LOW);
digitalWrite(ledYellow, LOW);
}
void turnOnAllLEDs() {
digitalWrite(ledRed, HIGH);
digitalWrite(ledWhite, HIGH);
digitalWrite(ledBlue, HIGH);
digitalWrite(ledGreen, HIGH);
digitalWrite(ledYellow, HIGH);
}
void Idle() {
if(idleLED == 8 ) {
idleLED = 13;
currentOutput = !currentOutput;
} else {
digitalWrite(idleLED, currentOutput);
}
idleLED--;
}
void GameOver() {
turnOffAllLEDs();
Serial.print("GameOVer!!!\n");
turnOffAllLEDs();
}
void GameClear() {
turnOffAllLEDs();
for(int i = 0; i < 3; i++) {
turnOnAllLEDs();
delay(500);
turnOffAllLEDs();
}
}
bool Game() {
turnOffAllLEDs();
int currentLED = 0;
int curStage = 0;
bool checkCurStage = false;
//Serial.print("GameSTart!!\n");
long time_ = millis();
byte LEDStates[5] = {LOW, };
while(1) {
checkCurStage = false;
currentLED = (rand() % 5) + 9;
if(curStage == 20) {
return true;
}
if((millis() - time_) >= (1000)) {
time_ = millis();
digitalWrite(currentLED, HIGH);
Serial.print("ON!!!!\n");
while((millis() - time_) < (3000 - (50 * curStage))) { // waiting for button inputs (digitalRead)
if(digitalRead(currentLED - 7) == 0) {
Serial.print(currentLED - 7);
Serial.print("Button Pressed!\n");
checkCurStage = true;
curStage++;
break;
}
}
if(!checkCurStage) {
break;
}
time_ = millis();
digitalWrite(currentLED, LOW);
Serial.print("OFF!!!!\n");
}
turnOffAllLEDs();
}
return false;
}
void setup() {
pinMode(ledRed, OUTPUT);
pinMode(ledWhite, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(ledBlue, OUTPUT);
pinMode(ledRedBtn, INPUT_PULLUP);
pinMode(ledWhiteBtn, INPUT_PULLUP);
pinMode(ledGreenBtn, INPUT_PULLUP);
pinMode(ledYellowBtn, INPUT_PULLUP);
pinMode(ledBlueBtn, INPUT_PULLUP);
pinMode(GameStartBtn, INPUT_PULLUP);
//srand((unsigned int)time(NULL));
turnOffAllLEDs();
//MsTimer2::set(1000, Idle);
//MsTimer2::start();
Serial.begin(9600);
}
void loop() {
turnOffAllLEDs();
if(digitalRead(GameStartBtn) == 0) {
Serial.print("GameStart!\n");
if(Game()) {
Serial.print("Game Clear! Congrats!\n");
GameClear();
} else {
GameOver();
}
}
}