Use a buzz (piezo) to produce tone

Dependencies:   mbed

Fork of Arch_Play_Music by Yihui Xiong

Committer:
yihui
Date:
Mon Apr 28 06:13:29 2014 +0000
Revision:
0:343592047b5a
Child:
1:0e53bbdcc678
initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:343592047b5a 1 #include "mbed.h"
yihui 0:343592047b5a 2 #include "pitches.h"
yihui 0:343592047b5a 3
yihui 0:343592047b5a 4 DigitalOut myled(LED1);
yihui 0:343592047b5a 5
yihui 0:343592047b5a 6 PwmOut buzzer(P1_14);
yihui 0:343592047b5a 7
yihui 0:343592047b5a 8 int frequency[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3, 1, NOTE_B3, NOTE_C4};
yihui 0:343592047b5a 9 int beat[] = {4, 8, 8, 4,4,4,4,4 };
yihui 0:343592047b5a 10
yihui 0:343592047b5a 11 int main() {
yihui 0:343592047b5a 12 for (int i = 0; i < (sizeof(frequency) / sizeof(int)); i++) {
yihui 0:343592047b5a 13 buzzer.period(1.0 / frequency[i]);
yihui 0:343592047b5a 14 buzzer.write(0.5);
yihui 0:343592047b5a 15 wait(1.0 / beat[i]);
yihui 0:343592047b5a 16 buzzer.write(0);
yihui 0:343592047b5a 17 wait(0.05);
yihui 0:343592047b5a 18 }
yihui 0:343592047b5a 19
yihui 0:343592047b5a 20 while(1) {
yihui 0:343592047b5a 21 myled = !myled;
yihui 0:343592047b5a 22 wait(1);
yihui 0:343592047b5a 23 }
yihui 0:343592047b5a 24 }