-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimon-says.ino
342 lines (270 loc) · 7.48 KB
/
simon-says.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
#include <EEPROM.h> //Voor highscore :)
//Very simple simon says game.
//Made by Nawid. GitHub: https://github.com/Druganov1
//
// _____ _
// / ___(_)
// \ `--. _ _ __ ___ ___ _ __ ___ __ _ _ _ ___
// `--. \ | '_ ` _ \ / _ \| '_ \ / __|/ _` | | | / __|
// /\__/ / | | | | | | (_) | | | | \__ \ (_| | |_| \__ \
// \____/|_|_| |_| |_|\___/|_| |_| |___/\__,_|\__, |___/
// __/ |
// |___/
//
#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;
uint8_t EEPROMaddress = 130;
const int MAX_LEVEL = 100; // Hoogste level
int sequence[MAX_LEVEL]; //Dit is de juiste volgorde dus
int your_sequence[MAX_LEVEL]; //Reeks van de user die later word ingevuld bij het drukken van de knoppen
int level = 1; //Level van de speler
int score = 0; //Score
int scoreEEPROM = EEPROM.read(EEPROMaddress); //Score van rom
bool gameOver = false;
int velocity = 1000; //Snelheid voor delays
const int buzzer = 11; //buzzer pin die in het midden zit nice eve!
const int yellow_btn = 10; //Yellow led button
const int green_btn = 8; //Green led button
const int red_btn = 7; //Red led button
const int blue_btn = 9; //Blue led button
const int yellow_led = 6; //yellow led
const int green_led = 2; //green led
const int red_led = 3; // red led
const int blue_led = 5; // blue led
bool gameStarted = false;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setRGB(0, 255, 0);
DisplayScore();
pinMode(yellow_btn, INPUT); //yellow
pinMode(green_btn, INPUT); //Green
pinMode(red_btn, INPUT); // red
pinMode(blue_btn, INPUT); // blue
pinMode(yellow_led, OUTPUT);
pinMode(green_led, OUTPUT);
pinMode(red_led, OUTPUT);
pinMode(blue_led, OUTPUT);
digitalWrite(yellow_led, LOW); //omdat arduino soms beetje kut is doen we gewoon alles ff uit voor de leuk
digitalWrite(green_led, LOW);
digitalWrite(red_led, LOW);
digitalWrite(blue_led, LOW);
}
void blinkLampjesLoop()
{
if(!gameStarted){
int kaas[4] = {6, 2, 3, 5};
for(int i=0; i<4; i++){
digitalWrite(kaas[i], HIGH);
delay(100);
digitalWrite(kaas[i], LOW);
delay(200);
}
}
}
void loop() // loop loop loop looooooooooooooop
{
// while(!gameOver){
if (level == 1)
generate_sequence();//Maak een nieuwe random sequence (reeks) aan;
//while(!gameStarted){
// int kaas[4] = {6, 2, 3, 5};
//
// for(int i=0; i<4; i++){
// digitalWrite(kaas[i], HIGH);
// delay(200);
// digitalWrite(kaas[i], LOW);
// delay(200);
// }
//}
blinkLampjesLoop();
if (digitalRead(blue_btn) == HIGH || digitalRead(green_btn) == HIGH || digitalRead(red_btn) == HIGH || digitalRead(yellow_btn) == HIGH || level != 1) //Op blauwe knop klikken = spel starten. dont ask why heb het gewoon zo gedaan
{
gameStarted = true;
Serial.println("Spel is gestart!");
show_sequence(); //Laat de lampjes zien, die in de array al dus staan (Eerder aangeroepen door generate_sequence())
get_sequence(); //Wacht voor de input van de user
}
}
void show_sequence()
{
digitalWrite(yellow_led, LOW);
digitalWrite(green_led, LOW);
digitalWrite(red_led, LOW);
digitalWrite(blue_led, LOW);
for (int i = 0; i < level; i++)
{
digitalWrite(sequence[i], HIGH);
playBuzzer(sequence[i]);
delay(200);
digitalWrite(sequence[i], LOW);
delay(velocity);
}
}
void get_sequence()
{
int flag = 0; //Checkt of de ingevoerde knopjes matchen
for (int i = 0; i < level; i++)
{
flag = 0;
while(flag == 0)
{
if (digitalRead(yellow_btn) == HIGH)
{
digitalWrite(yellow_led, HIGH);
playBuzzer(6);
your_sequence[i] = yellow_led;
flag = 1;
delay(500);
if (your_sequence[i] != sequence[i])
{
wrong_sequence();
return;
}
digitalWrite(yellow_led, LOW);
}
if (digitalRead(green_btn) == HIGH)
{
Serial.println("Groen werkt toch wel??!");
digitalWrite(green_led, HIGH);
playBuzzer(2);
your_sequence[i] = green_led;
flag = 1;
delay(500);
if (your_sequence[i] != sequence[i])
{
wrong_sequence();
return;
}
digitalWrite(green_led, LOW);
}
if (digitalRead(red_btn) == HIGH)
{
digitalWrite(red_led, HIGH);
playBuzzer(3);
your_sequence[i] = red_led;
flag = 1;
delay(500);
if (your_sequence[i] != sequence[i])
{
wrong_sequence();
return;
}
digitalWrite(red_led, LOW);
}
if (digitalRead(blue_btn) == HIGH)
{
digitalWrite(blue_led, HIGH);
playBuzzer(5);
your_sequence[i] = blue_led;
flag = 1;
delay(500);
if (your_sequence[i] != sequence[i])
{
wrong_sequence();
return;
}
digitalWrite(blue_led, LOW);
}
}
}
right_sequence();
// }
}
void generate_sequence()
{
randomSeed(millis()); //in this way is really random!!!
int kaas[4] = {6, 2, 3, 5};
for (int i = 0; i < MAX_LEVEL; i++)
{
sequence[i] = kaas[random(0, 4)];
}
}
void wrong_sequence() //fout?
{
Serial.println("Fout, game over!!");
for (int i = 0; i < 3; i++) //3 keer knipperen
{
digitalWrite(yellow_led, HIGH);
digitalWrite(green_led, HIGH);
digitalWrite(red_led, HIGH);
digitalWrite(blue_led, HIGH);
delay(250);
digitalWrite(yellow_led, LOW);
digitalWrite(green_led, LOW);
digitalWrite(red_led, LOW);
digitalWrite(blue_led, LOW);
delay(250);
}
score=0; //reset score
gameStarted = false; //Zodat die lampjes weer rond draaien
tone(buzzer, 100); // game over geluidje voor 2 sec
lcd.clear();
lcd.print("GAME OVER!");
delay(2000);
noTone(buzzer);
DisplayScore();
blinkLampjesLoop(); // start lampjes loop weer en reset de rest
level = 1;
velocity = 1000;
}
void right_sequence() //goed?
{
score++;
if(score>scoreEEPROM){ //Dus als je de highscore hebt verbroken dan zet die t nieuwe record erin
EEPROM.write(EEPROMaddress, score); //hier write die naar byte 130
}
Serial.println("Right!!");
DisplayScore();
digitalWrite(yellow_led, LOW);
digitalWrite(green_led, LOW);
digitalWrite(red_led, LOW);
digitalWrite(blue_led, LOW);
delay(250);
digitalWrite(yellow_led, HIGH);
digitalWrite(green_led, HIGH);
digitalWrite(red_led, HIGH);
digitalWrite(blue_led, HIGH);
delay(500);
digitalWrite(yellow_led, LOW);
digitalWrite(green_led, LOW);
digitalWrite(red_led, LOW);
digitalWrite(blue_led, LOW);
delay(500);
if (level < MAX_LEVEL);
level++;
velocity -= 50; //increase difficulty
}
void playBuzzer(int button) {
if (button == 6) {// Lampjes
tone(buzzer, 950);
delay(100);
noTone(buzzer);
}
else if (button == 2) {
tone(buzzer, 850);
delay(100);
noTone(buzzer);
}
else if (button == 3) {
tone(buzzer, 750);
delay(100);
noTone(buzzer);
}
else if (button == 5) {
tone(buzzer, 650);
delay(100);
noTone(buzzer);
}
}
void DisplayScore(){
int scoreEEPROM2 = EEPROM.read(EEPROMaddress); //Score van rom
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Highscore: ");
lcd.print(scoreEEPROM2);
lcd.setCursor(0, 0);
lcd.print("Score: ");
lcd.print(score);
}