-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlitherIO_RGB.ino
More file actions
344 lines (300 loc) · 8.14 KB
/
SlitherIO_RGB.ino
File metadata and controls
344 lines (300 loc) · 8.14 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <MPU6050_tockn.h>
#include <Wire.h>
#include <RGBmatrixPanel.h>
#define CLK 8 // USE THIS ON ARDUINO UNO, ADAFRUIT METRO M0, etc.
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);
int gameLevel = 0;
int MAX_LEVEL = 201;
int tail[201][2]; //Head = Position [0,0]. 0 is Position, 1 is the color
int food[201]; //food balls placed all over
int currentSnakeSize = 0;
int currentX = 8;
int currentY = 16;
int game = -1; //1 or 2. Zero means not selected
#define key2 1 //2 = Orange wire
#define key1 11 //1 = Yellow wire
#define key4 12 //4 = Green wire
#define key3 13 //3 = Blue wire
uint16_t color1;
uint16_t color2;
uint16_t color3;
uint16_t color4;
boolean key1bouce = false;
boolean key2bouce = false;
boolean key3bouce = false;
boolean key4bouce = false;
uint16_t selectedColor;
MPU6050 mpu6050(Wire);
unsigned long timerStart;
int TIMEOUT = 60000; //60s
boolean timeRanout = false;
//Double click Key#1 to rest
unsigned long lastTimeKey1Pressed;
int key1PressCount = 0; //resets in 2s
int addToTail(int pos, uint16_t color333) {
int pop = tail[currentSnakeSize][0]; //last item in tail
uint16_t popColor = tail[currentSnakeSize][1];
for (int i = currentSnakeSize; i > 0; i--) {
//Shift the tail to make room on head;
tail[i][0] = tail[i - 1][0];
tail[i][1] = tail[i - 1][1];
}
tail[0][0] = pos;
tail[0][1] = color333;
return pop;
}
void deleteFromTail(int pos) {
for (int i = 0; i < MAX_LEVEL; i++) {
if (tail[i][0] == pos) {
tail[i][0] = -1;
tail[i][1] = matrix.Color333(0, 7, 0);
break;
}
}
return;
}
boolean isTouchingTheTail(int pos) {
boolean touching = false;
for (int i = 1; i <= currentSnakeSize; i++) {
if (tail[i][0] == pos) {
touching = true;
break;
}
}
return touching;
}
void displayFood(boolean resetDisplay) {
if (resetDisplay) {
clearScreen();
}
for (int i = 0; i < MAX_LEVEL; i++) {
if (food[i] >= 0) {
int row = food[i] % 16; //y axis (0 to 7)
int col = food[i] / 16; //x axis (0 to 31)
matrix.drawPixel(col, row, matrix.Color333(7, 0, 0));
}
}
}
boolean isFood(int pos) {
boolean found = false;
for (int i = 0; i < MAX_LEVEL; i++) {
if (food[i] == pos) {
found = true;
break;
}
}
return found;
}
void eatFood(int pos) {
for (int i = 0; i < MAX_LEVEL; i++) {
if (food[i] == pos) {
food[i] = -1;
break;
}
}
}
void addFood(int pos) {
for (int i = 0; i < MAX_LEVEL; i++) {
if (food[i] == -1) {
//empty location
food[i] = pos;
break;
}
}
}
void setup() {
Serial.begin(9600);
Serial.println("Slither IO");
Wire.begin();
pinMode(key1, INPUT_PULLUP);// set pin as input
pinMode(key2, INPUT_PULLUP);// set pin as input
pinMode(key3, INPUT_PULLUP);// set pin as input
pinMode(key4, INPUT_PULLUP);// set pin as input
attachInterrupt(digitalPinToInterrupt(key1),key1Pressed,FALLING); //CHANGE, RISING, FALLING, LOW, HIGH
attachInterrupt(digitalPinToInterrupt(key2),key2Pressed,FALLING);
attachInterrupt(digitalPinToInterrupt(key3),key3Pressed,FALLING);
attachInterrupt(digitalPinToInterrupt(key4),key4Pressed,FALLING);
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
randomSeed(analogRead(0));
matrix.begin();
displayStart();
}
void loop() {
// delay(50);
mpu6050.update();
int yAngle = mpu6050.getAccAngleY();
int xAngle = mpu6050.getAccAngleX();
unsigned long delta = millis() - lastTimeKey1Pressed;
if (delta > 2000) {
key1PressCount = 0; //resets to 0 after 2s
}
if (key1PressCount > 1 && game != -1) {
Serial.println("Reset Game");
key1PressCount = 0; //we got the reset intention, reset click count
game = -1;
resetDrawScreen();
currentX = 8;
currentY = 16;
return;
}
if (key1bouce) {
delay(100); key1bouce = false;
}
// Serial.print("accAngleX : "); Serial.print(xAngle);
// Serial.print("\taccAngleY : "); Serial.print(yAngle);
currentX = (xAngle < -8) ? currentX + 1 : ((xAngle > 8) ? currentX - 1 : currentX);
currentY = (yAngle < -6) ? currentY + 1 : ((yAngle > 6) ? currentY - 1 : currentY);
currentX = (currentX > 15) ? 15 : ((currentX < 0) ? 0 : currentX);
currentY = (currentY > 31) ? 31 : ((currentY < 0) ? 0 : currentY);
unsigned int currentPos = currentX + 16*currentY;
// Serial.print("\tX : "); Serial.print(currentX);
// Serial.print("\tY : "); Serial.print(currentY);
// Serial.print("\tCurrentPos : "); Serial.println(currentPos);
// Serial.print("Tail: ");
// for (int i = 0; i <= currentSnakeSize; i++) {
// Serial.print(tail[i]);
// Serial.print(" ");
// }
// Serial.println();
switch (game) {
case -1:
for (int i = 0; i<MAX_LEVEL; i++) {
tail[i][0] = -1;
tail[i][1] = matrix.Color333(0, 7, 0);
food[i] = -1;
}
currentX = 0;
currentPos = currentX + 16*currentY;
clearScreen();
displayGameStart();
game = 0;
break;
case 0:
displayGameStart();
selectGame(currentPos);
break;
case 1:
playGame1(currentPos);
break;
case 2:
playGame2(currentPos);
break;
case 3:
playDice();
break;
case 4:
draw(currentPos);
break;
// case 5:
// loopAudio();
// break;
}
}
void blinkLED(int col, int row, uint16_t color333, boolean on=true) {
if (on) {
for (int i = 0; i < 2; i++) {
matrix.drawPixel(col, row, matrix.Color333(0, 0, 0));
delay(40);
matrix.drawPixel(col, row, color333);
delay(40);
}
} else {
for (int i = 0; i < 2; i++) {
matrix.drawPixel(col, row, color333);
delay(40);
matrix.drawPixel(col, row, matrix.Color333(0, 0, 0));
delay(40);
}
}
}
void selectGame(int currentPos) {
// Serial.print("Select Game");
// Serial.print("\tCurrentPos : "); Serial.println(currentPos);
int clearPos = addToTail(currentPos, matrix.Color333(0, 7, 0));
int row = clearPos % 16; //y axis (0 to 7)
int col = clearPos / 16; //x axis (0 to 31)
// Serial.print("\tclearPos : "); Serial.println(clearPos);
matrix.drawPixel(col, row, matrix.Color333(0, 0, 0));
//set the currentPos
row = currentPos % 16; //y axis (0 to 7)
col = currentPos / 16; //x axis (0 to 31)
blinkLED(col,row, matrix.Color333(0, 7, 0));
if ((row >4 ) && (row <= 10)) {
if ((col >=5 ) && (col <= 7)) {
game = 1;
resetGame1();
} else if ((col >= 10) & (col <= 14)) {
game = 2;
resetGame2();
} else if ((col >= 16) & (col <= 20)) {
game = 3;
setupDice();
} else if ((col >= 22) & (col <= 26)) {
game = 4;
resetDrawScreen();
// } else if ((col >= 25) & (col <= 29)) {
// game = 5;
// resetDrawScreen();
// setupAudio();
}
}
}
void key1Pressed() {
if (!key1bouce) {
key1bouce = true;
Serial.print(millis());
Serial.println("\tkey 1 is pressed");
selectedColor = color1;
lastTimeKey1Pressed = millis();
key1PressCount +=1;
}
}
void key2Pressed() {
if (!key2bouce) {
key2bouce = true;
Serial.print(millis());
Serial.println("\tkey 2 is pressed");
selectedColor = color2;
}
}
void key3Pressed() {
if (!key3bouce) {
key3bouce = true;
Serial.print(millis());
Serial.println("\tkey 3 is pressed");
selectedColor = color3;
}
}
void key4Pressed() {
if (!key4bouce) {
key4bouce = true;
Serial.print(millis());
Serial.println("\tkey 4 is pressed");
selectedColor = color4;
}
}
void timeoutBar() {
unsigned long timePassed = millis() - timerStart;
int ledToLightUp = map(timePassed, 0, TIMEOUT, 0,32);
switch (game) {
case 1:
matrix.drawLine(0, 15, ledToLightUp, 15, matrix.Color333(1, 1, 1));
if (ledToLightUp >=32) {
timeRanout = true;
}
break;
case 2:
case 4:
matrix.drawPixel(ledToLightUp, 15, matrix.Color333(1, 1, 1));
if (ledToLightUp >=32) {
timeRanout = true;
}
break;
}
}