Bhakti Kulkarni / Mbed 2 deprecated Happy_Birthday

Dependencies:   DebounceInterrupts mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /****************************************************************************************/
00002 /*                                 IO DEVICES ASSIGNMENT 6                              */
00003 /*This program uses the PWM output to generate a "HAPPY BIRTHDAY" Tone on the speaker   */
00004 /****************************************************************************************/
00005 
00006 #include "mbed.h"
00007 
00008 //              ms      
00009 #define A4      440        
00010 #define B4b     446         
00011 #define C4      261
00012 #define C4_1    130 
00013 #define C5      523      
00014 #define D4      293   
00015 #define D4b     277               
00016 #define E4      329          
00017 #define F4      349           
00018 #define G4      392  
00019 
00020 PwmOut buzzer(p26);
00021 
00022 
00023 float notes[] = {C4_1,C4,D4,C4,F4,E4,C4_1,C4,D4,C4,G4,F4,C4_1,C4,C5,A4,F4,E4,D4,B4b,B4b,A4,F4,G4,F4};
00024 
00025 
00026 float interval[] = {4, 4, 8, 8, 8, 10, 4, 4, 8, 8, 8, 10, 4, 4, 8, 8, 8, 8, 8, 4, 4, 8, 8, 8, 12};                            
00027                              
00028 int main() {
00029     while (1) {
00030     for (int i=0;i<=24;i++) {
00031         buzzer.period((1/notes[i]));                        // set PWM period
00032         buzzer=0.5;                                         // set duty cycle
00033         wait(0.8*interval[i]/10);                           // hold for beat period
00034         }
00035     buzzer = 0;
00036     wait(5);
00037     }
00038 }
00039 
00040 
00041 /****************************************************************************************/
00042 /*                                        TESTING                                       */
00043 /****************************************************************************************/
00044 
00045 /*1. Power up the mbed, load the program and press the reset button. The tone should play*/
00046 /*   as long as the mbed is powered up.                                              PASS*/
00047 /*****************************************************************************************/