A program for playing Ciciban song. All notes are included, so any song can be easily added if you know its notes.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 PwmOut speaker(p26);
00004 AnalogIn pot1(p19);
00005 AnalogIn pot2(p20);
00006 DigitalIn joystickCenter(p14);
00007 
00008 int main() {
00009     /* note frequencies in Hz
00010     * rows: C C# D Eb E F F# G G# A Bb B
00011     * columns: 0 1 2 3 4 5 6 7 8
00012     */
00013     enum noteNames {C, Cs, D, Eb, E, F, Fs, G, Gs, A, Bb, B};
00014     float nt[12][9] = { {16.35}, {17.32}, {18.35}, {19.45}, {20.60}, {21.83},
00015                         {23.12}, {24.5}, {25.96}, {27.5}, {29.14}, {30.87} };
00016     for (int i = 0; i < 12; i++) 
00017         for (int j = 1; j < 9; j++) 
00018             nt[i][j] = nt[i][j-1] * 2;
00019     
00020     while(1) {
00021         int scale = pot2 * 8;
00022         float speed = 0.05 + pot1; // minimum wait is 50 ms
00023         float notesCiciban[] = {nt[G][scale], 0, nt[G][scale], 0, nt[E][scale], 0,
00024                                 nt[G][scale], 0, nt[G][scale], 0, nt[E][scale], 0,
00025                                 nt[G][scale], 0, nt[G][scale], 0, nt[E][scale], 0, nt[E][scale], 0,
00026                                 nt[G][scale], 0, nt[E][scale], 0, nt[C][scale], 0};
00027         float beatCiciban[] =  {1, 0.5, 1, 0.5, 2, 0.5,
00028                                 1, 0.5, 1, 0.5, 2, 0.5,
00029                                 1, 0.5, 1, 0.5, 1, 0.5, 1, 0.5,
00030                                 1, 0.5, 1, 0.5, 2, 5};
00031     
00032         for (int i = 0; i < 26; i++) {
00033             speaker.period(1 / (2*notesCiciban[i]) );
00034             speaker = 0.5;
00035             wait(speed * beatCiciban[i]);
00036         }
00037         if (joystickCenter) // press joystick center to stop playing
00038             break;
00039     }
00040 }