play songs

Dependencies:   mbed

Committer:
maclobdell
Date:
Wed May 18 18:45:55 2016 +0000
Revision:
6:406f73a0eb49
Parent:
5:7003c9f6d633
Child:
7:7adef7c066ea
change melody

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 2:610196c7b41c 22
maclobdell 0:0fb17f0d39e7 23 }
maclobdell 0:0fb17f0d39e7 24
maclobdell 0:0fb17f0d39e7 25 // this code runs when the microcontroller starts up
maclobdell 1:8922cc02aec7 26 int main() {
maclobdell 2:610196c7b41c 27
maclobdell 0:0fb17f0d39e7 28 // declare a melody
maclobdell 6:406f73a0eb49 29 int melody[] = {
maclobdell 6:406f73a0eb49 30 NOTE_C4, NOTE_D4, NOTE_DS4, NOTE_C4, NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4,
maclobdell 6:406f73a0eb49 31 NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4,
maclobdell 6:406f73a0eb49 32 NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4
maclobdell 0:0fb17f0d39e7 33 };
maclobdell 2:610196c7b41c 34
maclobdell 0:0fb17f0d39e7 35 // note durations: 4 = quarter note, 8 = eighth note, etc.:
maclobdell 6:406f73a0eb49 36 // the rapid succession of 16th notes produces a twill effect
maclobdell 0:0fb17f0d39e7 37 int duration[] = {
maclobdell 6:406f73a0eb49 38 2, 8, 4, 4, 16, 16, 16, 16,
maclobdell 6:406f73a0eb49 39 16, 16, 16, 16, 16, 16, 16,
maclobdell 6:406f73a0eb49 40 16, 16, 16, 16, 16
maclobdell 2:610196c7b41c 41 };
maclobdell 6:406f73a0eb49 42
maclobdell 0:0fb17f0d39e7 43
maclobdell 0:0fb17f0d39e7 44 // melody & duration are on the heap, need to get them on the stack
maclobdell 0:0fb17f0d39e7 45 int *m = new int[sizeof(melody) / sizeof(int)];
maclobdell 0:0fb17f0d39e7 46 memcpy(m, melody, sizeof(melody));
maclobdell 0:0fb17f0d39e7 47 int *d = new int[sizeof(duration) / sizeof(int)];
maclobdell 0:0fb17f0d39e7 48 memcpy(d, duration, sizeof(duration));
maclobdell 0:0fb17f0d39e7 49
maclobdell 0:0fb17f0d39e7 50 if (sizeof(melody) != sizeof(duration)) {
maclobdell 0:0fb17f0d39e7 51 printf("Melody and duration do not have same number of elements! Aborting!\r\n");
maclobdell 2:610196c7b41c 52 return 1;
maclobdell 0:0fb17f0d39e7 53 }
maclobdell 0:0fb17f0d39e7 54
maclobdell 0:0fb17f0d39e7 55 play_song(sizeof(melody) / sizeof(int), m, d);
maclobdell 2:610196c7b41c 56
maclobdell 2:610196c7b41c 57 return 0;
maclobdell 0:0fb17f0d39e7 58 }