TVZ Mechatronics Team
/
Star_Wars_The_imperial_March_2
Star_Wars_The_Imperial_March_Piezo_Buzzer_with_Ticker_and_Interrupts
main.cpp@0:8544e0d29036, 2019-11-18 (annotated)
- Committer:
- dfraj
- Date:
- Mon Nov 18 17:08:14 2019 +0000
- Revision:
- 0:8544e0d29036
Star_Wars_The_Imperial_March_Piezo_Buzzer_with_Ticker_and_Interrupts
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dfraj | 0:8544e0d29036 | 1 | #include "mbed.h" |
dfraj | 0:8544e0d29036 | 2 | |
dfraj | 0:8544e0d29036 | 3 | PwmOut speaker(p26); |
dfraj | 0:8544e0d29036 | 4 | AnalogIn pot1(p19); |
dfraj | 0:8544e0d29036 | 5 | AnalogIn pot2(p20); |
dfraj | 0:8544e0d29036 | 6 | InterruptIn joystickCenter(p14); |
dfraj | 0:8544e0d29036 | 7 | Ticker speedster; |
dfraj | 0:8544e0d29036 | 8 | float speed = 0; |
dfraj | 0:8544e0d29036 | 9 | bool zastavica = false; |
dfraj | 0:8544e0d29036 | 10 | |
dfraj | 0:8544e0d29036 | 11 | void setSpeed(){ |
dfraj | 0:8544e0d29036 | 12 | speed = 0.05 + pot1; // minimum wait is 50 ms |
dfraj | 0:8544e0d29036 | 13 | } |
dfraj | 0:8544e0d29036 | 14 | |
dfraj | 0:8544e0d29036 | 15 | void zaustaviPjesmu(){ |
dfraj | 0:8544e0d29036 | 16 | zastavica = true; |
dfraj | 0:8544e0d29036 | 17 | } |
dfraj | 0:8544e0d29036 | 18 | |
dfraj | 0:8544e0d29036 | 19 | int main() { |
dfraj | 0:8544e0d29036 | 20 | speedster.attach_us(&setSpeed, 1000); |
dfraj | 0:8544e0d29036 | 21 | joystickCenter.rise(&zaustaviPjesmu); |
dfraj | 0:8544e0d29036 | 22 | // note frequencies in Hz |
dfraj | 0:8544e0d29036 | 23 | // rows: C C# D Eb E F F# G G# A Bb B |
dfraj | 0:8544e0d29036 | 24 | //columns: 0 1 2 3 4 5 6 7 8 |
dfraj | 0:8544e0d29036 | 25 | |
dfraj | 0:8544e0d29036 | 26 | enum noteNames {C, Cs, D, Eb, E, F, Fs, G, Gs, A, Bb, B}; |
dfraj | 0:8544e0d29036 | 27 | float nt[12][9] = { {16.35}, {17.32}, {18.35}, {19.45}, {20.60}, {21.83}, |
dfraj | 0:8544e0d29036 | 28 | {23.12}, {24.5}, {25.96}, {27.5}, {29.14}, {30.87} }; |
dfraj | 0:8544e0d29036 | 29 | for (int i = 0; i < 12; i++) |
dfraj | 0:8544e0d29036 | 30 | for (int j = 1; j < 9; j++) |
dfraj | 0:8544e0d29036 | 31 | nt[i][j] = nt[i][j-1] * 2; |
dfraj | 0:8544e0d29036 | 32 | |
dfraj | 0:8544e0d29036 | 33 | while(1) { |
dfraj | 0:8544e0d29036 | 34 | //int scale = pot2 * 8; |
dfraj | 0:8544e0d29036 | 35 | float notesCiciban[] = {nt[A][1], 0, nt[A][1], 0, nt[A][1], 0, |
dfraj | 0:8544e0d29036 | 36 | nt[F][1], 0, nt[C][2], 0, nt[A][1], 0, |
dfraj | 0:8544e0d29036 | 37 | nt[F][1], 0, nt[C][2], 0, nt[A][1], 0, |
dfraj | 0:8544e0d29036 | 38 | nt[E][2], 0, nt[E][2], 0, nt[E][2], 0, |
dfraj | 0:8544e0d29036 | 39 | nt[F][2], 0, nt[C][2], 0, nt[Gs][1], 0, |
dfraj | 0:8544e0d29036 | 40 | nt[F][1], 0, nt[C][2], 0, nt[A][1], 0, |
dfraj | 0:8544e0d29036 | 41 | nt[A][2], 0, nt[A][1], 0, nt[A][1], 0, |
dfraj | 0:8544e0d29036 | 42 | nt[A][2], 0, nt[Gs][2], 0, nt[G][2], 0, |
dfraj | 0:8544e0d29036 | 43 | nt[Fs][2], 0, nt[F][2], 0, nt[Fs][2], 0, |
dfraj | 0:8544e0d29036 | 44 | nt[Bb][1], 0, nt[Eb][2], 0, nt[D][2], 0, |
dfraj | 0:8544e0d29036 | 45 | nt[Cs][2], 0, nt[C][2], 0, nt[B][1], 0, |
dfraj | 0:8544e0d29036 | 46 | nt[C][2], 0, nt[F][1], 0, nt[Gs][1], 0, |
dfraj | 0:8544e0d29036 | 47 | nt[F][1], 0, nt[Gs][1], 0, nt[C][2], 0, |
dfraj | 0:8544e0d29036 | 48 | nt[A][1], 0, nt[C][2], 0, nt[E][2], 0, |
dfraj | 0:8544e0d29036 | 49 | nt[A][2], 0, nt[A][1], 0, nt[A][1], 0, |
dfraj | 0:8544e0d29036 | 50 | nt[A][2], 0, nt[Gs][2], 0, nt[G][2], 0, |
dfraj | 0:8544e0d29036 | 51 | nt[Fs][2], 0, nt[F][2], 0, nt[Fs][2], 0, |
dfraj | 0:8544e0d29036 | 52 | nt[Bb][1], 0, nt[Eb][2], 0, nt[D][2], 0, |
dfraj | 0:8544e0d29036 | 53 | nt[Cs][2], 0,}; |
dfraj | 0:8544e0d29036 | 54 | float beatCiciban[] = {1, 0.5, 1, 0.5, 1, 0.5, |
dfraj | 0:8544e0d29036 | 55 | 0.5, 0.5, 0.5, 0.5, 1, 0.5, |
dfraj | 0:8544e0d29036 | 56 | 0.5, 0.5, 0.5, 0.5, 2, 0.5, |
dfraj | 0:8544e0d29036 | 57 | 1, 0.5, 1, 0.5, 1, 0.5, |
dfraj | 0:8544e0d29036 | 58 | 0.5, 0.5, 0.5, 0.5, 1, 0.5, |
dfraj | 0:8544e0d29036 | 59 | 0.5, 0.5, 0.5, 0.5, 2, 0.5, |
dfraj | 0:8544e0d29036 | 60 | 1, 0.5, 0.5, 0.5, 0.5, 0.5, |
dfraj | 0:8544e0d29036 | 61 | 1, 0.5, 0.5, 0.5, 0.5, 0.5, |
dfraj | 0:8544e0d29036 | 62 | 0.25, 0.5, 0.25, 0.5, 0.25, 0.5, |
dfraj | 0:8544e0d29036 | 63 | 0.25, 0.5, 1, 0.5, 0.5, 0.5, |
dfraj | 0:8544e0d29036 | 64 | 0.5, 0.5, 0.25, 0.5, 0.25, 0.5, |
dfraj | 0:8544e0d29036 | 65 | 0.25, 0.5, 0.25, 0.5, 1, 0.5, |
dfraj | 0:8544e0d29036 | 66 | 0.5, 0.5, 0.5, 0.5, 1, 0.5, |
dfraj | 0:8544e0d29036 | 67 | 0.5, 0.5, 0.5, 0.5, 2, 0.5, |
dfraj | 0:8544e0d29036 | 68 | 1, 0.5, 0.5, 0.5, 0.5, 0.5, |
dfraj | 0:8544e0d29036 | 69 | 1, 0.5, 0.5, 0.5, 0.5, 0.5, |
dfraj | 0:8544e0d29036 | 70 | 0.25, 0.5, 0.25, 0.5, 0.25, 0.5, |
dfraj | 0:8544e0d29036 | 71 | 0.25, 0.5, 1, 0.5, 0.5, 0.5, |
dfraj | 0:8544e0d29036 | 72 | 0.5, 0.5}; |
dfraj | 0:8544e0d29036 | 73 | |
dfraj | 0:8544e0d29036 | 74 | for (int i = 0; i < 109; i++) { |
dfraj | 0:8544e0d29036 | 75 | speaker.period(1 / (4*notesCiciban[i]) ); |
dfraj | 0:8544e0d29036 | 76 | speaker = 0.75; |
dfraj | 0:8544e0d29036 | 77 | wait(speed * beatCiciban[i]); |
dfraj | 0:8544e0d29036 | 78 | } |
dfraj | 0:8544e0d29036 | 79 | if(zastavica){ |
dfraj | 0:8544e0d29036 | 80 | speaker = 0; |
dfraj | 0:8544e0d29036 | 81 | break; |
dfraj | 0:8544e0d29036 | 82 | } |
dfraj | 0:8544e0d29036 | 83 | } |
dfraj | 0:8544e0d29036 | 84 | } |