Demo for new class to play a note on a speaker using a PWM output See http://mbed.org/users/4180_1/notebook/using-a-speaker-for-audio-output/

Dependencies:   mbed

Revision:
1:2e6ea42675c7
Parent:
0:b2fdf3770282
diff -r b2fdf3770282 -r 2e6ea42675c7 main.cpp
--- a/main.cpp	Sun Jan 20 03:02:37 2013 +0000
+++ b/main.cpp	Tue Oct 21 03:00:15 2014 +0000
@@ -1,19 +1,32 @@
 #include "mbed.h"
-#include "Speaker.h"
+#include "SongPlayer.h"
 
-// Speaker test program - euro police style siren now using new Speaker class method
+// Song test program - plays a song using PWM and timer interrupts
 // for documentation see http://mbed.org/users/4180_1/notebook/using-a-speaker-for-audio-output/
-// can also be used to play a song, if you have the notes and durations
+// can be used to play a song, if you have the notes and durations
 // for musical note frequencies see http://en.wikipedia.org/wiki/Piano_key_frequencies
 
+//Set up notes and durations for sample song to play
+// A 0.0 duration note at end terminates song play
+float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
+                 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0
+                };
+float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
+                     0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0
+                    };
+
+DigitalOut led1(LED1);
 int main()
 {
-// setup instance of new Speaker class, mySpeaker using pin 21
+// setup instance of new SongPlayer class, mySpeaker using pin 26
 // the pin must be a PWM output pin
-    Speaker mySpeaker(p21);
-    // loops forever playing two notes on speaker
+    SongPlayer mySpeaker(p26);
+// Start song and return once playing starts
+    mySpeaker.PlaySong(note,duration);
+    // loops forever while song continues to play to end using interrupts
     while(1) {
-        mySpeaker.PlayNote(969.0,0.5,0.5);
-        mySpeaker.PlayNote(800.0,0.5,0.5);
+        led1 = !led1;
+        wait(.1);
     }
 }
+