-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaint_that_track_arduino
425 lines (405 loc) · 8.98 KB
/
paint_that_track_arduino
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
//librairie de notes
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
#define NOTE_NULL 1
//on utilise la bande de 120 leds Adafruit, cf http://learn.adafruit.com/adafruit-neopixel-uberguide/
#include <Adafruit_NeoPixel.h>
#define PIN 13
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino 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(120, PIN, NEO_GRB + NEO_KHZ800);
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
//durées des notes au tempo 100
int noire=600;
int croche=300;
int blanche=1200;
int mesure=2400;
//dancing queeeeen
//int melody[] = {NOTE_C3, NOTE_D3, NOTE_E3, NOTE_E3, NOTE_F3, NOTE_F3, NOTE_E3, NOTE_F3, NOTE_F3, NOTE_G3, NOTE_F3, NOTE_E3, NOTE_F3, NOTE_F3, NOTE_NULL, NOTE_E3, NOTE_F3, NOTE_F3, NOTE_E3, NOTE_F3, NOTE_F3, NOTE_G3, NOTE_F3, NOTE_G3, NOTE_A3,NOTE_A3, NOTE_G3};
int lumiere[] = {1, 3, 5, 5, 6, 6, 5, 6, 6, 8, 6, 5, 6, 6, 0, 5, 6, 5, 5, 6, 6, 8, 6, 8, 10, 10, 8};
int noteDurations[] = {croche, croche, noire, noire, noire, blanche, noire, noire, noire, croche, croche, noire, noire, blanche, mesure, noire, noire, blanche, noire, noire, noire, croche, croche, noire, noire, noire, noire};
#define LENGTH 27
//parametres capteur et switch
const int echo = A0;
const int trigger = 4;
unsigned long pulseTime = 0;
int switchPin = 2;
//score! (valeur et numéro de la prochaine led)
int score = 0;
int ledScore = 115;
void setup() {
//initialise le ruban de led
strip.begin();
strip.show();
// make the init pin an output:
pinMode(trigger, OUTPUT);
// make the echo pin an input:
pinMode(echo, INPUT);
// initialize the serial port:
Serial.begin(9600);
//initialise la lecture du switch
pinMode(switchPin, INPUT);
//parcours de la gamme au démarage
for (int k = 1; k < 21; k++) {
if (k < 18) {
strip.setPixelColor(3*k, couleur(k));
}
if (k > 3) {
strip.setPixelColor(3*(k-3), strip.Color(0,0,0));
}
strip.show();
delay(500);
}
strip.show();
}
void loop() {
if (digitalRead(switchPin) == HIGH) {
chanson();
}
else {
int led = jouerUneNote();
if (led) {
strip.setPixelColor(3*led, couleur(led));
strip.show();
delay(100);
strip.setPixelColor(3*led, 0,0,0);
strip.show();
delay(5);
}
}
}
void allumer (int led, int temps) {
strip.setPixelColor(3*led, couleur(led));
strip.show(); //allumer la LED numero led
delay(temps*0.30); //on laisse le temps de placer sa main à hauteur et d'entendre
int note = jouerUneNote(); //jouer la note placée
modifierScore(note, led);
delay(temps); //durée de la note
strip.setPixelColor(3*led, strip.Color(0,0,0));
strip.show(); // éteindre la LED
delay(temps*0.30);
}
int jouerUneNote() {
float distance=0;
// send the sensor a 10microsecond pulse:
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
//wait for the pulse to return. The pulse
//goes from low to HIGH to low, so we specify
//that we want a HIGH-going pulse below:
pulseTime = pulseIn(echo, HIGH);
distance= float (pulseTime)*0.0130192839+1.9856160084;
//on determine la note et on la joue
if (distance<10) {
//Do3
Serial.println("a");
return 1;
}
else if (distance<15) {
//Do#3
Serial.println("b");
return 2;
}
else if (distance<20) {
//Ré3
Serial.println("c");
return 3;
}
else if (distance<25) {
//Ré#3
Serial.println("d");
return 4;
}
else if (distance<30) {
//Mi3
Serial.println("e");
return 5;
}
else if (distance<35) {
//Fa3
Serial.println("f");
return 6;
}
else if (distance<40) {
//Fa#3
Serial.println("g");
return 7;
}
else if (distance<45) {
//Sol3
Serial.println("h");
return 8;
}
else if (distance<50) {
//Sol#3
Serial.println("i");
return 9;
}
else if (distance<55) {
//La3
Serial.println("j");
return 10;
}
else if (distance<60) {
//La#3
Serial.println("k");
return 11;
}
else if (distance<65) {
//Si3
Serial.println("l");
return 12;
}
else if (distance<70) {
//Do4
Serial.println("m");
return 13;
}
else if (distance<75) {
//Do#4
Serial.println("n");
return 14;
}
else if (distance<80) {
//Ré4
Serial.println("o");
return 15;
}
else if (distance<85) {
//Ré#4
Serial.println("p");
return 16;
}
else if (distance<90) {
//Mi4
Serial.println("q");
return 17;
}
else {
return 0;
}
}
//choix de la couleur
uint32_t couleur(int led) {
float r;
float v;
float b;
switch (led) {
case 1:
r = 0.43;
v = 0.0;
b = 0.65;
break;
case 2:
r = 0.33;
v = 0.0;
b = 1.0;
break;
case 3:
r = 0.0;
v = 0.0;
b = 1.0;
break;
case 4:
r = 0.0;
v = 0.4;
b = 1.0;
break;
case 5:
r = 0.0;
v = 0.8;
b = 1.0;
break;
case 6:
r = 0.0;
v = 1.0;
b = 0.5;
break;
case 7:
r = 0.14;
v = 1.0;
b = 0.0;
break;
case 8:
r = 0.29;
v = 1.0;
b = 0.0;
break;
case 9:
r = 0.0;
v = 1.0;
b = 0.0;
case 10:
r = 0.43;
v = 1.0;
b = 0.0;
break;
case 11:
r = 0.57;
v = 1.0;
b = 0.0;
break;
case 12:
r = 0.71;
v = 1.0;
b = 0.0;
break;
case 13:
r = 0.86;
v = 1.0;
b = 0.0;
break;
case 14:
r = 1.0;
v = 1.0;
b = 0.0;
break;
case 15:
r = 1.0;
v = 0.85;
b = 0.0;
break;
case 16:
r = 1.0;
v = 0.38;
b = 0.0;
break;
case 17:
r = 1.0;
v = 0.0;
b = 0.0;
break;
default:
r = 0.0;
v = 0.0;
b = 0.0;
}
return strip.Color(255*r,255*v,255*b);
}
void chanson() {
int ledScore = 115;
for (int i = 0; i < LENGTH; i++) {
allumer(lumiere[i], noteDurations[i]);
afficherScore();
Serial.println(score, DEC);
}
delay(5000); //5 secondes en fin de chanson
//on reset l'affichage du score
for (int led = (116 - score/10); led <= 115; led++ ) {
strip.setPixelColor(led, 0,0,0);
}
strip.show();
score = 0;
ledScore = 115;
}
void modifierScore(int noteJouee, int noteAttendue) {
if (noteJouee == noteAttendue) {
score += 10;
}
else if (noteJouee && abs(noteJouee - noteAttendue) <= 1) {
score += 5;
}
}
void afficherScore() {
if (score < 401 && (score - (115 - ledScore)*10) >= 10) {
strip.setPixelColor(ledScore, 255,255,255);
strip.show();
ledScore -= 1;
}
}