-
Notifications
You must be signed in to change notification settings - Fork 0
/
Homework5.ino
1129 lines (1099 loc) · 31 KB
/
Homework5.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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "LedControl.h"
#include <LiquidCrystal.h>
#include <EEPROM.h>
#include "pitches.h"
const byte rs = 9;
const byte en = 8;
const byte d4 = 7;
const byte d5 = 6;
const byte d6 = 5;
const byte d7 = 4;
const byte buzzerPin = 13;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const byte dinPin = 12;
const byte clockPin = 11;
const byte loadPin = 10;
const byte matrixSize = 8;
LedControl lc = LedControl(dinPin, clockPin, loadPin, 1); //DIN, CLK, LOAD, No. DRIVER
byte matrixBrightness = 1;
byte aboutScrollText = 0;
byte HTPscrollText = 0;
byte upDownArrows[8] = {
B00100,
B01110,
B11111,
B00000,
B00000,
B11111,
B01110,
B00100
};
byte block[8] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
};
const byte matrixHello[matrixSize] = {
B10010001,
B10010001,
B10010101,
B10010001,
B11110101,
B10010101,
B10010100,
B10010101
};
const byte matrixMenu[5][8] = {
{ 0b00100000,
0b00110000,
0b00111000,
0b00111100,
0b00111100,
0b00111000,
0b00110000,
0b00100000 },
{ 0b01111100,
0b00111000,
0b00010000,
0b01111100,
0b01010100,
0b00111000,
0b00010000,
0b00111000 },
{ 0b00010000,
0b00111000,
0b01110000,
0b11110000,
0b01001000,
0b00000100,
0b00000010,
0b00000001 },
{ 0b00000000,
0b00011000,
0b00100100,
0b00000100,
0b00001000,
0b00001000,
0b00000000,
0b00001000 },
{ 0b00000000,
0b00001000,
0b00000000,
0b00011000,
0b00001000,
0b00001000,
0b00001000,
0b00011100 }
};
const byte matrixHappy[8] = {
0b00100100,
0b00100100,
0b00100100,
0b00100100,
0b00000000,
0b10000001,
0b01000010,
0b00111100
};
byte matrix[matrixSize][matrixSize] = {
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 }
};
const String menuOptions[5] = {
"Start game",
"Leaderboard",
"Settings",
"How to play",
"About"
};
const String settingsOptions[6] = {
"Player name",
"LCD bright.",
"Matrix bright.",
"Difficulty",
"Sounds",
"Reset HS"
};
String name = "AAA";
char nameLetters[3] = {
'A', 'A', 'A'
};
byte namePos = 0;
byte menuCurrentItem = 0;
const byte pinSW = 2;
const byte pinX = A0;
const byte pinY = A1;
bool swState = LOW;
bool lastSwState = LOW;
byte state = 0;
byte switchState = HIGH;
int xValue = 0;
byte subMenuOption = 0;
int yValue = 0;
bool joyBackToMiddleX = LOW;
bool joyBackToMiddleY = LOW;
int minThreshold = 400;
int maxThreshold = 600;
unsigned long lastDebounceTime = 0;
unsigned long prevScrollTime = 0;
unsigned int debounceDelay = 50;
bool matrixChanged = true;
unsigned int score = 0;
byte xPos = 0;
byte yPos = 0;
byte xLastPos = 0;
byte yLastPos = 0;
byte lastFoodRow = 0;
byte lastFoodColumn = 0;
int scrollTextPosition = 0;
byte currentFoodRow = 0;
byte leaderboard = 0;
byte leaderboardPos = 0;
byte settings = 0;
byte beatHighscore = 0;
byte settingsPos = 0;
byte currentFoodColumn = 0;
byte startGame = 0;
String messageAbout = "Made by Vlad Toader. Github user: @vladfxstoader";
String messageHTP = "Eat the blinking food to increase the score. When reaching 10, the game is over.";
const byte moveInterval = 100;
int LCDbrightness;
byte difficulty = 1;
byte sounds;
unsigned long long lastBlink = 0;
byte blinkLetter = LOW;
const int blinkInterval = 250;
unsigned long long lastMoved = 0;
int highscores[5];
String highscoreNames[5] = { "", "", "", "", "" };
unsigned long lastLetterBlink = 0;
/*
in EEPROM, the memory will be put like this:
1 - LCD brightness level (0-5, multiplied by 51 and write it to pin)
2 - Matrix brightness
3 - Difficulty
4 - Sounds on/off
5 - First highscore
6 -> 9 - First highscore name
10 - Second highscore
11 -> 14 - Second highscore name
15 - Third highscore
16 -> 19 - Third highscore name
20 - Fourth highscore
21 -> 24 - Fourth highscore name
25 - Fifth highscore
26 -> 29 - Fifth highscore name
*/
// function that gets the settings values from EEPROM
void getSettingsFromEEPROM() {
LCDbrightness = EEPROM.read(1);
matrixBrightness = EEPROM.read(2);
difficulty = EEPROM.read(3);
sounds = EEPROM.read(4);
}
// function used for initialising the leaderboard with 0
void putInitialHStoEEPROM() {
for (int i = 1; i <= 5; ++i) {
EEPROM.update(i * 5, 0);
EEPROM.update(i * 5 + 1, 'N');
EEPROM.update(i * 5 + 2, 'a');
EEPROM.update(i * 5 + 3, 'N');
}
}
// function that updates the highscores at the end of the game
void updateHStoEEPROM(int newScore, String newName) {
if (newScore > highscores[0]) {
beatHighscore = 1;
EEPROM.update(25, EEPROM.read(20));
EEPROM.update(26, EEPROM.read(21));
EEPROM.update(27, EEPROM.read(22));
EEPROM.update(28, EEPROM.read(23));
EEPROM.update(20, EEPROM.read(15));
EEPROM.update(21, EEPROM.read(16));
EEPROM.update(22, EEPROM.read(17));
EEPROM.update(23, EEPROM.read(18));
EEPROM.update(15, EEPROM.read(10));
EEPROM.update(16, EEPROM.read(11));
EEPROM.update(17, EEPROM.read(12));
EEPROM.update(18, EEPROM.read(13));
EEPROM.update(10, EEPROM.read(5));
EEPROM.update(11, EEPROM.read(6));
EEPROM.update(12, EEPROM.read(7));
EEPROM.update(13, EEPROM.read(8));
EEPROM.update(5, newScore);
EEPROM.update(6, newName[0]);
EEPROM.update(7, newName[1]);
EEPROM.update(8, newName[2]);
}
else if (newScore > highscores[1]) {
beatHighscore = 2;
EEPROM.update(25, EEPROM.read(20));
EEPROM.update(26, EEPROM.read(21));
EEPROM.update(27, EEPROM.read(22));
EEPROM.update(28, EEPROM.read(23));
EEPROM.update(20, EEPROM.read(15));
EEPROM.update(21, EEPROM.read(16));
EEPROM.update(22, EEPROM.read(17));
EEPROM.update(23, EEPROM.read(18));
EEPROM.update(15, EEPROM.read(10));
EEPROM.update(16, EEPROM.read(11));
EEPROM.update(17, EEPROM.read(12));
EEPROM.update(18, EEPROM.read(13));
EEPROM.update(10, newScore);
EEPROM.update(11, newName[0]);
EEPROM.update(12, newName[1]);
EEPROM.update(13, newName[2]);
}
else if (newScore > highscores[2]) {
beatHighscore = 3;
EEPROM.update(25, EEPROM.read(20));
EEPROM.update(26, EEPROM.read(21));
EEPROM.update(27, EEPROM.read(22));
EEPROM.update(28, EEPROM.read(23));
EEPROM.update(20, EEPROM.read(15));
EEPROM.update(21, EEPROM.read(16));
EEPROM.update(22, EEPROM.read(17));
EEPROM.update(23, EEPROM.read(18));
EEPROM.update(15, newScore);
EEPROM.update(16, newName[0]);
EEPROM.update(17, newName[1]);
EEPROM.update(18, newName[2]);
}
else if (newScore > highscores[3]) {
beatHighscore = 4;
EEPROM.update(25, EEPROM.read(20));
EEPROM.update(26, EEPROM.read(21));
EEPROM.update(27, EEPROM.read(22));
EEPROM.update(28, EEPROM.read(23));
EEPROM.update(20, newScore);
EEPROM.update(21, newName[0]);
EEPROM.update(22, newName[1]);
EEPROM.update(23, newName[2]);
}
else if (newScore > highscores[4]) {
beatHighscore = 5;
EEPROM.update(25, newScore);
EEPROM.update(26, newName[0]);
EEPROM.update(27, newName[1]);
EEPROM.update(28, newName[2]);
}
}
// function that retrieves the highscores from EEPROM
void getHighscores() {
for (int i = 1; i <= 5; ++i) {
highscores[i - 1] = EEPROM.read(i * 5);
int firstLetter = EEPROM.read(i * 5 + 1);
int secondLetter = EEPROM.read(i * 5 + 2);
int thirdLetter = EEPROM.read(i * 5 + 3);
String name = "";
name += char(firstLetter);
name += char(secondLetter);
name += char(thirdLetter);
highscoreNames[i - 1] = name;
Serial.print(highscores[i - 1]);
Serial.print(" - ");
Serial.println(highscoreNames[i - 1]);
}
}
// function that plays a song used for the welcome screen
void song() {
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(buzzerPin, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(8);
}
}
// function used to blink the current letter when the player
// chooses their name
void blinkLetterName() {
if (state == 3 && settings == 1 && settingsPos == 0) {
if (millis() - lastLetterBlink > 400) {
lastLetterBlink = millis();
blinkLetter = !blinkLetter;
}
lcd.setCursor(namePos + 4, 1);
if (blinkLetter == HIGH) {
lcd.print(nameLetters[namePos]);
}
else {
lcd.print(" ");
}
}
}
// function used for different uses of the button of the joystick
void buttonLogic() {
if (swState != lastSwState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (swState != switchState) {
switchState = swState;
if (switchState == LOW) {
if (state == 0 && startGame == 0 || state == 4) { // past the welcome screen
state = 1;
lcd.clear();
lcd.print("<MENU>");
lcd.setCursor(15, 0);
lcd.write(byte(0));
lcd.setCursor(0, 1);
lcd.print(">");
lcd.print(menuOptions[menuCurrentItem]);
lc.clearDisplay(0);
for (int row = 0; row < matrixSize; row++) {
lc.setRow(0, row, matrixMenu[menuCurrentItem][row]);
}
}
else if (subMenuOption == 1) { // when the game starts
startGame = 1;
lcd.clear();
lcd.print("PLAYER: ");
lcd.print(nameLetters[0]);
lcd.print(nameLetters[1]);
lcd.print(nameLetters[2]);
lcd.setCursor(0, 1);
lcd.print("SCORE: ");
lcd.print(score);
lc.clearDisplay(0); // clear screen
matrix[xPos][yPos] = 1;
generateFood();
}
else if (state == 3 && startGame == 0 && settings == 1 && settingsPos == 0) { // after the player saves their name
state = 2;
lcd.clear();
lcd.print("<SETTINGS>");
lcd.setCursor(15, 0);
lcd.write(byte(0));
lcd.setCursor(0, 1);
lcd.print(">");
lcd.print(settingsOptions[settingsPos]);
lc.clearDisplay(0);
for (int row = 0; row < matrixSize; row++) {
lc.setRow(0, row, matrixMenu[menuCurrentItem][row]);
}
}
else if (state == 3 && startGame == 0 && settings == 1 && settingsPos == 5) { // after the player saves their name
putInitialHStoEEPROM();
getHighscores();
state = 2;
lcd.clear();
lcd.print("<SETTINGS>");
lcd.setCursor(15, 0);
lcd.write(byte(0));
lcd.setCursor(0, 1);
lcd.print(">");
lcd.print(settingsOptions[settingsPos]);
lc.clearDisplay(0);
for (int row = 0; row < matrixSize; row++) {
lc.setRow(0, row, matrixMenu[menuCurrentItem][row]);
}
}
}
}
}
lastSwState = swState;
}
// function used for different uses of the y axis of the joystick
void yAxisLogic() {
if (yValue > maxThreshold && joyBackToMiddleY == LOW && state == 1 && startGame == 0) { // selecting an option from the menu
state = 2;
if (menuCurrentItem == 0) {
subMenuOption = 1;
lcd.clear();
lcd.print("<START GAME>");
lcd.setCursor(0, 1);
lcd.print("Press to start");
}
else if (menuCurrentItem == 1) {
lcd.clear();
lcd.print("<LEADERBOARD>");
lcd.setCursor(15, 0);
lcd.write(byte(0));
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(leaderboardPos + 1);
lcd.print(".");
lcd.print(highscoreNames[leaderboardPos]);
lcd.print(" - ");
lcd.print(highscores[leaderboardPos]);
lcd.print(" pct");
leaderboard = 1;
}
else if (menuCurrentItem == 2) {
lcd.clear();
lcd.print("<SETTINGS>");
lcd.setCursor(15, 0);
lcd.write(byte(0));
settings = 1;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(">");
lcd.print(settingsOptions[settingsPos]);
}
else if (menuCurrentItem == 3) {
lcd.clear();
lcd.print("<HOW TO PLAY>");
lcd.setCursor(0, 1);
HTPscrollText = 1;
}
else if (menuCurrentItem == 4) {
lcd.clear();
lcd.print("<ABOUT> SNAKE");
lcd.setCursor(0, 1);
aboutScrollText = 1;
}
joyBackToMiddleY = HIGH;
}
else if (yValue < minThreshold && joyBackToMiddleY == LOW && state == 2 && startGame == 0) { // moving back to the main menu
state = 1;
aboutScrollText = 0;
HTPscrollText = 0;
scrollTextPosition = 0;
leaderboardPos = 0;
subMenuOption = 0;
leaderboard = 0;
settingsPos = 0;
settings = 0;
lcd.clear();
lcd.print("<MENU>");
lcd.setCursor(15, 0);
lcd.write(byte(0));
lcd.setCursor(0, 1);
lcd.print(">");
lcd.print(menuOptions[menuCurrentItem]);
lc.clearDisplay(0);
for (int row = 0; row < matrixSize; row++) {
lc.setRow(0, row, matrixMenu[menuCurrentItem][row]);
}
joyBackToMiddleY = HIGH;
}
else if (yValue > maxThreshold && joyBackToMiddleY == LOW && state == 2 && startGame == 0 && settings == 1) { // choosing an option from the settings submenu
state = 3;
lcd.clear();
if (settingsPos == 0) {
lcd.print("<NAME> Press to");
lcd.setCursor(0, 1);
lcd.write(byte(0));
lcd.print(" ");
lcd.print(nameLetters[0]);
lcd.print(nameLetters[1]);
lcd.print(nameLetters[2]);
lcd.print(" SAVE");
}
else if (settingsPos == 1) {
lcd.print("<LCD BRIGHT.>");
lcd.setCursor(0, 1);
lcd.write(byte(0));
lcd.setCursor(4, 1);
lcd.print("-");
for (int i = 0; i < LCDbrightness; ++i)
lcd.write(byte(1));
lcd.setCursor(10, 1);
lcd.print("+");
}
else if (settingsPos == 2) {
for (int row = 0; row < matrixSize; row++) {
for (int col = 0; col < matrixSize; col++) {
lc.setLed(0, row, col, true);
}
}
lcd.print("<MATRIX BRIGHT.>");
lcd.setCursor(0, 1);
lcd.write(byte(0));
lcd.setCursor(4, 1);
lcd.print("-");
for (int i = 0; i < matrixBrightness; ++i)
lcd.write(byte(1));
lcd.setCursor(10, 1);
lcd.print("+");
}
else if (settingsPos == 3) {
lcd.print("<DIFFICULTY>");
lcd.setCursor(0, 1);
lcd.write(byte(0));
lcd.print(" ");
if (difficulty == 1) {
lcd.print("EASY");
}
else if (difficulty == 2) {
lcd.print("MEDIUM");
}
else if (difficulty == 3) {
lcd.print("HARD");
}
}
else if (settingsPos == 4) {
lcd.print("<SOUNDS>");
lcd.setCursor(0, 1);
lcd.write(byte(0));
lcd.print(" ");
if (sounds == 1)
lcd.print("ON");
else
lcd.print("OFF");
}
else if (settingsPos == 5) {
lcd.print("<RESET HS>");
lcd.setCursor(0, 1);
lcd.print("Press to reset");
}
joyBackToMiddleY = HIGH;
}
else if (yValue < minThreshold && joyBackToMiddleY == LOW && state == 3 && startGame == 0 && settings == 1 and settingsPos != 0) { // going back to the settings submenu
state = 2;
lcd.clear();
lcd.print("<SETTINGS>");
lcd.setCursor(15, 0);
lcd.write(byte(0));
lcd.setCursor(0, 1);
lcd.print(">");
lcd.print(settingsOptions[settingsPos]);
lc.clearDisplay(0);
for (int row = 0; row < matrixSize; row++) {
lc.setRow(0, row, matrixMenu[menuCurrentItem][row]);
}
joyBackToMiddleY = HIGH;
}
else if (yValue < minThreshold && joyBackToMiddleY == LOW && state == 3 && startGame == 0 && settings == 1 and settingsPos == 0) { // moving left through the name field
if (namePos > 0) {
lcd.setCursor(namePos + 4, 1);
lcd.print(nameLetters[namePos]);
namePos--;
}
joyBackToMiddleY = HIGH;
}
else if (yValue > maxThreshold && joyBackToMiddleY == LOW && state == 3 && startGame == 0 && settings == 1 and settingsPos == 0) { // moving right through the name field
if (namePos < 2) {
lcd.setCursor(namePos + 4, 1);
lcd.print(nameLetters[namePos]);
namePos++;
}
joyBackToMiddleY = HIGH;
}
else if (joyBackToMiddleY == HIGH && yValue < maxThreshold && yValue > minThreshold && startGame == 0) {
joyBackToMiddleY = LOW;
}
}
// function used for different uses of the x axis of the joystick
void xAxisLogic() {
if (xValue < minThreshold && joyBackToMiddleX == LOW && state == 1 && startGame == 0 && leaderboard == 0) { // moving down through the menu
if (menuCurrentItem < 4) {
menuCurrentItem++;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(">");
lcd.print(menuOptions[menuCurrentItem]);
lc.clearDisplay(0);
for (int row = 0; row < matrixSize; row++) {
lc.setRow(0, row, matrixMenu[menuCurrentItem][row]);
}
}
else if (menuCurrentItem == 4) {
menuCurrentItem = 0;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(">");
lcd.print(menuOptions[menuCurrentItem]);
lc.clearDisplay(0);
for (int row = 0; row < matrixSize; row++) {
lc.setRow(0, row, matrixMenu[menuCurrentItem][row]);
}
}
joyBackToMiddleX = HIGH;
}
else if (xValue > maxThreshold && joyBackToMiddleX == LOW && state == 1 && startGame == 0 && leaderboard == 0) { // moving up through the menu
if (menuCurrentItem > 0) {
menuCurrentItem--;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(">");
lcd.print(menuOptions[menuCurrentItem]);
lc.clearDisplay(0);
for (int row = 0; row < matrixSize; row++) {
lc.setRow(0, row, matrixMenu[menuCurrentItem][row]);
}
}
else if (menuCurrentItem == 0) {
menuCurrentItem = 4;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(">");
lcd.print(menuOptions[menuCurrentItem]);
lc.clearDisplay(0);
for (int row = 0; row < matrixSize; row++) {
lc.setRow(0, row, matrixMenu[menuCurrentItem][row]);
}
}
joyBackToMiddleX = HIGH;
}
else if (xValue < minThreshold && joyBackToMiddleX == LOW && state == 2 && startGame == 0 && leaderboard == 1) { // moving down through the leaderboard
if (leaderboardPos < 4) {
leaderboardPos++;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(leaderboardPos + 1);
lcd.print(".");
lcd.print(highscoreNames[leaderboardPos]);
lcd.print(" - ");
lcd.print(highscores[leaderboardPos]);
lcd.print(" pct");
}
joyBackToMiddleX = HIGH;
}
else if (xValue > maxThreshold && joyBackToMiddleX == LOW && state == 2 && startGame == 0 && leaderboard == 1) { // moving up through the leaderboard
if (leaderboardPos > 0) {
leaderboardPos--;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(leaderboardPos + 1);
lcd.print(".");
lcd.print(highscoreNames[leaderboardPos]);
lcd.print(" - ");
lcd.print(highscores[leaderboardPos]);
lcd.print(" pct");
}
joyBackToMiddleX = HIGH;
}
else if (xValue < minThreshold && joyBackToMiddleX == LOW && state == 2 && startGame == 0 && settings == 1) { // moving down through the settings submenu
if (settingsPos < 5) {
settingsPos++;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(">");
lcd.print(settingsOptions[settingsPos]);
}
else if (settingsPos == 5) {
settingsPos = 0;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(">");
lcd.print(settingsOptions[settingsPos]);
}
joyBackToMiddleX = HIGH;
}
else if (xValue > maxThreshold && joyBackToMiddleX == LOW && state == 2 && startGame == 0 && settings == 1) { // moving up through the settings submenu
if (settingsPos > 0) {
settingsPos--;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(">");
lcd.print(settingsOptions[settingsPos]);
}
else if (settingsPos == 0) {
settingsPos = 5;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(">");
lcd.print(settingsOptions[settingsPos]);
}
joyBackToMiddleX = HIGH;
}
else if (xValue < minThreshold && joyBackToMiddleX == LOW && state == 3 && startGame == 0 && settings == 1) { // different values when in the settings submenu
if (settingsPos == 0) { // scrolling through letters while in the input of the name
if (nameLetters[namePos] > 'A') {
nameLetters[namePos]--;
lcd.setCursor(namePos + 4, 1);
lcd.print(nameLetters[namePos]);
}
else if (nameLetters[namePos] == 'A') {
nameLetters[namePos] = 'Z';
lcd.setCursor(namePos + 4, 1);
lcd.print(nameLetters[namePos]);
}
}
else if (settingsPos == 1) { // changing the brightness
if (LCDbrightness > 1) {
LCDbrightness--;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.write(byte(0));
lcd.setCursor(4, 1);
lcd.print("-");
for (int i = 0; i < LCDbrightness; ++i)
lcd.write(byte(1));
lcd.setCursor(10, 1);
lcd.print("+");
analogWrite(3, LCDbrightness * 51);
EEPROM.update(1, LCDbrightness);
}
}
else if (settingsPos == 2) { // changing the brightness
if (matrixBrightness > 1) {
matrixBrightness--;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.write(byte(0));
lcd.setCursor(4, 1);
lcd.print("-");
for (int i = 0; i < matrixBrightness; ++i)
lcd.write(byte(1));
lcd.setCursor(10, 1);
lcd.print("+");
lc.setIntensity(0, 3 * matrixBrightness);
EEPROM.update(2, matrixBrightness);
}
}
else if (settingsPos == 3) { // changing the difficulty
if (difficulty > 1) {
difficulty--;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.write(byte(0));
lcd.print(" ");
if (difficulty == 1) {
lcd.print("EASY");
}
else if (difficulty == 2) {
lcd.print("MEDIUM");
}
else if (difficulty == 3) {
lcd.print("HARD");
}
EEPROM.update(3, difficulty);
}
}
else if (settingsPos == 4) { // changing the sounds
if (sounds == 1) {
sounds = 0;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.write(byte(0));
lcd.print(" ");
lcd.print("OFF");
EEPROM.update(4, sounds);
}
}
joyBackToMiddleX = HIGH;
}
else if (xValue > maxThreshold && joyBackToMiddleX == LOW && state == 3 && startGame == 0 && settings == 1) { // different values when in the settings submenu
if (settingsPos == 0) {
if (nameLetters[namePos] < 'Z') {
nameLetters[namePos]++;
lcd.setCursor(namePos + 4, 1);
lcd.print(nameLetters[namePos]);
}
else if (nameLetters[namePos] == 'Z') {
nameLetters[namePos] = 'A';
lcd.setCursor(namePos + 4, 1);
lcd.print(nameLetters[namePos]);
}
}
else if (settingsPos == 1) {
if (LCDbrightness < 5) {
LCDbrightness++;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.write(byte(0));
lcd.setCursor(4, 1);
lcd.print("-");
for (int i = 0; i < LCDbrightness; ++i)
lcd.write(byte(1));
lcd.setCursor(10, 1);
lcd.print("+");
analogWrite(3, LCDbrightness * 51);
EEPROM.update(1, LCDbrightness);
}
}
else if (settingsPos == 2) {
if (matrixBrightness < 5) {
matrixBrightness++;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.write(byte(0));
lcd.setCursor(4, 1);
lcd.print("-");
for (int i = 0; i < matrixBrightness; ++i)
lcd.write(byte(1));
lcd.setCursor(10, 1);
lcd.print("+");
lc.setIntensity(0, 3 * matrixBrightness);
EEPROM.update(2, matrixBrightness);
}
}
else if (settingsPos == 3) {
if (difficulty < 3) {
difficulty++;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.write(byte(0));
lcd.print(" ");
if (difficulty == 1) {
lcd.print("EASY");
}
else if (difficulty == 2) {
lcd.print("MEDIUM");
}
else if (difficulty == 3) {
lcd.print("HARD");
}
EEPROM.update(3, difficulty);
}
}
else if (settingsPos == 4) {
if (sounds == 0) {
sounds = 1;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.write(byte(0));
lcd.print(" ");
lcd.print("ON");
EEPROM.update(4, sounds);
}
}
joyBackToMiddleX = HIGH;
}
else if (joyBackToMiddleX == HIGH && xValue < maxThreshold && xValue > minThreshold && startGame == 0) {
joyBackToMiddleX = LOW;
}
}
void setup() {
putInitialHStoEEPROM();
LCDbrightness = EEPROM.read(1);
matrixBrightness = EEPROM.read(2);
difficulty = EEPROM.read(3);
sounds = EEPROM.read(4);
//song();
analogWrite(3, LCDbrightness * 51);
pinMode(pinSW, INPUT_PULLUP);
Serial.begin(9600);
lc.shutdown(0, false);
lc.setIntensity(0, 3 * matrixBrightness);
lc.clearDisplay(0);
for (int row = 0; row < matrixSize; row++) {
lc.setRow(0, row, matrixHello[row]);
}
randomSeed(analogRead(A2));
lcd.begin(16, 2);
lcd.print(" SNAKE!");
lcd.setCursor(0, 1);
lcd.print(" Press to begin");
getHighscores();
lcd.createChar(0, upDownArrows);
lcd.createChar(1, block);
}
void loop() {
if (startGame == 1) {
game();
if (score == 10) {
exitGame();
}
}
else {
blinkLetterName();
if (aboutScrollText == 1 && scrollTextPosition < messageAbout.length() - 14) {
lcd.setCursor(0, 1);
lcd.print(messageAbout.substring(scrollTextPosition-1, 16 + scrollTextPosition));
if (millis() - prevScrollTime >= 500) {
prevScrollTime = millis();
lcd.setCursor(0, 1);
lcd.print(messageAbout.substring(scrollTextPosition, 16 + scrollTextPosition));
scrollTextPosition++;
}
}
if (HTPscrollText == 1 && scrollTextPosition < messageAbout.length() - 14) {
lcd.setCursor(0, 0);
lcd.print("<HOW TO PLAY> ");
lcd.setCursor(0, 1);
lcd.print(messageHTP.substring(scrollTextPosition-1, 16 + scrollTextPosition));
if (millis() - prevScrollTime >= 500) {
prevScrollTime = millis();
lcd.setCursor(0, 1);
lcd.print(messageHTP.substring(scrollTextPosition, 16 + scrollTextPosition));
scrollTextPosition++;
}
}
swState = digitalRead(pinSW);
xValue = analogRead(pinX);
yValue = analogRead(pinY);
buttonLogic();
yAxisLogic();
xAxisLogic();
}
}
// function used for printing the scrolling text
void showLetters(int printStart, int startLetter, char* message) {
lcd.setCursor(printStart, 1);
for (int letter = startLetter; letter <= startLetter + 15; letter++) {
lcd.print(message[letter]);
}
lcd.print(" ");
}
// function used for the game logic
void game() {
if (millis() - lastBlink > blinkInterval) {
matrix[currentFoodRow][currentFoodColumn] = !matrix[currentFoodRow][currentFoodColumn];
updateMatrix();
lastBlink = millis();
}
if (millis() - lastMoved > moveInterval) {
updatePositions();
lastMoved = millis();
}
if (matrixChanged == true) {
updateMatrix();
matrixChanged = false;
}
}
// function that generates new food (blinking point on the matrix)
void generateFood() {