Plays a song using PWM and timer interrupts. Returns once song starts.

Dependencies:   mbed

Fork of speaker_demo_PWM by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SongPlayer.h"
00003 
00004 // Song test program - plays a song using PWM and timer interrupts
00005 // for documentation see http://mbed.org/users/4180_1/notebook/using-a-speaker-for-audio-output/
00006 // can be used to play a song, if you have the notes and durations
00007 // for musical note frequencies see http://en.wikipedia.org/wiki/Piano_key_frequencies
00008 
00009 //Set up notes and durations for sample song to play
00010 // A 0.0 duration note at end terminates song play
00011 float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
00012                  1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0
00013                 };
00014 float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
00015                      0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0
00016                     };
00017 
00018 DigitalOut led1(LED1);
00019 int main()
00020 {
00021 // setup instance of new SongPlayer class, mySpeaker using pin 26
00022 // the pin must be a PWM output pin
00023     SongPlayer mySpeaker(p26);
00024 // Start song and return once playing starts
00025     mySpeaker.PlaySong(note,duration);
00026     // loops forever while song continues to play to end using interrupts
00027     while(1) {
00028         led1 = !led1;
00029         wait(.1);
00030     }
00031 }
00032