A program for playing Ciciban song. All notes are included, so any song can be easily added if you know its notes.
Dependencies: mbed
main.cpp@0:c84a78a490d0, 2014-12-18 (annotated)
- Committer:
- tbjazic
- Date:
- Thu Dec 18 17:01:08 2014 +0000
- Revision:
- 0:c84a78a490d0
- Child:
- 1:a92e43e7429e
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tbjazic | 0:c84a78a490d0 | 1 | #include "mbed.h" |
tbjazic | 0:c84a78a490d0 | 2 | |
tbjazic | 0:c84a78a490d0 | 3 | PwmOut speaker(p26); |
tbjazic | 0:c84a78a490d0 | 4 | AnalogIn pot1(p19); |
tbjazic | 0:c84a78a490d0 | 5 | AnalogIn pot2(p20); |
tbjazic | 0:c84a78a490d0 | 6 | DigitalIn joystickCenter(p14); |
tbjazic | 0:c84a78a490d0 | 7 | |
tbjazic | 0:c84a78a490d0 | 8 | int main() { |
tbjazic | 0:c84a78a490d0 | 9 | /* note frequencies in Hz |
tbjazic | 0:c84a78a490d0 | 10 | * rows: C C# D Eb E F F# G G# A Bb B |
tbjazic | 0:c84a78a490d0 | 11 | * columns: 0 1 2 3 4 5 6 7 8 |
tbjazic | 0:c84a78a490d0 | 12 | */ |
tbjazic | 0:c84a78a490d0 | 13 | enum noteNames {C, Cs, D, Eb, E, F, Fs, G, Gs, A, Bb, B}; |
tbjazic | 0:c84a78a490d0 | 14 | float nt[12][9] = { {16.35}, {17.32}, {18.35}, {19.45}, {20.60}, {21.83}, |
tbjazic | 0:c84a78a490d0 | 15 | {23.12}, {24.5}, {25.96}, {27.5}, {29.14}, {30.87} }; |
tbjazic | 0:c84a78a490d0 | 16 | for (int i = 0; i < 12; i++) |
tbjazic | 0:c84a78a490d0 | 17 | for (int j = 1; j < 9; j++) |
tbjazic | 0:c84a78a490d0 | 18 | nt[i][j] = nt[i][j-1] * 2; |
tbjazic | 0:c84a78a490d0 | 19 | |
tbjazic | 0:c84a78a490d0 | 20 | while(1) { |
tbjazic | 0:c84a78a490d0 | 21 | int scale = pot2 * 8; |
tbjazic | 0:c84a78a490d0 | 22 | float speed = 0.05 + pot1; // minimum wait is 50 ms |
tbjazic | 0:c84a78a490d0 | 23 | float notesCiciban[] = {nt[G][scale], 0, nt[G][scale], 0, nt[E][scale], 0, |
tbjazic | 0:c84a78a490d0 | 24 | nt[G][scale], 0, nt[G][scale], 0, nt[E][scale], 0, |
tbjazic | 0:c84a78a490d0 | 25 | nt[G][scale], 0, nt[G][scale], 0, nt[E][scale], 0, nt[E][scale], 0, |
tbjazic | 0:c84a78a490d0 | 26 | nt[G][scale], 0, nt[E][scale], 0, nt[C][scale], 0}; |
tbjazic | 0:c84a78a490d0 | 27 | float beatCiciban[] = {1, 0.5, 1, 0.5, 2, 0.5, |
tbjazic | 0:c84a78a490d0 | 28 | 1, 0.5, 1, 0.5, 2, 0.5, |
tbjazic | 0:c84a78a490d0 | 29 | 1, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, |
tbjazic | 0:c84a78a490d0 | 30 | 1, 0.5, 1, 0.5, 2, 5}; |
tbjazic | 0:c84a78a490d0 | 31 | |
tbjazic | 0:c84a78a490d0 | 32 | for (int i = 0; i < 26; i++) { |
tbjazic | 0:c84a78a490d0 | 33 | speaker.period(1 / notesCiciban[i]); |
tbjazic | 0:c84a78a490d0 | 34 | speaker = 0.5; |
tbjazic | 0:c84a78a490d0 | 35 | wait(speed * beatCiciban[i]); |
tbjazic | 0:c84a78a490d0 | 36 | } |
tbjazic | 0:c84a78a490d0 | 37 | if (joystickCenter) // press joystick center to stop playing |
tbjazic | 0:c84a78a490d0 | 38 | break; |
tbjazic | 0:c84a78a490d0 | 39 | } |
tbjazic | 0:c84a78a490d0 | 40 | } |