-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemoryGame.cpp
More file actions
175 lines (152 loc) · 3.87 KB
/
memoryGame.cpp
File metadata and controls
175 lines (152 loc) · 3.87 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
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Adjust the LCD address if needed
LiquidCrystal_I2C lcd(0x27, 16, 2);
int buttons[4] = {12, 13, 14, 15}; // Update with suitable GPIO pins for ESP32
int leds[4] = {25, 26, 18, 19}; // Update with suitable GPIO pins for ESP32
boolean button[4] = {0, 0, 0, 0};
#define buzzer 23// Update with a suitable GPIO pin for ESP32
#define levelsInGame 50
int bt_simonSaid[100];
int led_simonSaid[100];
boolean lost;
int game_play, level, stage;
void setup() {
Serial.begin(9600);
// Initialize button and LED pins
for (int i = 0; i <= 3; i++) {
pinMode(buttons[i], INPUT_PULLUP);
pinMode(leds[i], OUTPUT);
}
pinMode(buzzer, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" Welcome To ");
lcd.setCursor(0, 1);
lcd.print("> Memory Game <");
delay(1000);
lcd.clear();
randomSeed(analogRead(0)); // Seed the random number generator
}
void loop() {
switch (stage) {
case 0:
lcd.setCursor(0, 0); lcd.print("Press Red Button");
lcd.setCursor(0, 1); lcd.print(" for Start Game ");
button[0] = digitalRead(buttons[0]);
while (button[0] == HIGH) {
button[0] = digitalRead(buttons[0]);
}
playBuzzer(25);
level = 1;
stage = 1;
game_play = 1;
break;
case 1:
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("Level: ");
lcd.print((level / 10) % 10);
lcd.print(level % 10);
lcd.setCursor(0, 1);
lcd.print(" -- Memorize -- ");
delay(1500);
led_simonSaid[level] = leds[random(0, 4)];
for (int i = 1; i <= level; i++) {
digitalWrite(led_simonSaid[i], HIGH);
playBuzzer(led_simonSaid[i] -15);
digitalWrite(led_simonSaid[i], LOW);
delay(400);
}
delay(500);
stage = 2;
break;
case 2:
stage = 3;
lcd.setCursor(0, 1);
lcd.print(" -- Play -- ");
break;
case 3:
for (int i = 0; i <= 3; i++) {
button[i] = digitalRead(buttons[i]);
if (button[i] == LOW) {
bt_simonSaid[game_play] = leds[i];
digitalWrite(leds[i], HIGH);
playBuzzer(i + 1);
while (button[i] == LOW) {
button[i] = digitalRead(buttons[i]);
}
delay(50);
digitalWrite(leds[i], LOW);
game_play++;
if (game_play - 1 == level) {
game_play = 1;
stage = 4;
break;
}
}
}
delay(10);
break;
case 4:
lcd.setCursor(0, 1);
lcd.print(" Verification ");
delay(1000);
for (int i = 1; i <= level; i++) {
if (led_simonSaid[i] != bt_simonSaid[i]) {
lost = 1;
break;
}
}
if (lost == 1) stage = 5;
else stage = 6;
break;
case 5:
lcd.setCursor(0, 0);
lcd.print(" !! You Lost !! ");
tone(buzzer, 350);
for (int i = 0; i <= 3; i++) {
digitalWrite(leds[i], HIGH);
}
delay(1000);
lcd.setCursor(0, 1);
lcd.print(" Score : ");
lcd.print(((level-1) / 10) % 10);
lcd.print((level-1) % 10);
lcd.print(" ");
noTone(buzzer);
delay(3000);
for (int i = 0; i <= 3; i++) {
digitalWrite(leds[i], LOW);
}
level = 1;
stage = 0;
lost = 0;
break;
case 6:
lcd.setCursor(0, 1);
lcd.print(" ** You Win ** ");
delay(1000);
if (level == levelsInGame) {
lcd.setCursor(0, 0);
lcd.print("Congratulation");
lcd.setCursor(0, 1);
lcd.print(" Level Complete");
delay(1000);
lcd.clear();
level = 1;
} else {
if (level < levelsInGame) level++;
}
stage = 1;
break;
default:
break;
}
}
void playBuzzer(int x) {
tone(buzzer, 650 + (x * 100));
delay(300);
noTone(buzzer);
}