-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicale.ino
99 lines (84 loc) · 2.19 KB
/
musicale.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
#define SOUND_OUT 9
#define LED 13
#define WHOLE 1.0
#define HALF .5
#define QUARTER .25
#define EIGHTH .128
#define MIDDLE_C 261.626
#define CIS 277
#define D 294
#define DIS 311
#define E 330
#define MIDDLE_F 349
#define FIS 370
#define G 392
#define GIS 415
#define A 440
#define AIS 466
#define B 494
int bpm = 90;
void setup() {
pinMode(LED, OUTPUT);
pinMode(SOUND_OUT, OUTPUT);
// Serial.begin(9600);
}
void loop() {
playNote(WHOLE, MIDDLE_C);
playNote(2.0, 440);
playNote(HALF, MIDDLE_C);
playNote(QUARTER, B);
playNote(QUARTER, 220);
playNote(WHOLE, 523.251);
rest(WHOLE);
playNote(QUARTER, MIDDLE_C);
playNote(QUARTER, D);
playNote(QUARTER, E);
playNote(QUARTER, MIDDLE_F);
playNote(QUARTER, G);
playNote(QUARTER, A);
playNote(QUARTER, B);
playNote(QUARTER, MIDDLE_C * 2);
rest(WHOLE);
}
int hertzToDelay(float hertz) {
return (int) 1000.0 / hertz * 1000.0;
}
void rest(int noteValue) {
delay(valueToDuration(noteValue) / 100);
// Serial.println("Rest.");
}
void playNote(float noteValue, float frequency) {
/* Serial.println("playing note: ");
Serial.print(noteValue, DEC);
Serial.print('\t');
Serial.print(frequency, DEC);
Serial.print('\t');
Serial.print(valueToDuration(noteValue), DEC);
Serial.println(""); */
emitTone(valueToDuration(noteValue), hertzToDelay(frequency));
}
float valueToDuration(float noteValue) { // in µSec
return 100000 * 60 * noteValue / bpm;
}
void blink(int on, int off, int frequency) {
digitalWrite(LED, HIGH);
emitTone(on, frequency);
digitalWrite(LED, LOW);
delay(off);
}
void emitTone(float durationInMiliseconds, int frequencyInMicroseconds) { // Emit tone on pin SOUND_OUT
digitalWrite(LED, HIGH);
/* Serial.println("Emitting tone: ");
Serial.print(durationInMiliseconds * 10 / frequencyInMicroseconds, DEC);
Serial.print('\t');
Serial.print(frequencyInMicroseconds, DEC);
Serial.println("");
*/
for (int i = 0; i < durationInMiliseconds * 10 / frequencyInMicroseconds; i++) {
digitalWrite(SOUND_OUT, HIGH);
delayMicroseconds(frequencyInMicroseconds / 2);
digitalWrite(SOUND_OUT, LOW);
delayMicroseconds(frequencyInMicroseconds / 2);
}
digitalWrite(LED, LOW);
}