Simple audio out using mbed templates

Dependencies:   mbed

Committer:
mm73
Date:
Tue Jun 10 12:14:05 2014 +0000
Revision:
0:05e1cec99ce1
Options Girls 2014

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mm73 0:05e1cec99ce1 1 #include "mbed.h"
mm73 0:05e1cec99ce1 2
mm73 0:05e1cec99ce1 3 #define C3 131
mm73 0:05e1cec99ce1 4 #define D3 147
mm73 0:05e1cec99ce1 5 #define E3 165
mm73 0:05e1cec99ce1 6 #define F3 175
mm73 0:05e1cec99ce1 7 #define G3 196
mm73 0:05e1cec99ce1 8 #define A3 220
mm73 0:05e1cec99ce1 9 #define B3 247
mm73 0:05e1cec99ce1 10 #define C4 262
mm73 0:05e1cec99ce1 11 #define D4 294
mm73 0:05e1cec99ce1 12 #define E4 330
mm73 0:05e1cec99ce1 13 #define F4 349
mm73 0:05e1cec99ce1 14 #define G4 392
mm73 0:05e1cec99ce1 15 #define A4 440
mm73 0:05e1cec99ce1 16 #define B4 494
mm73 0:05e1cec99ce1 17 #define C5 523
mm73 0:05e1cec99ce1 18 #define A5 880
mm73 0:05e1cec99ce1 19 #define B5 988
mm73 0:05e1cec99ce1 20 #define Pause 1
mm73 0:05e1cec99ce1 21
mm73 0:05e1cec99ce1 22 Ticker tick;
mm73 0:05e1cec99ce1 23 DigitalOut led1(LED_RED);
mm73 0:05e1cec99ce1 24 DigitalOut led2(LED_GREEN);
mm73 0:05e1cec99ce1 25 DigitalOut gpo(D10);
mm73 0:05e1cec99ce1 26
mm73 0:05e1cec99ce1 27 void flip() {
mm73 0:05e1cec99ce1 28 led2 = !led2;
mm73 0:05e1cec99ce1 29 gpo = !gpo;
mm73 0:05e1cec99ce1 30 }
mm73 0:05e1cec99ce1 31
mm73 0:05e1cec99ce1 32 int main() {
mm73 0:05e1cec99ce1 33 int Notes_Hz[] = {Pause, G4, F4, G4, Pause, D3, B5, A4, Pause, G4, E3, E4, E4, E4, F4, G4,Pause,
mm73 0:05e1cec99ce1 34 G4, F4, G4, Pause, D3, B5, A4, Pause, G4, E3, E4, E4, E4, F4, G4, Pause};
mm73 0:05e1cec99ce1 35 int i;
mm73 0:05e1cec99ce1 36 int bpm = 120;
mm73 0:05e1cec99ce1 37 //tick.attach(&flip, 0.7); // setup ticker to call flip led2 after 0.7 seconds
mm73 0:05e1cec99ce1 38
mm73 0:05e1cec99ce1 39 while (1) {
mm73 0:05e1cec99ce1 40 i = 0;
mm73 0:05e1cec99ce1 41 while (Notes_Hz[i]) {
mm73 0:05e1cec99ce1 42 led1 = !led1;
mm73 0:05e1cec99ce1 43 tick.attach(&flip, 0.5/Notes_Hz[i]);
mm73 0:05e1cec99ce1 44 wait (50.0/bpm); // bpm = beats per minute
mm73 0:05e1cec99ce1 45 tick.attach(&flip, 1);
mm73 0:05e1cec99ce1 46 wait (10.0/bpm); //
mm73 0:05e1cec99ce1 47 i++;
mm73 0:05e1cec99ce1 48 }
mm73 0:05e1cec99ce1 49 }
mm73 0:05e1cec99ce1 50 }