SEARCH
TOOLBOX
LANGUAGES
modified on 17 September 2014 at 19:13 ••• 14,596 views

04-07 OrangesAndLemons

From Manuals

(Difference between revisions)
Jump to: navigation, search
Support (Talk | contribs)
(Created page with 'This example uses a PWM to play the tune "Oranges and Lemons" on a piezo buzzer using a PWM. /*Program Example 4.7: Plays the tune "Oranges and Lemons" on a piezo buzzer, using…')
Newer edit →

Revision as of 15:16, 14 August 2014

This example uses a PWM to play the tune "Oranges and Lemons" on a piezo buzzer using a PWM.

/*Program Example 4.7: Plays the tune "Oranges and Lemons" on a piezo buzzer, using PWM
                                                                         */
#include "mbed.h"
PwmOut buzzer(PWM1);
//frequency array
float frequency[]= {659,554,659,554,440,494,554,587,494,659,554,440};
float beat[]= {1,1,1,1,1,0.5,0.5,1,1,1,1,2};             //beat array

int main()
{
   while (1) {
       for (int i=0; i<=11; i++) {
           buzzer.period(1/(2*frequency[i]));                 // set PWM period
           buzzer=0.5;                                        // set duty cycle
           wait(0.4f*beat[i]);                                 // hold for beat period
       }
   }
}


BAM210
Schematic for example 04-07