Rob Toulson / Mbed 2 deprecated PE_04-07_OrangesAndLemons

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*Program Example 4.7: Plays the tune "Oranges and Lemons" on a piezo buzzer, using PWM
00002                                                                           */
00003 #include "mbed.h"
00004 PwmOut buzzer(p21);
00005 
00006 //frequency array
00007 float frequency[]= {659,554,659,554,440,494,554,587,494,659,554,440};
00008 float beat[]= {1,1,1,1,1,0.5,0.5,1,1,1,1,2};             //beat array
00009 int main()
00010 {
00011     while (1) {
00012         for (int i=0; i<=11; i++) {
00013             buzzer.period(1/(2*frequency[i]));                 // set PWM period
00014             buzzer=0.5;                                        // set duty cycle
00015             wait(0.4*beat[i]);                                 // hold for beat period
00016         }
00017     }
00018 }
00019