Simple program playing a song using PWM on a speaker.
Dependencies: mbed
wurlitzer.cpp@0:b34f133646a8, 2015-10-02 (annotated)
- Committer:
- lvagasi
- Date:
- Fri Oct 02 21:36:39 2015 +0000
- Revision:
- 0:b34f133646a8
Simple program to show how PWM can be used to play a simple song on a speaker. Working.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lvagasi | 0:b34f133646a8 | 1 | #include "mbed.h" |
lvagasi | 0:b34f133646a8 | 2 | |
lvagasi | 0:b34f133646a8 | 3 | PwmOut speaker(PB_4); // Speaker |
lvagasi | 0:b34f133646a8 | 4 | DigitalOut myled(PA_10); |
lvagasi | 0:b34f133646a8 | 5 | |
lvagasi | 0:b34f133646a8 | 6 | const float scala[38] = { 0.0f, 108.0f, 112.5f, 121.5f, 129.6f, 135.0f, 144.0f, 150.0f, 162.0f, 172.8f, 180.0f, 194.4f, 202.5f, |
lvagasi | 0:b34f133646a8 | 7 | 216.0f, 225.0f, 243.0f, 259.2f, 270.0f, 288.0f, 300.0f, 324.0f, 345.6f, 360.0f, 388.8f, 405.0f, |
lvagasi | 0:b34f133646a8 | 8 | 432.0f, 450.0f, 486.0f, 518.4f, 540.0f, 576.0f, 600.0f, 648.0f, 691.2f, 720.0f, 777.6f, 810.0f, |
lvagasi | 0:b34f133646a8 | 9 | 864.0f }; |
lvagasi | 0:b34f133646a8 | 10 | |
lvagasi | 0:b34f133646a8 | 11 | const uint32_t song[54][2] = { {16, 4}, {18, 4}, {20, 4}, {18, 4}, {16, 2}, { 0, 2}, {16, 4}, {18, 4}, {20, 4}, {18, 4}, {16, 2}, { 0, 2}, {20, 4}, {20, 4}, {21, 4}, {21, 4}, |
lvagasi | 0:b34f133646a8 | 12 | {20, 4}, {20, 4}, {18, 2}, {20, 4}, {20, 4}, {21, 4}, {21, 4}, {20, 4}, {20, 4}, {18, 2}, {20, 4}, {18, 4}, {20, 4}, {21, 4}, {23, 4}, {21, 4}, |
lvagasi | 0:b34f133646a8 | 13 | {20, 4}, {18, 4}, {20, 4}, {18, 4}, {20, 4}, {21, 4}, {23, 4}, {21, 4}, {20, 4}, {18, 4}, {16, 4}, {18, 4}, {20, 4}, {18, 4}, {16, 2}, { 0, 2}, |
lvagasi | 0:b34f133646a8 | 14 | {16, 4}, {18, 4}, {20, 4}, {18, 4}, {16, 2}, { 0, 2} }; |
lvagasi | 0:b34f133646a8 | 15 | |
lvagasi | 0:b34f133646a8 | 16 | |
lvagasi | 0:b34f133646a8 | 17 | int main() { |
lvagasi | 0:b34f133646a8 | 18 | float tone; |
lvagasi | 0:b34f133646a8 | 19 | int i; |
lvagasi | 0:b34f133646a8 | 20 | |
lvagasi | 0:b34f133646a8 | 21 | myled = 1; |
lvagasi | 0:b34f133646a8 | 22 | |
lvagasi | 0:b34f133646a8 | 23 | for (i = 0; i < 54; i++) { |
lvagasi | 0:b34f133646a8 | 24 | if (song[i][0] > 0.0f) { |
lvagasi | 0:b34f133646a8 | 25 | tone = float(1.0f/scala[song[i][0]]); |
lvagasi | 0:b34f133646a8 | 26 | speaker.period(tone); |
lvagasi | 0:b34f133646a8 | 27 | speaker = 0.5; |
lvagasi | 0:b34f133646a8 | 28 | } else { |
lvagasi | 0:b34f133646a8 | 29 | speaker = 0; |
lvagasi | 0:b34f133646a8 | 30 | } |
lvagasi | 0:b34f133646a8 | 31 | wait(float(1.0f/song[i][1])); |
lvagasi | 0:b34f133646a8 | 32 | } |
lvagasi | 0:b34f133646a8 | 33 | while (1) { |
lvagasi | 0:b34f133646a8 | 34 | myled = 0; // LED is ON |
lvagasi | 0:b34f133646a8 | 35 | wait(0.2); // 200 ms |
lvagasi | 0:b34f133646a8 | 36 | myled = 1; // LED is OFF |
lvagasi | 0:b34f133646a8 | 37 | wait(1.0); // 1 sec |
lvagasi | 0:b34f133646a8 | 38 | } |
lvagasi | 0:b34f133646a8 | 39 | |
lvagasi | 0:b34f133646a8 | 40 | } |
lvagasi | 0:b34f133646a8 | 41 |