Star_Wars_The_Imperial_March_Piezo_Buzzer_with_Ticker_and_Interrupts

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 InterruptIn joystickCenter(p14);
00007 Ticker speedster;
00008 float speed = 0;
00009 bool zastavica = false;
00010 
00011 void setSpeed(){
00012     speed = 0.05 + pot1; // minimum wait is 50 ms
00013 }
00014 
00015 void zaustaviPjesmu(){
00016     zastavica = true;
00017 }
00018 
00019 int main() {
00020     speedster.attach_us(&setSpeed, 1000);
00021     joystickCenter.rise(&zaustaviPjesmu);
00022     // note frequencies in Hz
00023     // rows: C C# D Eb E F F# G G# A Bb B
00024     //columns: 0 1 2 3 4 5 6 7 8
00025 
00026     enum noteNames {C, Cs, D, Eb, E, F, Fs, G, Gs, A, Bb, B};
00027     float nt[12][9] = { {16.35}, {17.32}, {18.35}, {19.45}, {20.60}, {21.83},
00028                         {23.12}, {24.5}, {25.96}, {27.5}, {29.14}, {30.87} };
00029     for (int i = 0; i < 12; i++) 
00030         for (int j = 1; j < 9; j++) 
00031             nt[i][j] = nt[i][j-1] * 2;
00032     
00033     while(1) {
00034         //int scale = pot2 * 8;
00035         float notesCiciban[] = {nt[A][1], 0, nt[A][1], 0, nt[A][1], 0,
00036                                 nt[F][1], 0, nt[C][2], 0, nt[A][1], 0,
00037                                 nt[F][1], 0, nt[C][2], 0, nt[A][1], 0, 
00038                                 nt[E][2], 0, nt[E][2], 0, nt[E][2], 0,
00039                                 nt[F][2], 0, nt[C][2], 0, nt[Gs][1], 0, 
00040                                 nt[F][1], 0, nt[C][2], 0, nt[A][1], 0,
00041                                 nt[A][2], 0, nt[A][1], 0, nt[A][1], 0,
00042                                 nt[A][2], 0, nt[Gs][2], 0, nt[G][2], 0,
00043                                 nt[Fs][2], 0, nt[F][2], 0, nt[Fs][2], 0,
00044                                 nt[Bb][1], 0, nt[Eb][2], 0, nt[D][2], 0,
00045                                 nt[Cs][2], 0, nt[C][2], 0, nt[B][1], 0,
00046                                 nt[C][2], 0, nt[F][1], 0, nt[Gs][1], 0,
00047                                 nt[F][1], 0, nt[Gs][1], 0, nt[C][2], 0,
00048                                 nt[A][1], 0, nt[C][2], 0, nt[E][2], 0,
00049                                 nt[A][2], 0, nt[A][1], 0, nt[A][1], 0,
00050                                 nt[A][2], 0, nt[Gs][2], 0, nt[G][2], 0,
00051                                 nt[Fs][2], 0, nt[F][2], 0, nt[Fs][2], 0,
00052                                 nt[Bb][1], 0, nt[Eb][2], 0, nt[D][2], 0,
00053                                 nt[Cs][2], 0,};
00054         float beatCiciban[] =  {1, 0.5, 1, 0.5, 1, 0.5,
00055                                 0.5, 0.5, 0.5, 0.5, 1, 0.5,
00056                                 0.5, 0.5, 0.5, 0.5, 2, 0.5, 
00057                                 1, 0.5, 1, 0.5, 1, 0.5, 
00058                                 0.5, 0.5, 0.5, 0.5, 1, 0.5,
00059                                 0.5, 0.5, 0.5, 0.5, 2, 0.5,
00060                                 1, 0.5, 0.5, 0.5, 0.5, 0.5,
00061                                 1, 0.5, 0.5, 0.5, 0.5, 0.5,
00062                                 0.25, 0.5, 0.25, 0.5, 0.25, 0.5,
00063                                 0.25, 0.5, 1, 0.5, 0.5, 0.5,
00064                                 0.5, 0.5, 0.25, 0.5, 0.25, 0.5,
00065                                 0.25, 0.5, 0.25, 0.5, 1, 0.5,
00066                                 0.5, 0.5, 0.5, 0.5, 1, 0.5,
00067                                 0.5, 0.5, 0.5, 0.5, 2, 0.5,
00068                                 1, 0.5, 0.5, 0.5, 0.5, 0.5,
00069                                 1, 0.5, 0.5, 0.5, 0.5, 0.5,
00070                                 0.25, 0.5, 0.25, 0.5, 0.25, 0.5,
00071                                 0.25, 0.5, 1, 0.5, 0.5, 0.5,
00072                                 0.5, 0.5};
00073     
00074         for (int i = 0; i < 109; i++) {
00075             speaker.period(1 / (4*notesCiciban[i]) );
00076             speaker = 0.75;
00077             wait(speed * beatCiciban[i]);
00078         }
00079         if(zastavica){
00080               speaker = 0;
00081               break;
00082         }
00083     }
00084 }