Dragon Bank: Music-related code snippet

For the speaker, we first modified a copy of the Game of Thrones theme to sound nice when repeated. Then we looked up every note frequency and wrote it out. We determined that the music sounded best at a speed of 200 bpm and found that the time an eighth note takes at this speed would be 150 ms.

Team Dracarys – Music composed for the speaker with the frequencies written out

When coding it, we set up 2 arrays. One array held the frequency of the note and the other held how long that note was to be played relative to the speed of an eighth note. So the first entry in the frequency array would correspond to the first note and would be 392.00. Then, because the note has a duration of 6 eighth notes, then a 6 went into the delay array. So the final arrays looked like this:

double frequencies[] = {392.00, 261.63, 311.13, 349.23, 392.00, 261.63, 311.13, 349.23, 293.66, 196.00, 0, 233.08, 261.63, 293.66, 196.00, 0, 233.08, 261.63, 293.66, 196.00, 0, 233.08, 261.63, 293.66, 0, 196.00, 0, 233.08, 0, 349.23, 233.08, 311.13, 293.66, 349.23, 233.08, 311.13, 293.66, 261.63, 174.61, 0, 207.65, 233.08, 261.63, 0, 174.61, 0, 207.65, 233.08, 261.63, 0, 174.61, 0, 207.65, 233.08, 261.63, 0, 174.61, 0, 207.65, 0};

int delays[] = {6, 6, 1, 1, 4, 4, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 6, 1, 1, 4, 4, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

Then, one loop of the music was:

for(int i = 0; i < 60; i++){
 
   if(frequencies[i] == 0){
     OCR1A = 1;
   } else{
     OCR1A = F_CPU / (frequencies[i] * TIMER1_PRESCALER * (2^octave)) - 1;
   }
   for(int j = 0; j < delays[i]; j++){
     _delay_ms(150);
   }
}//for

Then, a variable called octave went back and forth between 0 and 1 to cause it to alternate octaves with each play through. This was then all put into a while-loop what would play the song only when told to by the master board. Then all of that was put into a loop that would run forever.