play songs

Dependencies:   mbed

Committer:
maclobdell
Date:
Mon May 09 19:52:47 2016 +0000
Revision:
0:0fb17f0d39e7
Child:
1:8922cc02aec7
initial version - modified from https://github.com/janjongboom/sxsw

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 0:0fb17f0d39e7 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 0:0fb17f0d39e7 6 static int BPM = 120;
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 0:0fb17f0d39e7 15 buzzer.period_us(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 0:0fb17f0d39e7 20 // YOUR CODE HERE
maclobdell 0:0fb17f0d39e7 21 }
maclobdell 0:0fb17f0d39e7 22
maclobdell 0:0fb17f0d39e7 23 // this code runs when the microcontroller starts up
maclobdell 0:0fb17f0d39e7 24 void main() {
maclobdell 0:0fb17f0d39e7 25 // declare a melody
maclobdell 0:0fb17f0d39e7 26 int melody[] = {
maclobdell 0:0fb17f0d39e7 27 NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4,
maclobdell 0:0fb17f0d39e7 28 NOTE_D4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4,
maclobdell 0:0fb17f0d39e7 29 NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4,
maclobdell 0:0fb17f0d39e7 30 NOTE_C4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_C4
maclobdell 0:0fb17f0d39e7 31 };
maclobdell 0:0fb17f0d39e7 32
maclobdell 0:0fb17f0d39e7 33 // note durations: 4 = quarter note, 8 = eighth note, etc.:
maclobdell 0:0fb17f0d39e7 34 int duration[] = {
maclobdell 0:0fb17f0d39e7 35 4, 4, 4, 4, 4, 4, 2,
maclobdell 0:0fb17f0d39e7 36 4, 4, 2, 4, 4, 2,
maclobdell 0:0fb17f0d39e7 37 4, 4, 4, 4, 4, 4, 4,
maclobdell 0:0fb17f0d39e7 38 4, 4, 4, 4, 4, 2
maclobdell 0:0fb17f0d39e7 39 };
maclobdell 0:0fb17f0d39e7 40
maclobdell 0:0fb17f0d39e7 41 // melody & duration are on the heap, need to get them on the stack
maclobdell 0:0fb17f0d39e7 42 int *m = new int[sizeof(melody) / sizeof(int)];
maclobdell 0:0fb17f0d39e7 43 memcpy(m, melody, sizeof(melody));
maclobdell 0:0fb17f0d39e7 44 int *d = new int[sizeof(duration) / sizeof(int)];
maclobdell 0:0fb17f0d39e7 45 memcpy(d, duration, sizeof(duration));
maclobdell 0:0fb17f0d39e7 46
maclobdell 0:0fb17f0d39e7 47 if (sizeof(melody) != sizeof(duration)) {
maclobdell 0:0fb17f0d39e7 48 printf("Melody and duration do not have same number of elements! Aborting!\r\n");
maclobdell 0:0fb17f0d39e7 49 return;
maclobdell 0:0fb17f0d39e7 50 }
maclobdell 0:0fb17f0d39e7 51
maclobdell 0:0fb17f0d39e7 52 play_song(sizeof(melody) / sizeof(int), m, d);
maclobdell 0:0fb17f0d39e7 53 }