#include "pitches.h";
// const int buzzerPin = 9;
// // Define note frequencies
// int E4 = 329;
// int G4 = 392;
// int J4 = 440;
// int B4 = 466;
// void setup() {
// pinMode(buzzerPin, OUTPUT);
// // put your setup code here, to run once:
// }
// void loop() {
// playNote(E4, 500); // Arise
// delay(50); // Short pause between notes
// playNote(G4, 500); // O
// delay(50);
// playNote(J4, 500); // Com
// delay(50);
// playNote(E4, 500); // Pa
// delay(50);
// playNote(B4, 500); // Tri
// delay(50);
// playNote(J4, 500); // O
// delay(50);
// playNote(G4, 1000); // Tots
// delay(50);
// // Add more notes for the remaining parts of the anthem...
// // A short pause at the end
// delay(2000);
// }
// void playNote(int frequency, int duration) {
// tone(buzzerPin, frequency, duration);
// delay(duration + 50); // Add a small delay between notes for better sound separation
// noTone(buzzerPin);
// }
#define melodyPin 9
int melody[] = {
NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,
NOTE_E5,
NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,
NOTE_D5, NOTE_G5
};
int tempo[] = {
8, 8, 4,
8, 8, 4,
8, 8, 8, 8,
2,
8, 8, 8, 8,
8, 8, 8, 16, 16,
8, 8, 8, 8,
4, 4
};
int wish_melody[] = {
NOTE_B3,
NOTE_F4, NOTE_F4, NOTE_G4, NOTE_F4, NOTE_E4,
NOTE_D4, NOTE_D4, NOTE_D4,
NOTE_G4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_F4,
NOTE_E4, NOTE_E4, NOTE_E4,
NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4, NOTE_G4,
NOTE_F4, NOTE_D4, NOTE_B3, NOTE_B3,
NOTE_D4, NOTE_G4, NOTE_E4,
NOTE_F4
};
int wish_tempo[] = {
4,
4, 8, 8, 8, 8,
4, 4, 4,
4, 8, 8, 8, 8,
4, 4, 4,
4, 8, 8, 8, 8,
4, 4, 8, 8,
4, 4, 4,
2
};
int santa_melody[] = {
NOTE_G4,
NOTE_E4, NOTE_F4, NOTE_G4, NOTE_G4, NOTE_G4,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, NOTE_C5,
NOTE_E4, NOTE_F4, NOTE_G4, NOTE_G4, NOTE_G4,
NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4,
NOTE_E4, NOTE_G4, NOTE_C4, NOTE_E4,
72 NOTE_D4, NOTE_F4, NOTE_B3,
73 NOTE_C4
74};
75
76int santa_tempo[] = {
77 8,
78 8, 8, 4, 4, 4,
79 8, 8, 4, 4, 4,
80 8, 8, 4, 4, 4,
81 8, 8, 4, 2,
82 4, 4, 4, 4,
83 4, 2, 4,
84 1
85};
86
87int switchOne = 0;
88int switchTwo = 0;
89int switchThree = 0;
90
91void setup(void) {
92 pinMode(9, OUTPUT); // Buzzer
93 pinMode(13, OUTPUT); // Led indicator when singing a note
94 pinMode(2, INPUT);
95 pinMode(3, INPUT);
96 pinMode(4, INPUT);
97}
98
99void loop() {
100 switchOne = digitalRead(2);
101 switchTwo = digitalRead(3);
102 switchThree = digitalRead(4);
103 if (switchOne == HIGH) {
104 sing(1);
105 } else if (switchTwo == HIGH) {
106 sing(2);
107 } else if (switchThree == HIGH) {
108 sing(3);
109 }
110}
111
112int song = 0;
113
114void sing(int s) {
115 // iterate over the notes of the melody:
116 song = s;
117 if (song == 3) {
118 Serial.println(" 'We wish you a Merry Christmas'");
119 int size = sizeof(wish_melody) / sizeof(int);
120 for (int thisNote = 0; thisNote < size; thisNote++) {
121
122 // to calculate the note duration, take one second
123 // divided by the note type.
124 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
125 int noteDuration = 1000 / wish_tempo[thisNote];
126
127 buzz(melodyPin, wish_melody[thisNote], noteDuration);
128
129 // to distinguish the notes, set a minimum time between them.
130 // the note's duration + 30% seems to work well:
131 int pauseBetweenNotes = noteDuration * 1.30;
132 delay(pauseBetweenNotes);
133
134 // stop the tone playing:
135 buzz(melodyPin, 0, noteDuration);
136
137 }
138 } else if (song == 2) {
139 Serial.println(" 'Santa Claus is coming to town'");
140 int size = sizeof(santa_melody) / sizeof(int);
141 for (int thisNote = 0; thisNote < size; thisNote++) {
142
143 // to calculate the note duration, take one second
144 // divided by the note type.
145 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
146 int noteDuration = 900 / santa_tempo[thisNote];
147
148 buzz(melodyPin, santa_melody[thisNote], noteDuration);
149
150 // to distinguish the notes, set a minimum time between them.
151 // the note's duration + 30% seems to work well:
152 int pauseBetweenNotes = noteDuration * 1.30;
153 delay(pauseBetweenNotes);
154
155 // stop the tone playing:
156 buzz(melodyPin, 0, noteDuration);
157
158 }
159 } else {
160
161 Serial.println(" 'Jingle Bells'");
162 int size = sizeof(melody) / sizeof(int);
163 for (int thisNote = 0; thisNote < size; thisNote++) {
164
165 // to calculate the note duration, take one second
166 // divided by the note type.
167 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
168 int noteDuration = 1000 / tempo[thisNote];
169
170 buzz(melodyPin, melody[thisNote], noteDuration);
171
172 // to distinguish the notes, set a minimum time between them.
173 // the note's duration + 30% seems to work well:
174 int pauseBetweenNotes = noteDuration * 1.30;
175 delay(pauseBetweenNotes);
176
177 // stop the tone playing:
178 buzz(melodyPin, 0, noteDuration);
179
180 }
181 }
182}
183
184void buzz(int targetPin, long frequency, long length) {
185 digitalWrite(13, HIGH);
186 long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
187 //// 1 second's worth of microseconds, divided by the frequency, then split in half since
188 //// there are two phases to each cycle
189 long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
190 //// multiply frequency, which is really cycles per second, by the number of seconds to
191 //// get the total number of cycles to produce
192 for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
193 digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
194 delayMicroseconds(delayValue); // wait for the calculated delay value
195 digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
196 delayMicroseconds(delayValue); // wait again or the calculated delay value
197 }
// 198 digitalWrite(13, LOW);
199
200}
// void loop() {
// tone(buzzer, 90, 100);
// delay(100);
// noTone(buzzer);
// tone(buzzer, 50, 100);
// delay(1000);
// put your main code here, to run repeatedly:
// }