int speakerPin = 27;
//"happy birthday melody notes"
char notes[] = "GGAGcB GGAGdc GGxecBA yyecdc";
int beats[] = { 2, 2, 8, 8, 8, 16, 1, 8, 2, 8, 8, 8, 16, 1, 2, 2, 8, 8, 8, 8, 16, 1, 8, 2, 8, 8, 8, 16 };
void playTone(int tone) {
for (long i = 0; i < 100; i++) {
sigmaDeltaWrite(0, 0);
delayMicroseconds(tone);
sigmaDeltaWrite(0, 100);
delayMicroseconds(tone);
}
}
void playNote(char note) {
//notes
char note_name[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'x', 'y' };
int timeHigh[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 834, 765, 593, 468, 346, 224, 655 , 715 };
// play the tone corresponding to the note name
for (int i = 0; i < sizeof(note_name); i++) {
if (note_name[i] == note) {
playTone(timeHigh[i]);
}
}
}
void setup()
{
'sigmaDetachaRead'(0, 12000);
//attach pin speakerPin to channel 0
'sigmaDeltaAttachPin'(speakerPin==27);
//initialize channel 0 to off
'sigmaDetachaWrite'(0, 12000);
}
void loop() {
for (int i = 0; i < strlen(notes); i++) {
// space is rest note
if (notes[i] == ' ') {
delay(beats[i] * 100);
} else {
playNote(notes[i]);
}
// pause between notes
delay(100);
}
}