Star_Wars_The_Imperial_March Piezo Buzzer Song
Embed:
(wiki syntax)
Show/hide line numbers
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[A][1], 0, nt[A][1], 0, nt[A][1], 0, 00024 nt[F][1], 0, nt[C][2], 0, nt[A][1], 0, 00025 nt[F][1], 0, nt[C][2], 0, nt[A][1], 0, 00026 nt[E][2], 0, nt[E][2], 0, nt[E][2], 0, 00027 nt[F][2], 0, nt[C][2], 0, nt[Gs][1], 0, 00028 nt[F][1], 0, nt[C][2], 0, nt[A][1], 0, 00029 nt[A][2], 0, nt[A][1], 0, nt[A][1], 0, 00030 nt[A][2], 0, nt[Gs][2], 0, nt[G][2], 0, 00031 nt[Fs][2], 0, nt[F][2], 0, nt[Fs][2], 0, 00032 nt[Bb][1], 0, nt[Eb][2], 0, nt[D][2], 0, 00033 nt[Cs][2], 0, nt[C][2], 0, nt[B][1], 0, 00034 nt[C][2], 0, nt[F][1], 0, nt[Gs][1], 0, 00035 nt[F][1], 0, nt[Gs][1], 0, nt[C][2], 0, 00036 nt[A][1], 0, nt[C][2], 0, nt[E][2], 0, 00037 nt[A][2], 0, nt[A][1], 0, nt[A][1], 0, 00038 nt[A][2], 0, nt[Gs][2], 0, nt[G][2], 0, 00039 nt[Fs][2], 0, nt[F][2], 0, nt[Fs][2], 0, 00040 nt[Bb][1], 0, nt[Eb][2], 0, nt[D][2], 0, 00041 nt[Cs][2], 0,}; 00042 float beatCiciban[] = {1, 0.5, 1, 0.5, 1, 0.5, 00043 0.5, 0.5, 0.5, 0.5, 1, 0.5, 00044 0.5, 0.5, 0.5, 0.5, 2, 0.5, 00045 1, 0.5, 1, 0.5, 1, 0.5, 00046 0.5, 0.5, 0.5, 0.5, 1, 0.5, 00047 0.5, 0.5, 0.5, 0.5, 2, 0.5, 00048 1, 0.5, 0.5, 0.5, 0.5, 0.5, 00049 1, 0.5, 0.5, 0.5, 0.5, 0.5, 00050 0.25, 0.5, 0.25, 0.5, 0.25, 0.5, 00051 0.25, 0.5, 1, 0.5, 0.5, 0.5, 00052 0.5, 0.5, 0.25, 0.5, 0.25, 0.5, 00053 0.25, 0.5, 0.25, 0.5, 1, 0.5, 00054 0.5, 0.5, 0.5, 0.5, 1, 0.5, 00055 0.5, 0.5, 0.5, 0.5, 2, 0.5, 00056 1, 0.5, 0.5, 0.5, 0.5, 0.5, 00057 1, 0.5, 0.5, 0.5, 0.5, 0.5, 00058 0.25, 0.5, 0.25, 0.5, 0.25, 0.5, 00059 0.25, 0.5, 1, 0.5, 0.5, 0.5, 00060 0.5, 0.5}; 00061 00062 for (int i = 0; i < 109; i++) { 00063 speaker.period(1 / (4*notesCiciban[i]) ); 00064 speaker = 0.75; 00065 wait(speed * beatCiciban[i]); 00066 } 00067 if (joystickCenter) // press joystick center to stop playing 00068 break; 00069 } 00070 }
Generated on Tue Jul 19 2022 18:15:39 by
1.7.2