-
Notifications
You must be signed in to change notification settings - Fork 0
/
genius.ino
228 lines (190 loc) · 4.25 KB
/
genius.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
/*
REFERENCE
digital 6 --> 0
digital 7 --> 1
digital 8 --> 2
digital 9 --> 3
*/
#define debug false
/* game */
boolean started = false;
/* LEDS */
int ledOnTime = 300;
int highLeds[20];
int yourLeds[20];
int index = 0;
int indexHighLeds = 1;
boolean finishedArray = false;
void randomStartStage();
void startFinishGame();
void startLeds(int port, int code);
void showLEDS();
void getYourLEDS();
void displayLed(int port);
void gameOver();
void restart();
void setup() {
for (int i = 6; i < 10; i++){ pinMode(i, OUTPUT); }
pinMode(10, INPUT_PULLUP);
for (int i = 2; i < 6; i++){ pinMode(i, INPUT_PULLUP); }
Serial.begin(9600);
}
void loop() {
if(!started){ randomStartStage(); }
Serial.println("clicado --> ");
Serial.print(digitalRead(10));
Serial.print(digitalRead(5));
Serial.print(digitalRead(4));
Serial.print(digitalRead(3));
Serial.print(digitalRead(2));
if(debug){
Debug();
}else{
if(digitalRead(10) == HIGH && started == false){ startFinishGame(); }
if(digitalRead(10) == LOW) {
Serial.println(" -- started -- ");
started = true;
}
if (started){
if(!finishedArray){ showLEDS(); }
if(index <= indexHighLeds){getYourLEDS(); }
else{ next(); }
delay(200);
}
}
}
void randomStartStage(){
highLeds[0] = random(4);
highLeds[1] = random(4);
Serial.println("<----- Random Start Stage ----->");
Serial.print(highLeds[0]);
Serial.print(highLeds[1]);
for (int i = 2; i < 20; i++){ highLeds[i] = -1; }
}
void startFinishGame(){
for (int i = 6; i < 10; i++){
digitalWrite(i, HIGH);
delay(600);
}
for (int i = 6; i < 10; i++){
digitalWrite(i, LOW);
delay(600);
}
}
void gameOver(){
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
delay(3000);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
restart();
}
void restart(){
started = false;
ledOnTime = 300;
for(int j = 0; j < 21; j++){
highLeds[j] = -1;
yourLeds[j] = -1;
}
index = 0;
indexHighLeds = 1;
finishedArray = false;
}
void startLeds(int port, int code){
Serial.println("cosisosososo --> ");
Serial.print(index, indexHighLeds);
Serial.print(code);
if(highLeds[index] != code){ gameOver(); }
else{
if(index == 19){ win(); }
else{
digitalWrite(port, HIGH);
yourLeds[index] = code;
index++;
}
}
}
void Debug(){
if(digitalRead(5) == LOW){
digitalWrite(6, HIGH);
delay(1000);
digitalWrite(6, LOW);
}
if(digitalRead(4) == LOW){
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
}
if(digitalRead(3) == LOW){
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
}
if(digitalRead(2) == LOW){
digitalWrite(7, HIGH);
delay(1000);
digitalWrite(7, LOW);
}
}
void next(){
index = 0;
indexHighLeds ++;
int nextLed = random(4);
highLeds[indexHighLeds] = nextLed;
finishedArray = false;
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
//6, 8, 9, 7
void win(){
digitalWrite(6, HIGH);
delay(600);
digitalWrite(6, LOW);
digitalWrite(8, HIGH);
delay(600);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
delay(600);
digitalWrite(9, LOW);
digitalWrite(7, HIGH);
delay(600);
digitalWrite(7, LOW);
delay(100);
restart();
}
void showLEDS(){
delay(500);
int i = 0;
while (!finishedArray){
if(highLeds[i] == 0){ displayLed(6); }
else if(highLeds[i] == 1){ displayLed(7); }
else if(highLeds[i] == 2){ displayLed(8); }
else if(highLeds[i] == 3){ displayLed(9); }
else if(highLeds[i] == -1){ finishedArray = true; }
delay(100);
i ++;
}
}
void getYourLEDS(){
Serial.println("index");
Serial.println(index);
if(digitalRead(5) == LOW){ startLeds(6, 0); }
else { digitalWrite(6, LOW); }
if(digitalRead(4) == LOW){ startLeds(8, 2); }
else { digitalWrite(8, LOW); }
if(digitalRad(3) == LOW){ startLeds(9, 3); }
else { digitalWrite(9, LOW); }
if(digitalRead(2) == LOW){ startLeds(7, 1); }
else { digitalWrite(7, LOW); }
}
void displayLed(int port){
digitalWrite(port, HIGH);
delay(ledOnTime);
digitalWrite(port, LOW);
}