Program koji koristi PWM izlaz i piezo zvučnik za sviranje

Dependencies:   mbed

Fork of MAB_PWM_Speaker by TVZ Mechatronics Team

Revision:
0:c84a78a490d0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Dec 18 17:01:08 2014 +0000
@@ -0,0 +1,40 @@
+#include "mbed.h"
+
+PwmOut speaker(p26);
+AnalogIn pot1(p19);
+AnalogIn pot2(p20);
+DigitalIn joystickCenter(p14);
+
+int main() {
+    /* note frequencies in Hz
+    * rows: C C# D Eb E F F# G G# A Bb B
+    * columns: 0 1 2 3 4 5 6 7 8
+    */
+    enum noteNames {C, Cs, D, Eb, E, F, Fs, G, Gs, A, Bb, B};
+    float nt[12][9] = { {16.35}, {17.32}, {18.35}, {19.45}, {20.60}, {21.83},
+                        {23.12}, {24.5}, {25.96}, {27.5}, {29.14}, {30.87} };
+    for (int i = 0; i < 12; i++) 
+        for (int j = 1; j < 9; j++) 
+            nt[i][j] = nt[i][j-1] * 2;
+    
+    while(1) {
+        int scale = pot2 * 8;
+        float speed = 0.05 + pot1; // minimum wait is 50 ms
+        float notesCiciban[] = {nt[G][scale], 0, nt[G][scale], 0, nt[E][scale], 0,
+                                nt[G][scale], 0, nt[G][scale], 0, nt[E][scale], 0,
+                                nt[G][scale], 0, nt[G][scale], 0, nt[E][scale], 0, nt[E][scale], 0,
+                                nt[G][scale], 0, nt[E][scale], 0, nt[C][scale], 0};
+        float beatCiciban[] =  {1, 0.5, 1, 0.5, 2, 0.5,
+                                1, 0.5, 1, 0.5, 2, 0.5,
+                                1, 0.5, 1, 0.5, 1, 0.5, 1, 0.5,
+                                1, 0.5, 1, 0.5, 2, 5};
+    
+        for (int i = 0; i < 26; i++) {
+            speaker.period(1 / notesCiciban[i]);
+            speaker = 0.5;
+            wait(speed * beatCiciban[i]);
+        }
+        if (joystickCenter) // press joystick center to stop playing
+            break;
+    }
+}