-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimonsays.ino
333 lines (251 loc) · 7.29 KB
/
simonsays.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
#define led5 5 //green button led
#define led6 6 //blue button led
#define led7 7 //yellow button led
#define led8 8 //red button led
#define button5 9 //green button
#define button6 10 //blue button
#define button7 11 //yellow button
#define button8 12 //red button
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define totalLevels 28
#define buzzer 3
#define endgame 660
#define BUFSIZE 10
#include <SPI.h>
#include <Wire.h>
#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define NOTE_C7 2093
#define NOTE_E7 2637
#define NOTE_G7 3136
#define NOTE_G6 1568
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int option = 2;
bool gameOver = 0;
int buttonPressed = 0;
int record = 0;
char buf[BUFSIZE];
int simonSaid[totalLevels];
int tones[4] = {950, 850, 750, 650};
int tspeed[4] = {500, 400, 300, 200};
int tsilence[4] = {200, 150, 100, 80};
//Mario main theme melody
int melody[16] = {NOTE_E7, NOTE_E7, 0, NOTE_E7, 0, NOTE_C7, NOTE_E7, 0, NOTE_G7, 0, 0, 0, NOTE_G6, 0, 0, 0};
//Mario main them tempo
int tempo[16] = { 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12};
void displayShow(String text, int tsize){
display.clearDisplay();
display.setTextSize(tsize);
display.setTextColor(WHITE);
display.setCursor(1, 0);
display.println(text);
display.display();
}
void displayShow(String text, String text2, int tsize){
display.clearDisplay();
display.setTextSize(tsize);
display.setTextColor(WHITE);
display.setCursor(1, 0);
display.println(text);
display.print(text2);
display.display();
}
void setup() {
Serial.begin(9600);
//Config
pinMode(led7, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led8, OUTPUT);
digitalWrite(led7, LOW);
digitalWrite(led6, LOW);
digitalWrite(led5, LOW);
digitalWrite(led8, LOW);
pinMode(button7, INPUT_PULLUP);
pinMode(button6, INPUT_PULLUP);
pinMode(button5, INPUT_PULLUP);
pinMode(button8, INPUT_PULLUP);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(("Falha no Display"));
for(;;);
}
Serial.println("display genius");
displayShow("GENIUS", 3);
delay(2000);
eeprom_read_string(0, buf, BUFSIZE);
String strrecord = String(buf);
Serial.println(strrecord);
displayShow(buf, 2);
record = (String(strrecord[8]) + String(strrecord[9])).toInt();
Serial.println(record);
delay(2000);
menuDificuldade();
}
void sort(){
randomSeed(analogRead(0));
for (int i = 0; i <= totalLevels; i++) {
simonSaid[i] = random(5, 9);
Serial.print(String(simonSaid[i]) + ", ");
}
}
void loop() {
sort();
gameOver = 0;
while (!gameOver) {
for (int i = 1; i < totalLevels; i++) {
displayShow("Nivel " + String(i), (i < 10? 3: 2));
delay(1400);
playLevel(i);
for (int g = 0; g < i; g++) {
buttonPressed = readBtn();
playBuzzer(buttonPressed, tspeed[option]);
//Manter 100 mseconds
switch(option){
case 2: delay(20); break;
case 3: delay(40); break;
}
if (!(buttonPressed == simonSaid[g])) {
int total = i-1;
displayShow("GAME OVER", "Total: " + String(total), 2);
playGameOver();
if (total >= 10){
displayShow("Brocou!", 3);
delay(2000);
}
if (total > record){
displayShow("Novo ", "Record! " + String(total), 2);
delay(2000);
String myString = "Record: " + String(total);
myString.toCharArray(buf, BUFSIZE);
eeprom_write_string(0, buf);
}
gameOver = 1;
break;
}
}
if (gameOver) break;
}
}
}
int readBtn(){
buttonPressed = 0;
while (!buttonPressed) {
if (!digitalRead(button8)){
buttonPressed = 8;
}
if (!digitalRead(button7)){
buttonPressed = 7;
}
if (!digitalRead(button6)){
buttonPressed = 6;
}
if (!digitalRead(button5)){
buttonPressed = 5;
}
}
return buttonPressed;
}
void playLevel(int i){
for (int g = 0; g < i; g++) {
Serial.println("");
Serial.println("said = " + String(simonSaid[g]));
Serial.println("option = " + String(option));
Serial.println("tspeed = " + String(tspeed[option]));
playBuzzer(simonSaid[g], tspeed[option]);
delay(tsilence[option]); // pausa entre uma luz e outra
}
}
void menuDificuldade(){
displayShow("Grau?", 3);
option = readBtn() - 5;
Serial.println("");
Serial.println("option = " + String(option));
switch(option){
case 0: displayShow("Facil", 3); Serial.println("Facil"); break;
case 1: displayShow("Normal", 3); Serial.println("Normal"); break;
case 2: displayShow("Dificil", 3); Serial.println("Dificil"); break;
case 3: displayShow("Insano", 3); Serial.println("Insano"); break;
}
playBuzzer(buttonPressed, 300);
delay(1700);
}
void playGameOver(){
tone(buzzer, endgame);
digitalWrite(led7, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led8, HIGH);
delay(250);
noTone(buzzer);
delay(50);
tone(buzzer, endgame);
delay(250);
noTone(buzzer);
delay(650);
digitalWrite(led7, LOW);
digitalWrite(led6, LOW);
digitalWrite(led5, LOW);
digitalWrite(led8, LOW);
delay(2000);
// softReset();
}
void playBuzzer(int button, int mseconds) {
Serial.println("button = " + String(button));
Serial.println("seconds= " + String(mseconds));
digitalWrite(button, HIGH);
tone(buzzer, tones[button - 5]);
delay(mseconds);
digitalWrite(button, LOW);
noTone(buzzer);
}
const int EEPROM_MIN_ADDR = 0;
const int EEPROM_MAX_ADDR = 511;
boolean eeprom_is_addr_ok(int addr) {
return ((addr >= EEPROM_MIN_ADDR) && (addr <= EEPROM_MAX_ADDR));
}
boolean eeprom_write_bytes(int startAddr, const byte* array, int numBytes) {
int i;
if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes)) {
return false;
}
for (i = 0; i < numBytes; i++) {
EEPROM.write(startAddr + i, array[i]);
}
return true;
}
boolean eeprom_write_string(int addr, const char* string) {
int numBytes;
numBytes = strlen(string) + 1;
return eeprom_write_bytes(addr, (const byte*)string, numBytes);
}
boolean eeprom_read_string(int addr, char* buffer, int bufSize) {
byte ch;
int bytesRead;
if (!eeprom_is_addr_ok(addr)) {
return false;
}
if (bufSize == 0) {
return false;
}
if (bufSize == 1) {
buffer[0] = 0;
return true;
}
bytesRead = 0;
ch = EEPROM.read(addr + bytesRead);
buffer[bytesRead] = ch;
bytesRead++;
while ( (ch != 0x00) && (bytesRead < bufSize) && ((addr + bytesRead) <= EEPROM_MAX_ADDR) ) {
ch = EEPROM.read(addr + bytesRead);
buffer[bytesRead] = ch;
bytesRead++;
}
if ((ch != 0x00) && (bytesRead >= 1)) {
buffer[bytesRead - 1] = 0;
}
return true;
}