play songs

Dependencies:   mbed

Committer:
maclobdell
Date:
Sat May 21 17:20:20 2016 +0000
Revision:
7:7adef7c066ea
Parent:
6:406f73a0eb49
remove rtos library.  not needed for this bare-metal example.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maclobdell 0:0fb17f0d39e7 1 #include "mbed.h" // this tells us to load mbed related functions
maclobdell 1:8922cc02aec7 2 #include "tones.h" // list of all the tones and their frequencies
maclobdell 0:0fb17f0d39e7 3
maclobdell 0:0fb17f0d39e7 4 PwmOut buzzer(D3); // our buzzer is a PWM output (pulse-width modulation)
maclobdell 0:0fb17f0d39e7 5
maclobdell 5:7003c9f6d633 6 static int BPM = 80;
maclobdell 0:0fb17f0d39e7 7
maclobdell 0:0fb17f0d39e7 8 static void silence() {
maclobdell 0:0fb17f0d39e7 9 buzzer.write(0.0f); // silence!
maclobdell 0:0fb17f0d39e7 10 }
maclobdell 0:0fb17f0d39e7 11
maclobdell 0:0fb17f0d39e7 12 // this is our function that plays a tone.
maclobdell 0:0fb17f0d39e7 13 // Takes in a tone frequency, and after duration (in ms.) we stop playing again
maclobdell 0:0fb17f0d39e7 14 static void play_tone(int tone) {
maclobdell 2:610196c7b41c 15 buzzer.period_us(1000000/(tone));
maclobdell 0:0fb17f0d39e7 16 buzzer.write(0.10f); // 10% duty cycle, otherwise it's too loud
maclobdell 0:0fb17f0d39e7 17 }
maclobdell 0:0fb17f0d39e7 18
maclobdell 0:0fb17f0d39e7 19 static void play_song(int notes_left, int* melody, int* duration) {
maclobdell 1:8922cc02aec7 20
maclobdell 0:0fb17f0d39e7 21 // YOUR CODE HERE
maclobdell 7:7adef7c066ea 22 // melody and duration are pointers, they point to the array of tones and durations we declared earlier
maclobdell 7:7adef7c066ea 23 // every time we play a note we up these pointers (move one element forward)
maclobdell 7:7adef7c066ea 24 // so the current tone is always the first element of melody (same for duration)
maclobdell 7:7adef7c066ea 25
maclobdell 7:7adef7c066ea 26 int length;
maclobdell 7:7adef7c066ea 27
maclobdell 7:7adef7c066ea 28 while(notes_left > 0)
maclobdell 7:7adef7c066ea 29 {
maclobdell 7:7adef7c066ea 30
maclobdell 7:7adef7c066ea 31 int tone = melody[0];
maclobdell 7:7adef7c066ea 32 // BPM is quarter notes per minute, so length in milliseconds is:
maclobdell 7:7adef7c066ea 33 length = static_cast<int>(static_cast<float>(1000 / duration[0]) * (60000.0f / static_cast<float>(BPM * 1000)));
maclobdell 7:7adef7c066ea 34
maclobdell 7:7adef7c066ea 35 play_tone(tone);
maclobdell 7:7adef7c066ea 36
maclobdell 7:7adef7c066ea 37 // after half the length of this tone, we silence
maclobdell 7:7adef7c066ea 38 wait_ms(length / 2);
maclobdell 7:7adef7c066ea 39 silence();
maclobdell 7:7adef7c066ea 40
maclobdell 7:7adef7c066ea 41 //after the full length of this tone, call next note
maclobdell 7:7adef7c066ea 42 wait_ms(length);
maclobdell 7:7adef7c066ea 43
maclobdell 7:7adef7c066ea 44 // after the full length of this tone, we up the melody, and down the notes_left
maclobdell 7:7adef7c066ea 45
maclobdell 7:7adef7c066ea 46 notes_left--;
maclobdell 7:7adef7c066ea 47 melody++;
maclobdell 7:7adef7c066ea 48 duration++;
maclobdell 7:7adef7c066ea 49
maclobdell 7:7adef7c066ea 50 }
maclobdell 7:7adef7c066ea 51
maclobdell 7:7adef7c066ea 52 // we're done! just finish this note and silence
maclobdell 7:7adef7c066ea 53 wait_ms(length / 2);
maclobdell 7:7adef7c066ea 54 silence();
maclobdell 0:0fb17f0d39e7 55 }
maclobdell 0:0fb17f0d39e7 56
maclobdell 0:0fb17f0d39e7 57 // this code runs when the microcontroller starts up
maclobdell 1:8922cc02aec7 58 int main() {
maclobdell 2:610196c7b41c 59
maclobdell 0:0fb17f0d39e7 60 // declare a melody
maclobdell 6:406f73a0eb49 61 int melody[] = {
maclobdell 6:406f73a0eb49 62 NOTE_C4, NOTE_D4, NOTE_DS4, NOTE_C4, NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4,
maclobdell 6:406f73a0eb49 63 NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4,
maclobdell 6:406f73a0eb49 64 NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4
maclobdell 0:0fb17f0d39e7 65 };
maclobdell 2:610196c7b41c 66
maclobdell 0:0fb17f0d39e7 67 // note durations: 4 = quarter note, 8 = eighth note, etc.:
maclobdell 6:406f73a0eb49 68 // the rapid succession of 16th notes produces a twill effect
maclobdell 0:0fb17f0d39e7 69 int duration[] = {
maclobdell 6:406f73a0eb49 70 2, 8, 4, 4, 16, 16, 16, 16,
maclobdell 6:406f73a0eb49 71 16, 16, 16, 16, 16, 16, 16,
maclobdell 6:406f73a0eb49 72 16, 16, 16, 16, 16
maclobdell 2:610196c7b41c 73 };
maclobdell 6:406f73a0eb49 74
maclobdell 0:0fb17f0d39e7 75
maclobdell 0:0fb17f0d39e7 76 // melody & duration are on the heap, need to get them on the stack
maclobdell 0:0fb17f0d39e7 77 int *m = new int[sizeof(melody) / sizeof(int)];
maclobdell 0:0fb17f0d39e7 78 memcpy(m, melody, sizeof(melody));
maclobdell 0:0fb17f0d39e7 79 int *d = new int[sizeof(duration) / sizeof(int)];
maclobdell 0:0fb17f0d39e7 80 memcpy(d, duration, sizeof(duration));
maclobdell 0:0fb17f0d39e7 81
maclobdell 0:0fb17f0d39e7 82 if (sizeof(melody) != sizeof(duration)) {
maclobdell 0:0fb17f0d39e7 83 printf("Melody and duration do not have same number of elements! Aborting!\r\n");
maclobdell 2:610196c7b41c 84 return 1;
maclobdell 0:0fb17f0d39e7 85 }
maclobdell 0:0fb17f0d39e7 86
maclobdell 0:0fb17f0d39e7 87 play_song(sizeof(melody) / sizeof(int), m, d);
maclobdell 2:610196c7b41c 88
maclobdell 2:610196c7b41c 89 return 0;
maclobdell 0:0fb17f0d39e7 90 }