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

Files at this revision

API Documentation at this revision

Comitter:
4180_1
Date:
Sun Jan 20 03:02:37 2013 +0000
Child:
1:2e6ea42675c7
Child:
2:68eee0fb2026
Commit message:
ver 1.0

Changed in this revision

Speaker.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Speaker.h	Sun Jan 20 03:02:37 2013 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+// new class to play a note on Speaker based on PwmOut class
+class Speaker
+{
+public:
+    Speaker(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the Speaker Constructor
+    }
+// class method to play a note based on PwmOut class
+    void PlayNote(float frequency, float duration, float volume) {
+        _pin.period(1.0/frequency);
+        _pin = volume/2.0;
+        wait(duration);
+        _pin = 0.0;
+    }
+
+private:
+    PwmOut _pin;
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Jan 20 03:02:37 2013 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+#include "Speaker.h"
+
+// Speaker test program - euro police style siren now using new Speaker class method
+// 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
+// for musical note frequencies see http://en.wikipedia.org/wiki/Piano_key_frequencies
+
+int main()
+{
+// setup instance of new Speaker class, mySpeaker using pin 21
+// the pin must be a PWM output pin
+    Speaker mySpeaker(p21);
+    // loops forever playing two notes on speaker
+    while(1) {
+        mySpeaker.PlayNote(969.0,0.5,0.5);
+        mySpeaker.PlayNote(800.0,0.5,0.5);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Jan 20 03:02:37 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/0480438fc29c
\ No newline at end of file