Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 // Notes 00004 #define A4 440 00005 #define B4b 446 00006 #define C4 261 00007 #define C4_1 130 00008 #define C5 523 00009 #define D4 293 00010 #define D4b 277 00011 #define E4 329 00012 #define F4 349 00013 #define G4 392 00014 00015 const int 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}; 00016 const int intervals[] = {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}; 00017 00018 // speaker sound effect demo using PWM hardware output 00019 PwmOut speaker(PA_0); 00020 // serail port to PC (over USB) 00021 Serial pc(USBTX, USBRX); // tx, rx 00022 00023 int main() 00024 { 00025 int i; 00026 char c = 0; 00027 00028 // Intro ( ta-dah @ 500Hz ) 00029 speaker.period(1.0/500.0); // 500hz period 00030 speaker =0.5; //50% duty cycle - max volume 00031 wait(.05); 00032 speaker=0.0; // turn off audio 00033 wait(.05); 00034 speaker =0.5; //50% duty cycle - max volume 00035 wait(.5); 00036 speaker=0.0; // turn off audio 00037 00038 pc.printf (" \n"); 00039 pc.printf ("t=Tone, s=Sweep, p=Police, h=Song\n"); 00040 00041 while(c != ' ') { 00042 00043 c=pc.getc(); 00044 pc.printf("%c",c); 00045 wait(.1); 00046 00047 switch (c) 00048 { 00049 case 't': 00050 pc.printf ("Tone...\n"); 00051 // generate a short 150Hz tone using PWM hardware output 00052 // something like this can be used for a button click effect for feedback 00053 for (i=0; i<10; i++) { 00054 speaker.period(1.0/150.0); // 500hz period 00055 speaker =0.25; //25% duty cycle - mid range volume 00056 wait(.02); 00057 } 00058 speaker=0.0; // off 00059 break; 00060 00061 case 's': 00062 pc.printf ("Sweep...\n"); 00063 // sweep up in frequency by changing the PWM period 00064 for (i=0; i<8000; i=i+100) { 00065 speaker.period(1.0/float(i)); 00066 speaker=0.25; 00067 wait(.02); 00068 } 00069 speaker=0.0; // off 00070 break; 00071 00072 case 'p': 00073 pc.printf ("Police...\n"); 00074 // two tone police siren effect - two periods or two frequencies 00075 // increase volume - by changing the PWM duty cycle 00076 for (i=0; i<10; i=i+2) { 00077 speaker.period(1.0/969.0); 00078 speaker = float(i)/50.0; 00079 wait(.5); 00080 speaker.period(1.0/800.0); 00081 wait(.5); 00082 } 00083 speaker=0.0; // off 00084 break; 00085 00086 case 'h': 00087 pc.printf ("Happy birthday...\n"); 00088 for (int i=0;i<=24;i++) { 00089 speaker.period(1.0/notes[i]); 00090 speaker=0.25; 00091 wait(0.8*intervals[i]/10); 00092 } 00093 speaker=0.0; // off 00094 break; 00095 } 00096 } 00097 pc.printf ("Bye...\n"); 00098 }
Generated on Fri Jul 15 2022 10:10:02 by
1.7.2