mbed Starter Kit / mbed Starter Kit Demo Programs
Revision:
0:6a73d3dc037e
diff -r 000000000000 -r 6a73d3dc037e pwm_song/main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pwm_song/main.cpp	Mon Jul 28 20:29:28 2014 +0000
@@ -0,0 +1,67 @@
+// Plays a familiar melody using PWM to the headphones. To find the frequencies
+// of notes, see http://en.wikipedia.org/wiki/Piano_key_frequencies
+// Based on the "speaker_demo_PWM" program by Jim Hamblen
+
+#include "mbed.h"
+
+#define VOLUME 0.01
+#define BPM 100.0
+
+PwmOut pwm_pin(p21);
+
+// Plays a sound with the defined frequency, duration, and volume
+void playNote(float frequency, float duration, float volume) {
+    pwm_pin.period(1.0/frequency);
+    pwm_pin = volume/2.0;
+    wait(duration);
+    pwm_pin = 0.0;
+}
+
+int main()
+{
+    float beat_duration;
+
+    // Calculate duration of a quarter note from bpm
+    beat_duration = 60.0 / BPM;
+    
+    // Loop forever
+    while(1) {
+        
+        // First measure
+        playNote(391.995, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(391.995, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(391.995, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(311.127, (0.75 * beat_duration), VOLUME);
+        playNote(466.164, (0.25 * beat_duration), VOLUME);
+        
+        // Second measure
+        playNote(391.995, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(311.127, (0.75 * beat_duration), VOLUME);
+        playNote(466.164, (0.25 * beat_duration), VOLUME);
+        playNote(391.995, ((2 * beat_duration) - 0.1), VOLUME);
+        wait(0.1);
+        
+        // Third measure
+        playNote(587.330, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(587.330, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(587.330, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(622.254, (0.75 * beat_duration), VOLUME);
+        playNote(466.164, (0.25 * beat_duration), VOLUME);
+        
+        // Fourth measure
+        playNote(369.994, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(311.127, (0.75 * beat_duration), VOLUME);
+        playNote(466.164, (0.25 * beat_duration), VOLUME);
+        playNote(391.995, ((2 * beat_duration) - 0.1), VOLUME);
+        wait(0.1);
+    }
+}
+