This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Klaviartura.ino
553 lines (509 loc) · 13.3 KB
/
Klaviartura.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#include <Tone.h>
#include "kuzapazi.h"
#include "merrychristmas.h"
#include "happy.h"
#include "ringtone.h"
// Copyright
// Thanks to https://github.com/robsoncouto/arduino-songs, he did most songs that are included
// This code is meant for educational purposes and I, the owner don't allow making money (commercial use) with or without any modifications.
// copyright mytja - https://github.com/mytja/
// Version: Guinea pig 1.0
// Here are defined some important variables
int note = 0;
int mode = 2; // Set a mode - song
char text;
boolean songlearn = false;
// Here are defined LEDs for learning
const int led_c1 = 11;
const int led_d1 = 12;
const int led_e1 = A0;
const int led_f1 = A1;
const int led_g1 = A2;
const int led_a1 = A3;
const int led_h1 = A4; // USA uses B instead H
const int led_c2 = A5;
// Here are defined all the buttons for playing
const int c1 = 2;
const int d1 = 3;
const int e1 = 4;
const int f1 = 5;
const int g1 = 6;
const int a1 = 7;
const int h1 = 8; // USA uses B instead H
const int c2 = 9;
const int modeBtn = 13; // Button to change mode is here
// Here is the speaker defined
const int speaker = 10;
Tone tone1; // Here are tones defined
void ifModeBtn(){
if (digitalRead(modeBtn)==HIGH){
int mode = mode + 1;
note = 1000000;
Serial.println("modeBTN: ON");
delay(300);
if (mode==0){
led_startup();
}
else if (mode==1){
kuzapazi_refren();
}
else if (mode==2){
Serial.println("mode 2");
}
else if (mode==3){
Serial.println("mode 3");
}
if (mode==4){
mode = 0;
}
}
}
int notes_ringtone[] = {
// Nokia Ringtone
// Score available at https://musescore.com/user/29944637/scores/5266155
NOTE_E5, 8, NOTE_D5, 8, NOTE_FS4, 4, NOTE_GS4, 4,
NOTE_CS5, 8, NOTE_B4, 8, NOTE_D4, 4, NOTE_E4, 4,
NOTE_B4, 8, NOTE_A4, 8, NOTE_CS4, 4, NOTE_E4, 4,
NOTE_A4, 2,
};
int tempo = 180;
int wholenote = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0;
int notes = sizeof(notes_ringtone) / sizeof(notes_ringtone[0]) / 2;
void startup_ringtone(){
for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
// calculates the duration of each note
divider = notes_ringtone[thisNote + 1];
if (divider > 0) {
// regular note, just proceed
noteDuration = (wholenote) / divider;
} else if (divider < 0) {
// dotted notes are represented with negative durations!!
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
// we only play the note for 90% of the duration, leaving 10% as a pause
tone1.play(10, notes_ringtone[thisNote]);
// Wait for the specief duration before playing the next note.
delay(noteDuration);
// stop the waveform generation before the next note.
tone1.stop();
}
}
// int led_kuzapazi[] = {led_c1, led_c1, led_c1, led_c1, led_d1, led_d1, led_d1, led_d1, led_e1, led_e1, led_d1, led_d1, led_c1, led_c1, led_c1, led_c1};
// Function to turn all leds off
void turnoffall(){
digitalWrite(led_c1, LOW);
digitalWrite(led_d1, LOW);
digitalWrite(led_e1, LOW);
digitalWrite(led_f1, LOW);
digitalWrite(led_g1, LOW);
digitalWrite(led_a1, LOW);
digitalWrite(led_h1, LOW);
digitalWrite(led_c2, LOW);
}
void led_startup(){
digitalWrite(led_c1, HIGH);
delay(50);
digitalWrite(led_c1, LOW);
digitalWrite(led_d1, HIGH);
delay(50);
digitalWrite(led_d1, LOW);
digitalWrite(led_e1, HIGH);
delay(50);
digitalWrite(led_e1, LOW);
digitalWrite(led_f1, HIGH);
delay(50);
digitalWrite(led_f1, LOW);
digitalWrite(led_g1, HIGH);
delay(50);
digitalWrite(led_g1, LOW);
digitalWrite(led_a1, HIGH);
delay(50);
digitalWrite(led_a1, LOW);
digitalWrite(led_h1, HIGH);
delay(50);
digitalWrite(led_h1, LOW);
digitalWrite(led_c2, HIGH);
delay(50);
digitalWrite(led_c2, LOW);
turnoffall();
}
void kuzapazi_refren(){
for (note = 0; 15 > note; note++){
tone1.play(notes_kuzapazi[note]);
delay(250);
tone1.stop();
delay(100);
}
}
//void kuzapazi_learn(){
// note = 0;
// while (15 > note){
// note++;
// text = notes_kuzapazi[note];
// songlearn = true;
// while (songlearn==true){
// if (text==NOTE_C4){
// while (digitalRead(c1) == LOW){
// digitalWrite(led_c1, HIGH);
// delay(20);
// }
// digitalWrite(11, LOW);
// tone1.play(NOTE_C4);
// delay(100);
// tone1.stop();
// songlearn = false;
// }
// }
// }
//}
void setup() {
// Define speaker
pinMode(speaker, OUTPUT);
tone1.begin(speaker);
// Serial begin
Serial.begin(9600);
// Define Buttons
pinMode(c1, INPUT);
pinMode(d1, INPUT);
pinMode(e1, INPUT);
pinMode(f1, INPUT);
pinMode(g1, INPUT);
pinMode(a1, INPUT);
pinMode(h1, INPUT);
pinMode(c2, INPUT);
// Define LEDs
pinMode(led_c1, OUTPUT);
pinMode(led_d1, OUTPUT);
pinMode(led_e1, OUTPUT);
pinMode(led_f1, OUTPUT);
pinMode(led_g1, OUTPUT);
pinMode(led_a1, OUTPUT);
pinMode(led_h1, OUTPUT);
pinMode(led_c2, OUTPUT);
turnoffall();
led_startup();
startup_ringtone();
}
void loop() {
// Kuza pazi
while (mode==1){
kuzapazi_refren();
note = -1;
while (14 > note){
note++;
Serial.println(note);
songlearn = true;
while (songlearn==true){
if (notes_kuzapazi[note]==NOTE_C4){
while (digitalRead(c1) == LOW){
digitalWrite(led_c1, HIGH);
delay(20);
}
digitalWrite(led_c1, LOW);
tone1.play(NOTE_C4);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_kuzapazi[note]==NOTE_D4){
while (digitalRead(d1) == LOW){
digitalWrite(led_d1, HIGH);
delay(20);
}
digitalWrite(led_d1, LOW);
tone1.play(NOTE_D4);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_kuzapazi[note]==NOTE_E4){
while (digitalRead(e1) == LOW){
digitalWrite(led_e1, HIGH);
delay(20);
}
digitalWrite(led_e1, LOW);
tone1.play(NOTE_E4);
delay(300);
tone1.stop();
songlearn = false;
}
}
}
mode = 0;
// We wish you a merry christmas
}
while (mode==2){
note = -1;
while (58 > note){
note++;
Serial.println(note);
songlearn = true;
while (songlearn==true){
if (notes_merrychristmas[note]==NOTE_C5){
while (digitalRead(c1) == LOW){
digitalWrite(led_c1, HIGH);
delay(20);
}
digitalWrite(led_c1, LOW);
tone1.play(NOTE_C5);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_merrychristmas[note]==NOTE_D5){
while (digitalRead(d1) == LOW){
digitalWrite(led_d1, HIGH);
delay(20);
}
digitalWrite(led_d1, LOW);
tone1.play(NOTE_D5);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_merrychristmas[note]==NOTE_E5){
while (digitalRead(e1) == LOW){
digitalWrite(led_e1, HIGH);
delay(20);
}
digitalWrite(led_e1, LOW);
tone1.play(NOTE_E5);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_merrychristmas[note]==NOTE_F5){
while (digitalRead(f1) == LOW){
digitalWrite(led_f1, HIGH);
delay(20);
}
digitalWrite(led_f1, LOW);
tone1.play(NOTE_F5);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_merrychristmas[note]==NOTE_G5){
while (digitalRead(g1) == LOW){
digitalWrite(led_g1, HIGH);
delay(20);
}
digitalWrite(led_g1, LOW);
tone1.play(NOTE_G5);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_merrychristmas[note]==NOTE_A5){
while (digitalRead(a1) == LOW){
digitalWrite(led_a1, HIGH);
delay(20);
}
digitalWrite(led_a1, LOW);
tone1.play(NOTE_A5);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_merrychristmas[note]==NOTE_B5){
while (digitalRead(h1) == LOW){
digitalWrite(led_h1, HIGH);
delay(20);
}
digitalWrite(led_h1, LOW);
tone1.play(NOTE_B5);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_merrychristmas[note]==NOTE_C6){
while (digitalRead(c2) == LOW){
digitalWrite(led_c2, HIGH);
delay(20);
}
digitalWrite(led_c2, LOW);
tone1.play(NOTE_C6);
delay(300);
tone1.stop();
songlearn = false;
}
else if(notes_merrychristmas[note]==4){
delay(10);
songlearn = false;
}
else if(notes_merrychristmas[note]==8){
delay(10);
songlearn = false;
}
else if(notes_happy[note]==2){
delay(10);
songlearn = false;
}
}
}
mode = 0;
}
// Happy birthday
while (mode==3){
note = -1;
while (47 > note){
note++;
Serial.println(note);
songlearn = true;
while (songlearn==true){
if (notes_happy[note]==NOTE_C4){
while (digitalRead(c1) == LOW){
digitalWrite(led_c1, HIGH);
delay(20);
}
digitalWrite(led_c1, LOW);
tone1.play(NOTE_C4);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_happy[note]==NOTE_D4){
while (digitalRead(d1) == LOW){
digitalWrite(led_d1, HIGH);
delay(20);
}
digitalWrite(led_d1, LOW);
tone1.play(NOTE_D4);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_happy[note]==NOTE_E4){
while (digitalRead(e1) == LOW){
digitalWrite(led_e1, HIGH);
delay(20);
}
digitalWrite(led_e1, LOW);
tone1.play(NOTE_E4);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_happy[note]==NOTE_F4){
while (digitalRead(f1) == LOW){
digitalWrite(led_f1, HIGH);
delay(20);
}
digitalWrite(led_f1, LOW);
tone1.play(NOTE_F4);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_happy[note]==NOTE_G4){
while (digitalRead(g1) == LOW){
digitalWrite(led_g1, HIGH);
delay(20);
}
digitalWrite(led_g1, LOW);
tone1.play(NOTE_G4);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_happy[note]==NOTE_A4){
while (digitalRead(a1) == LOW){
digitalWrite(led_a1, HIGH);
delay(20);
}
digitalWrite(led_a1, LOW);
tone1.play(NOTE_A4);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_happy[note]==NOTE_B4){
while (digitalRead(h1) == LOW){
digitalWrite(led_h1, HIGH);
delay(20);
}
digitalWrite(led_h1, LOW);
tone1.play(NOTE_B4);
delay(300);
tone1.stop();
songlearn = false;
}
else if (notes_happy[note]==NOTE_C5){
while (digitalRead(c2) == LOW){
digitalWrite(led_c2, HIGH);
delay(20);
}
digitalWrite(led_c2, LOW);
tone1.play(NOTE_C5);
delay(300);
tone1.stop();
songlearn = false;
}
else if(notes_happy[note]==4){
delay(10);
songlearn = false;
}
else if(notes_happy[note]==8){
delay(10);
songlearn = false;
}
else if(notes_happy[note]==2){
delay(10);
songlearn = false;
}
}
}
mode = 0;
}
// Standard piano - mode 0
if (digitalRead(c1)==HIGH){
tone1.play(NOTE_C4);
Serial.println("c1");
delay(30);
tone1.stop();
}
else if (digitalRead(d1)==HIGH){
tone1.play(NOTE_D4);
Serial.println("d1");
delay(30);
tone1.stop();
}
else if (digitalRead(e1)==HIGH){
Serial.println("e1");
tone1.play(NOTE_E4);
delay(30);
tone1.stop();
}
else if (digitalRead(f1)==HIGH){
Serial.println("f1");
tone1.play(NOTE_F4);
delay(30);
tone1.stop();
}
else if (digitalRead(g1)==HIGH){
Serial.println("g1");
tone1.play(NOTE_G4);
delay(30);
tone1.stop();
}
else if (digitalRead(a1)==HIGH){
Serial.println("a1");
tone1.play(NOTE_A4);
delay(30);
tone1.stop();
}
else if (digitalRead(h1)==HIGH){
Serial.println("h1");
tone1.play(NOTE_B4);
delay(30);
tone1.stop();
}
else if (digitalRead(c2)==HIGH){
Serial.println("c2");
tone1.play(NOTE_C5);
delay(30);
tone1.stop();
}
}