1int speakerPin = 7;
2int length = 28;
3char notes[] = "GGAGcB GGAGdc GGxecBA yyecdc";
4int beats[] = {2,2,8,8,8,16,1,2,2,8,8,8,16,1,2,2,8,8,8,8,16,1,2,2,8,8,8,16};
5int tempo = 200;
6
7void playTone(int tone, int duration) {
8for (long i = 0; i < duration * 1000L; i += tone * 2) {
9 digitalWrite(speakerPin, HIGH);
10 delayMicroseconds(tone);
11 digitalWrite(speakerPin, LOW);
12 delayMicroseconds(tone);
13}
14}
15
16void playNote(char note, int duration) {
17char names[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B',
18
19 'c', 'd', 'e', 'f', 'g', 'a', 'b',
20
21 'x', 'y' };
22
23int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014,
24
25 956, 834, 765, 593, 468, 346, 224,
26
27 655 , 715 };
28
29int SPEE = 5;
30
31
32
33for (int i = 0; i < 17; i++) {
34
35 if (names[i] == note) {
36 int newduration = duration/SPEE;
37 playTone(tones[i], newduration);
38 }
39}
40}
41
42void setup() {
43pinMode(speakerPin, OUTPUT);
44}
45
46void loop() {
47for (int i = 0; i < length; i++) {
48 if (notes[i] == ' ') {
49 delay(beats[i] * tempo);
50 } else {
51 playNote(notes[i], beats[i] * tempo);
52 }
53
54 delay(tempo);
55}
56}
Sparkbuzzer
5 years ago
Try this project to wish your loved one with arduino also watch my video on youtube