Testing PWM with a speaker

Dependencies:   mbed

Revision:
0:b82c05c12d48
Child:
1:aa7cd19c6a4f
diff -r 000000000000 -r b82c05c12d48 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Feb 20 14:09:54 2014 +0000
@@ -0,0 +1,29 @@
+#include "mbed.h"
+
+DigitalOut  my_led(LED1);
+InterruptIn my_button(USER_BUTTON);
+PwmOut      my_pwm(PB_3);
+
+void pressed() {
+    if (my_pwm.read() == 0.25) {
+        my_pwm.write(0.75);
+    }
+    else {
+        my_pwm.write(0.25);
+    }
+}
+
+int main()
+{
+    // Set PWM
+    my_pwm.period_ms(10);
+    my_pwm.write(0.5);
+    
+    // Set button
+    my_button.fall(&pressed);
+    
+    while (1) {
+        my_led = !my_led;
+        wait(0.5); // 500 ms
+    }
+}