-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsommercamp16.ino
829 lines (757 loc) · 22.8 KB
/
sommercamp16.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
#include <Wire.h>
#include <EEPROM.h>
#include <assert.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#define CHECK_BIT(var, pos) ((var) & (1<<(pos)))
//Pins
#define goLeftPin 7
#define goRightPin 6
#define tasterPin 13
#define leftSonarTrig 11
#define leftSonarEcho 12
#define rightSonarTrig 9
#define rightSonarEcho 10
#define buzzerPin 8
/*************************************************
* Noten
*************************************************/
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_E6 1319
#define NOTE_G6 1568
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_D7 2349
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_G7 3136
#define NOTE_A7 3520
//Display
Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();
static const uint8_t PROGMEM
menu_0[] = {
B00011000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00011000},
menu_1[] = {
B00000000,
B00000000,
B00011000,
B00111100,
B00111100,
B01111110,
B00000000,
B00000000},
menu_2[] = {
B00000000,
B00000000,
B01010101,
B01010101,
B01010101,
B01010101,
B00000000,
B00000000},
menu_3[] = {
B00000000,
B00011110,
B00000010,
B01000010,
B11100010,
B01000010,
B01111110,
B00000000},
menu_pause[] = {
B00000000,
B01111110,
B01111110,
B00000000,
B00000000,
B01111110,
B01111110,
B00000000};
//Own Song - ImperialMarch
int starwars_notes[] = {
NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
NOTE_AS3, NOTE_AS4, 0,
0,
NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
NOTE_AS3, NOTE_AS4, 0,
0,
NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
NOTE_DS3, NOTE_DS4, 0,
0,
NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
NOTE_DS3, NOTE_DS4, 0,
0, NOTE_DS4, NOTE_CS4, NOTE_D4,
NOTE_CS4, NOTE_DS4,
NOTE_DS4, NOTE_GS3,
NOTE_G3, NOTE_CS4,
NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
NOTE_GS4, NOTE_DS4, NOTE_B3,
NOTE_AS3, NOTE_A3, NOTE_GS3,
0, 0, 0,
////
////
NOTE_A4, NOTE_A4, NOTE_A4, NOTE_F4, NOTE_C5,
NOTE_A4, NOTE_F4, NOTE_C5, NOTE_A4,
NOTE_E5, NOTE_E5, NOTE_E5, NOTE_F5, NOTE_C5,
NOTE_GS4, NOTE_F4, NOTE_C5, NOTE_A4,
NOTE_A5, NOTE_A4, NOTE_A4, NOTE_A5, NOTE_GS5, NOTE_GS5,
NOTE_FS5, NOTE_F5, NOTE_FS5, 0, NOTE_AS4, NOTE_DS4, NOTE_D5, NOTE_CS5,
//
NOTE_C5, NOTE_B4, NOTE_C5, 0, NOTE_F5, NOTE_GS4, NOTE_F4, NOTE_A4,
NOTE_C5, NOTE_A4, NOTE_C5, NOTE_E5,
NOTE_A5, NOTE_A4, NOTE_A4, NOTE_A5, NOTE_GS5, NOTE_G5,
NOTE_FS5, NOTE_F5, NOTE_F5, 0, NOTE_AS4, NOTE_DS5, NOTE_D5, NOTE_CS5,
NOTE_C5, NOTE_B4, 0, NOTE_F4, NOTE_GS4, NOTE_F4, NOTE_C5,
NOTE_A4, NOTE_F4, NOTE_C5, NOTE_A4,
//
NOTE_A4, NOTE_A4, NOTE_A4, NOTE_F4, NOTE_C5,
NOTE_A4, NOTE_F4, NOTE_C5, NOTE_A4,
NOTE_E5, NOTE_E5, NOTE_E5, NOTE_F5, NOTE_C5,
NOTE_GS4, NOTE_F4, NOTE_C5, NOTE_A4,
NOTE_A5, NOTE_A4, NOTE_A4, NOTE_A5, NOTE_GS5, NOTE_GS5,
NOTE_FS5, NOTE_F5, NOTE_FS5, 0, NOTE_AS4, NOTE_DS4, NOTE_D5, NOTE_CS5,
//
NOTE_C5, NOTE_B4, NOTE_C5, 0, NOTE_F4, NOTE_A4, NOTE_F4, NOTE_C5,
NOTE_GS4, NOTE_F4, NOTE_C5, NOTE_A4,
////
////
NOTE_E7, NOTE_E7, 0, NOTE_E7,
0, NOTE_C7, NOTE_E7, 0,
NOTE_G7, 0, 0, 0,
NOTE_G6, 0, 0, 0,
NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0,
0, NOTE_A6, 0, NOTE_B6,
0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7,
NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0, NOTE_C7,
NOTE_D7, NOTE_B6, 0, 0,
NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0,
0, NOTE_A6, 0, NOTE_B6,
0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7,
NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0, NOTE_C7,
NOTE_D7, NOTE_B6, 0, 0
};
int starwars_tempo[] = {
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
6, 18, 18, 18,
6, 6,
6, 6,
6, 6,
18, 18, 18, 18, 18, 18,
10, 10, 10,
10, 10, 10,
3, 3, 3,
////
////
4, 4, 4, 6, 16,
4, 6, 16, 2,
4, 4, 4, 6, 16,
4, 6, 16, 2,
4, 6, 16, 4, 6, 16,
16, 16, 8, 8, 8, 4, 6, 16,
//
16, 16, 8, 8, 8, 4, 6, 16,
4, 6, 16, 2,
4, 6, 16, 4, 6, 16,
16, 16, 8, 8, 8, 4, 6, 16,
16, 16, 8, 8, 8, 4, 6, 16,
4, 6, 16, 2,
//
4, 4, 4, 6, 16,
4, 6, 16, 2,
4, 4, 4, 6, 16,
4, 6, 16, 2,
4, 6, 16, 4, 6, 16,
16, 16, 8, 8, 8, 4, 6, 16,
//
16, 16, 8, 8, 8, 4, 6, 16,
4, 6, 16, 2,
////
////
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
};
static uint8_t mapArray[7] = {B10000001};
byte mapbuffer[7];
enum displayableModes {
M_PLAY, M_CREDITS, M_RESTART
};
enum modes {
MENU, GAME, CREDITS, BREAK, START
};
enum sides {
LEFT, RIGHT
};
enum move {
TOLEFT, STAY, TORIGHT
};
int currModeSel = M_PLAY;
int leftBorderPosition = 2;
int rightBorderPosition = 6;
int bottomLeftBorderPosition = 0;
int bottomRightBorderPosition = 7;
int playerPosition = 4;
int currMode = START;
long buttonPressedTime[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool buttonPressed[14] = {false, false, false, false, false, false, false, false, false, false, false, false, false,
false};
long lastStepTime = 0;
long lastMapStepTime = 0;
int barLength = 1;
long lastStepPlayedTime = 0;
long currentDelay = 0;
long currentNote = 0;
long timeGameStart = 0;
int gameTasterMode = false;
long sonarLeftData = 0, sonarLeftCount = 0;
long sonarRightData = 0, sonarRightCount = 0;
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}
long getSonarData(int side) {
int trig, echo;
if (side == LEFT) {
trig = leftSonarTrig;
echo = leftSonarEcho;
} else if (side == RIGHT) {
trig = rightSonarTrig;
echo = rightSonarEcho;
} else return -1;
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(5);
digitalWrite(trig, LOW);
pinMode(echo, INPUT);
long duration = pulseIn(echo, HIGH);
long distance = microsecondsToCentimeters(duration);
return distance;
}
void doSensorData(int side) {
if (side == LEFT) {
sonarLeftCount++;
sonarLeftData += getSonarData(side);
} else if (side == RIGHT) {
sonarRightCount++;
sonarRightData += getSonarData(side);
}
}
int getSonarBetterData(int side) {
if (side == LEFT) {
int data = (int) (sonarLeftData / sonarLeftCount);
sonarLeftCount = 0;
sonarLeftData = 0;
return data;
} else if (side == RIGHT) {
int data = (int) (sonarRightData / sonarRightCount);
sonarRightCount = 0;
sonarRightData = 0;
return data;
}
}
bool buttonShortPressed(int taster, bool onlythis = true) {
bool pressed = digitalRead(taster) == LOW;
Serial.print(millis()); Serial.print(": Testfor Button "); Serial.print(taster); Serial.print(": "); Serial.print(pressed); Serial.print("; buttonPressed[]: "); Serial.print(buttonPressed[taster]); Serial.print("; buttonPressedTime[]: "); Serial.print(buttonPressedTime[taster]);
if (pressed && !buttonPressed[taster]) {
buttonPressed[taster] = true;
buttonPressedTime[taster] = millis();
Serial.println("; Output: false");
return false;
} else if (!pressed && buttonPressed[taster]) {
//Serial.println((millis() - buttonPressedTime[taster]));
if ((millis() - buttonPressedTime[taster]) <= 500) {
buttonPressed[taster] = false;
Serial.println("; Output: true");
return true;
} else {
if(onlythis) buttonPressed[taster] = false;
Serial.println("; Output: false");
return false;
}
}
Serial.println("; Output: false");
return false;
}
bool buttonLongPressed(int taster) {
bool pressed = digitalRead(taster) == LOW;
Serial.print("Testfor Button "); Serial.print(taster); Serial.print(": "); Serial.print(pressed); Serial.print("; buttonPressed[]: "); Serial.print(buttonPressed[taster]); Serial.print("; buttonPressedTime[]: "); Serial.print(buttonPressedTime[taster]);
if (pressed && !buttonPressed[taster]) {
buttonPressed[taster] = true;
buttonPressedTime[taster] = millis();
Serial.println("; Output: false");
return false;
} else if (!pressed && buttonPressed[taster]) {
Serial.println((millis() - buttonPressedTime[taster]));
buttonPressed[taster] = false;
if ((millis() - buttonPressedTime[taster]) > 500) {
Serial.println("; Output: true");
return true;
} else {
Serial.println("; Output: false");
return false;
}
}
Serial.println("; Output: false");
return false;
}
int randint(int n) {
if ((n - 1) == RAND_MAX) {
return rand();
} else {
long end = RAND_MAX / n; // truncate skew
assert (end > 0L);
end *= n;
int r;
while ((r = rand()) >= end);
return r % n;
}
}
void displayText(String text, int pixelLength) {
//Zeigt einen Text auf dem Display
matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely
matrix.setTextSize(1);
matrix.setTextColor(LED_GREEN);
matrix.setRotation(3);
int length = pixelLength * -1;
for (int8_t x = 7; x >= length; x--) {
matrix.clear();
matrix.setCursor(x, 0);
matrix.print(text);
matrix.writeDisplay();
delay(100);
if (buttonShortPressed(tasterPin)) break;
}
matrix.setRotation(0);
}
void loading() {
matrix.clear();
if ((millis() - lastStepTime) >= 100) {
barLength = (barLength + 1) % 8;
lastStepTime = millis();
}
matrix.setRotation(3);
matrix.drawLine(0, 6, barLength, 6, LED_GREEN);
matrix.writeDisplay();
matrix.setRotation(0);
}
byte generateNewMapLine() {
int side = randint(3);
int leftBorder = leftBorderPosition;
int rightBorder = rightBorderPosition;
switch (side) {
case TOLEFT:
if (leftBorder - 1 >= 0) {
leftBorder--;
rightBorder--;
}
break;
case STAY:
break;
case TORIGHT:
if (rightBorder + 1 <= 7) {
leftBorder++;
rightBorder++;
}
break;
}
byte thisline = B00000000;
thisline |= 1 << leftBorder;
thisline |= 1 << rightBorder;
leftBorderPosition = leftBorder;
rightBorderPosition = rightBorder;
Serial.print("Mapgen: ");
Serial.println(thisline, BIN);
return thisline;
}
void preGenerateMap() {
//generiert den Anfang der Map
Serial.println("Startgen");
for (int i = 0; i < 7; i++) {
loading();
mapbuffer[i] = B10000001;
delay(100);
}
Serial.println("Endgen.");
}
int limitInt(int limit, int value) {
if (value >= limit) return limit;
else return value;
}
void updateDistances(int left, int right) {
if (gameTasterMode == 1) {
matrix.setRotation(3);
matrix.drawLine(0, 0, 8, 0, LED_RED);
matrix.setRotation(0);
} else {
matrix.setRotation(3);
int lineLengthLeft = limitInt(4, (int) ((left / 10.0) + .5));
int lineLengthRight = limitInt(4, (int) ((right / 10.0) + .5));
//Serial.println("updateSensor: ");
//Serial.print("Distance: "); Serial.print(left); Serial.print(" | "); Serial.println(right);
Serial.print("Length: "); Serial.print(lineLengthLeft); Serial.print(" | "); Serial.println(lineLengthRight);
matrix.drawLine(-1, 0, lineLengthLeft, 0, LED_YELLOW);
matrix.drawLine(8 - lineLengthRight, 0, 7, 0, LED_YELLOW);
matrix.setRotation(0);
}
}
void updatePlayer() {
if (gameTasterMode == 1) {
if (buttonShortPressed(goLeftPin) && playerPosition - 1 >= 0) playerPosition--;
if (buttonShortPressed(goRightPin) && playerPosition + 1 <= 7) playerPosition++;
} else if ((millis() - lastStepTime) >= 250) {
int lineLengthLeft = limitInt(4, (int) ((getSonarBetterData(LEFT) / 10.0) + .5));
int lineLengthRight = limitInt(4, (int) ((getSonarBetterData(RIGHT) / 10.0) + .5));
int heightDifference = lineLengthRight - lineLengthLeft;
//Serial.print("Difference: "); Serial.println(heightDifference);
playerPosition = 4 - heightDifference;
lastStepTime = millis();
}
matrix.setRotation(3);
matrix.drawPixel(playerPosition, 7, LED_RED);
matrix.setRotation(0);
}
void updateMap() {
int time = 500;
if (gameTasterMode == 1) time = 100;
if ((millis() - lastMapStepTime) >= time) {
//Shift map down
byte temp[7];
for (int i = 0; i < 6; i++) {
temp[i] = mapbuffer[i + 1];
}
temp[6] = (byte) generateNewMapLine();
//Write to map buffer
for (int i = 0; i < 7; i++) {
mapbuffer[i] = temp[i];
}
lastMapStepTime = millis();
bool first = false;
for (int i = 0; i < 8; i++) {
if (CHECK_BIT(mapbuffer[0], i)) if (!first) {
first = true;
bottomLeftBorderPosition = i;
} else {
bottomRightBorderPosition = i;
return;
}
}
bottomLeftBorderPosition = 0;
bottomRightBorderPosition = 7;
}
}
void displayMap() {
matrix.setRotation(1);
uint8_t PROGMEM bitmap[7];
for (int i = 0; i < 7; i++) {
bitmap[i] = mapbuffer[i];
}
int color = LED_GREEN;
if(gameTasterMode == 1) {
int score = (int) ((millis() - timeGameStart) / 1000 * 5);
int highscore = 0;
EEPROM.get(0x10, highscore);
if (score > highscore) {
color = LED_YELLOW;
}
} else {
int score = (int) ((millis() - timeGameStart) / 1000 * 20);
int highscore = 0;
EEPROM.get(0x10, highscore);
if (score > highscore) {
color = LED_YELLOW;
}
}
matrix.drawBitmap(0, 0, bitmap, 8, 7, color);
matrix.setRotation(0);
}
void startUp() {
//Startet ein neues Spiel
displayText("Heyho!", 36);
preGenerateMap();
currMode = MENU;
for (int i = 0; i < 7; i++) {
mapbuffer[i] = mapArray[i];
}
}
void reset() {
//Resettet alle Variablen auf die Startwerte
matrix.clear();
matrix.drawRect(0, 0, 8, 8, LED_RED);
matrix.drawRect(1, 1, 6, 6, LED_RED);
matrix.drawRect(2, 2, 4, 4, LED_RED);
matrix.drawRect(3, 3, 2, 2, LED_RED);
matrix.writeDisplay();
EEPROM.put(0x10, 0);
delay(500);
int currModeSel = M_PLAY;
int leftBorderPosition = 2;
int rightBorderPosition = 6;
int bottomLeftBorderPosition = 0;
int bottomRightBorderPosition = 7;
int playerPosition = 4;
int currMode = START;
long buttonPressedTime[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool buttonPressed[14] = {false, false, false, false, false, false, false, false, false, false, false, false, false,
false};
long lastStepTime = 0;
long lastMapStepTime = 0;
int barLength = 1;
long lastStepPlayedTime = 0;
long currentDelay = 0;
long currentNote = 0;
long timeGameStart = 0;
long sonarLeftData = 0, sonarLeftCount = 0;
long sonarRightData = 0, sonarRightCount = 0;
startUp();
}
void buzz(int targetPin, long frequency, long length) {
digitalWrite(buzzerPin, HIGH);
long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait again or the calculated delay value
}
digitalWrite(buzzerPin, LOW);
}
void playMusicStep() {
if ((millis() - lastStepPlayedTime) >= currentDelay) {
int size = sizeof(starwars_notes) / sizeof(int);
if (currentNote < size) {
currentNote++;
int noteDuration = 1000 / starwars_tempo[currentNote];
currentDelay = 1000000 / starwars_notes[currentNote] / 2;
//tone(buzzerPin, starwars_notes[currentNote], noteDuration);
if(starwars_notes[currentNote] != 0) buzz(buzzerPin, starwars_notes[currentNote], noteDuration);
currentDelay = (long) (noteDuration * 1.30);
lastStepPlayedTime = millis();
} else {
currentNote = 0;
}
} else {
}
}
void loopMenu() {
if (buttonShortPressed(tasterPin, false)) {
currModeSel = (currModeSel + 1) % 3;
}
if (buttonLongPressed(tasterPin)) {
switch (currModeSel) {
case M_PLAY:
preGenerateMap();
currMode = GAME;
timeGameStart = millis();
break;
case M_CREDITS:
currMode = CREDITS;
displayText("Copyright", 52);
displayText("Janik&Yannick", 75);
displayText("Sommercamp 2016", 96);
currMode = MENU;
break;
case M_RESTART:
reset();
break;
}
}
switch (currModeSel) {
case M_PLAY:
matrix.clear();
matrix.drawBitmap(0, 0, menu_0, 8, 8, LED_GREEN);
matrix.drawBitmap(0, 0, menu_1, 8, 8, LED_RED);
matrix.writeDisplay();
break;
case M_CREDITS:
matrix.clear();
matrix.drawBitmap(0, 0, menu_0, 8, 8, LED_GREEN);
matrix.drawBitmap(0, 0, menu_2, 8, 8, LED_RED);
matrix.writeDisplay();
break;
case M_RESTART:
matrix.clear();
matrix.drawBitmap(0, 0, menu_0, 8, 8, LED_GREEN);
matrix.drawBitmap(0, 0, menu_3, 8, 8, LED_RED);
matrix.writeDisplay();
break;
}
playMusicStep();
}
void doDeath() {
Serial.println("Death!");
int color = LED_RED;
int score = 0;
if(gameTasterMode == 1) {
score = (int) ((millis() - timeGameStart) / 1000 * 5);
int highscore = 0;
EEPROM.get(0x10, highscore);
if (score > highscore) {
EEPROM.put(0x10, score);
color = LED_YELLOW;
}
} else {
score = (int) ((millis() - timeGameStart) / 1000 * 20);
int highscore = 0;
EEPROM.get(0x10, highscore);
if (score > highscore) {
EEPROM.put(0x10, score);
color = LED_YELLOW;
}
}
matrix.drawRect(3, 3, 2, 2, color);
matrix.writeDisplay();
delay(500);
matrix.drawRect(2, 2, 4, 4, color);
matrix.writeDisplay();
delay(500);
matrix.drawRect(1, 1, 6, 6, color);
matrix.writeDisplay();
delay(500);
matrix.drawRect(0, 0, 8, 8, color);
matrix.writeDisplay();
delay(500);
displayText("Score: ", 30);
displayText(String(score), 45);
currMode = MENU;
}
void loopGame() {
matrix.clear();
doSensorData(LEFT);
doSensorData(RIGHT);
int leftDistance = (int) getSonarData(LEFT);
int rightDistance = (int) getSonarData(RIGHT);
updateDistances(leftDistance, rightDistance);
updatePlayer();
updateMap();
displayMap();
matrix.writeDisplay();
if (buttonShortPressed(tasterPin, false)) currMode = BREAK;
if (buttonLongPressed(tasterPin)) currMode = MENU;
Serial.print("Pos: ");
Serial.print(bottomLeftBorderPosition);
Serial.print("|");
Serial.print(playerPosition);
Serial.print("|");
Serial.println(bottomRightBorderPosition);
if (bottomLeftBorderPosition >= playerPosition || playerPosition >= bottomRightBorderPosition)
doDeath();
}
void loopBreak() {
if (buttonShortPressed(tasterPin, false)) currMode = GAME;
if (buttonLongPressed(tasterPin)) {
if (gameTasterMode == 1) gameTasterMode = 0;
else gameTasterMode = 1;
EEPROM.put(0x20, gameTasterMode);
}
matrix.clear();
matrix.drawBitmap(0, 0, menu_pause, 8, 8, LED_YELLOW);
matrix.writeDisplay();
}
void setup() {
//Init
Serial.begin(230400);
//Tasterinitialisierung
pinMode(tasterPin, INPUT);
pinMode(goLeftPin, INPUT);
pinMode(goRightPin, INPUT);
digitalWrite(tasterPin, HIGH);
digitalWrite(goLeftPin, HIGH);
digitalWrite(goRightPin, HIGH);
pinMode(buzzerPin, OUTPUT);//buzzer
Serial.println("Our awesome Racinggame");
Serial.println("");
//Displayinitialisierung
matrix.begin(0x70);
startUp();
EEPROM.get(0x20, gameTasterMode);
int highscore = 0;
EEPROM.get(0x10, highscore);
Serial.print("Highscore: "); Serial.println(highscore);
}
void loop() {
switch (currMode) {
case MENU:
loopMenu();
break;
case GAME:
loopGame();
break;
case BREAK:
loopBreak();
break;
}
Serial.print("Mode: ");
Serial.println(gameTasterMode);
}