Alarm Clock
Fork of SmartRise_MBED by
Embed:
(wiki syntax)
Show/hide line numbers
Alarm.cpp
00001 #include "mbed.h" 00002 PwmOut buzzer1(p21); 00003 PwmOut buzzer2(p22); 00004 DigitalOut led1(LED1); 00005 DigitalOut led3(LED3); 00006 bool go = true; // variable to control if the alarm sounds 00007 Timeout reset; // resets the alarm so that it will not sound in an endless loop 00008 00009 float frequency[] = {262,0,262,0,262,0,349,0,523,0,466,0,440,0,392,0,698,0,523,0,466,0,440,0,392,0,698,0,523,0,466,0,440,0,466,0,392,0,/**/}; 00010 float beat[] = {.3,0,.3,0,.3,0,2,0,2,0,.3,0,.3,0,.3,0,2,0,1,0,.3,0,.3,0,.3,0,2,0,1,0,.3,0,.3,0,.3,0,2,0/**/}; 00011 float frequency2[] = {262,0,262,0,294,0,294,0,466,0,440,0,392,0,349,0,349,0,392,0,440,0,392,0,294,0,330,0,523,0,523,0,698,0,622,0,554,0,523,0,466,0,415,0,392,0,349,0,523,0}; 00012 float beat2[] = {.75,0,.25,0,1.5,0,.5,0,.5,0,.5,0,.5,0,.5,0,.3,0,.3,0,.3,0,1,0,.5,0,1,0,.75,0,.25,0,1,0,.5,0,1,0,.5,0,1,0,.5,0,1,0,.5,0,4,0}; 00013 00014 void turn_on(void); // Makes the alarm able to sound 00015 void turn_off(void); // Turns of the alarm sound 00016 00017 void Sound_Alarm() 00018 { 00019 while(go) // Plays Star Wars 00020 { 00021 led1 = !led1; 00022 wait(.5); 00023 00024 for(int i=0; i<= 37;i++) 00025 { 00026 buzzer1.period(2/(frequency[i])); 00027 buzzer2.period(2/(frequency[i])); 00028 buzzer1 = 0.5; 00029 buzzer2 = 0.5; 00030 if(!go) 00031 { 00032 buzzer1 = 0; 00033 buzzer2 = 0; 00034 reset.attach(&turn_on,60.0); // Exit then Re-enable the alarm system after 60 seconds 00035 break; 00036 } 00037 00038 wait(0.4*beat[i]); 00039 if(beat[i]==0) 00040 {wait(.05);} 00041 00042 } 00043 00044 for(int i=0; i<= 49;i++) 00045 { 00046 buzzer1.period(2/(frequency2[i])); 00047 buzzer2.period(2/(frequency2[i])); 00048 buzzer1 = 0.5; 00049 buzzer2 = 0.5; 00050 00051 if(!go) 00052 { 00053 buzzer1 = 0; 00054 buzzer2 = 0; 00055 reset.attach(&turn_on,60.0); // Exit then Re-enable the alarm system after 60 seconds 00056 break; 00057 } 00058 00059 wait(0.4*beat2[i]); 00060 if(beat2[i]==0) 00061 {wait(.05);} 00062 00063 } 00064 00065 } 00066 00067 } 00068 00069 void turn_off() 00070 { 00071 go = false; 00072 led1 =0; 00073 led3 =0; 00074 } 00075 00076 void turn_on() 00077 { 00078 go = true; 00079 led3 =1; 00080 } 00081
Generated on Thu Jul 14 2022 11:15:41 by
1.7.2
