-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryGamePixelRing.ino
278 lines (258 loc) · 5.74 KB
/
MemoryGamePixelRing.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
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#ifndef PSTR
#define PSTR // Make Arduino Due happy
#endif
#define LED 13
#define PIN 6
#define buttonYellow 5
#define buttonBlue 4
#define buttonGreen 3
#define buttonRed 2
#define cYellow 0x0C0C00
#define cRed 0x0C0000
#define cGreen 0x000C00
#define cBlue 0x00000C
#define cWhite 0x0C0C0C
#define cBlack 0x000000
#define cPurple 0x0C000C
#define numPixels 16
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numPixels, PIN, NEO_GRB + NEO_KHZ800);
//Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(5, 8, 6,
// NEO_MATRIX_TOP + NEO_MATRIX_RIGHT +
// NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE,
// NEO_GRB + NEO_KHZ800);
unsigned long wacht = 1000;
unsigned int sequence = 4;
unsigned int steps = 0;
#define maxsequence 50
unsigned int score = 1;
unsigned int array[maxsequence];
void setup() {
pinMode(LED, OUTPUT);
pinMode(buttonRed, INPUT_PULLUP);
pinMode(buttonGreen, INPUT_PULLUP);
pinMode(buttonBlue, INPUT_PULLUP);
pinMode(buttonYellow,INPUT_PULLUP);
randomSeed(analogRead(0));
strip.begin();
strip.show(); // Initialize all pixels to 'off'
rood();
delay(1000);
groen();
delay(1000);
blauw();
delay(1000);
geel();
delay(1000);
Serial.begin(9600);
}
void loop() {
Serial.println("-------------------------------");
if ( sequence > maxsequence) {
einde();
}
wit();
for( int rndmx = 0; rndmx < sequence; rndmx++) {
array[rndmx] = random(1,5);
Serial.print(array[rndmx]);
Serial.print(" ");
}
Serial.println();
countDown(1,cWhite);
uit();
for( int showx = 0; showx < sequence; showx++) {
switch (array[showx]) {
case 0:
uit();
break;
case 1:
rood();
break;
case 2:
groen();
break;
case 3:
blauw();
break;
case 4:
geel();
break;
}
delay(wacht);
uit();
delay(250);
}
uit();
int arrayCounter = 0;
unsigned int bt = 0;
unsigned int bt_old = 0;
Serial.println(steps);
Serial.println((wacht/numPixels)*(4-steps));
for( int loopx = numPixels; loopx > 0 ; loopx--) {
// Serial.print("loopx: ");
// Serial.println(loopx);
long time = millis() + ((wacht/numPixels)*(5-steps));
strip.setPixelColor(loopx,cPurple);
strip.show();
digitalWrite(LED,LOW);
while ( time > millis() )
{
digitalWrite(LED,HIGH);
bt = button();
if ( (bt_old != 0) && (bt == 0) ) {
bt_old = 0;
}
if ( (bt_old == 0) && (bt != 0) ) {
bt_old = bt;
if ( array[arrayCounter] == bt) {
Serial.println("ok");
arrayCounter++;
} else {
Serial.println("error");
error();
loopx=0;
}
if ( arrayCounter == sequence ) {
loopx = 0;
}
}
}
digitalWrite(LED,LOW);
}
Serial.println("EINDE");
if ( arrayCounter != sequence) {
loose();
} else {
win();
steps++;
if( steps > 4) {
steps = 0;
sequence++;
}
wacht = wacht - (wacht / 100);
}
delay(1000);
// while( readButton(buttonRed) == HIGH ) {
// delay(50);
// }
}
int button() {
boolean rlb;
boolean rb;
boolean glb;
boolean gb;
boolean blb;
boolean bb;
boolean ylb;
boolean yb;
rlb = digitalRead(buttonRed);
glb = digitalRead(buttonGreen);
blb = digitalRead(buttonBlue);
ylb = digitalRead(buttonYellow);
delay(50);
rb = digitalRead(buttonRed);
gb = digitalRead(buttonGreen);
bb = digitalRead(buttonBlue);
yb = digitalRead(buttonYellow);
if ( ( rb == LOW) && (rlb == LOW) ) {
return(1);
}
if ( ( gb == LOW) && (glb == LOW) ) {
return(2);
}
if ( ( bb == LOW) && (blb == LOW) ) {
return(3);
}
if ( ( yb == LOW) && (ylb == LOW) ) {
return(4);
}
return 0;
}
int readButton(int button) {
boolean lb;
boolean b;
lb = digitalRead(button);
delay(50);
b = digitalRead(button);
if ( ( b == LOW) && (lb == LOW) ) {
return(LOW);
} else {
return(HIGH);
}
}
void segmentKleur(int segment, unsigned long kleur){
segment--;
for( int pxl = segment * 4; pxl < (segment * 4) + 4; pxl++) {
strip.setPixelColor(pxl,kleur);
}
strip.show();
}
void rood(){
segmentKleur(1,cRed);
}
void groen(){
segmentKleur(3,cGreen);
}
void blauw(){
segmentKleur(4,cBlue);
}
void geel(){
segmentKleur(2,cYellow);
}
void fullCircle(unsigned long kleur, unsigned int np = numPixels) {
for( int x = 0; x < np; x++) {
strip.setPixelColor(x,kleur);
}
strip.show();
}
void countDown(unsigned int time, unsigned long kleur) {
fullCircle(kleur);
for ( int pxl = numPixels; pxl > 0; pxl--) {
delay((time*1000)/numPixels);
strip.setPixelColor(pxl,cBlack);
strip.show();
}
}
void uit(){
fullCircle(cBlack);
}
void wit(){
fullCircle(cWhite);
}
void error(){
fullCircle(cRed);
}
void winlose(unsigned long kleur){
fullCircle(kleur);
delay(750);
uit();
fullCircle(kleur,score);
delay(1000);
}
void win(){
score++;
winlose(cGreen);
}
void loose(){
winlose(cRed);
}
void einde() {
uit();
for ( int pxl = numPixels; pxl > 0; pxl--) {
strip.setPixelColor(pxl,cPurple);
strip.show();
delay((1000)/numPixels);
strip.setPixelColor(pxl,cBlack);
strip.show();
delay((1000)/numPixels);
}
}