![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Alarm Clock
Fork of SmartRise_MBED by
Alarm.cpp@12:f42b74f76630, 2016-01-27 (annotated)
- Committer:
- pstephens18
- Date:
- Wed Jan 27 04:46:00 2016 +0000
- Revision:
- 12:f42b74f76630
- Parent:
- 11:be164273b969
Working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pstephens18 | 5:90f059fdc625 | 1 | #include "mbed.h" |
pstephens18 | 5:90f059fdc625 | 2 | PwmOut buzzer1(p21); |
pstephens18 | 5:90f059fdc625 | 3 | PwmOut buzzer2(p22); |
pstephens18 | 11:be164273b969 | 4 | DigitalOut led1(LED1); |
pstephens18 | 11:be164273b969 | 5 | DigitalOut led3(LED3); |
pstephens18 | 11:be164273b969 | 6 | bool go = true; // variable to control if the alarm sounds |
pstephens18 | 11:be164273b969 | 7 | Timeout reset; // resets the alarm so that it will not sound in an endless loop |
pstephens18 | 5:90f059fdc625 | 8 | |
pstephens18 | 5:90f059fdc625 | 9 | 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,/**/}; |
pstephens18 | 5:90f059fdc625 | 10 | 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/**/}; |
pstephens18 | 5:90f059fdc625 | 11 | 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}; |
pstephens18 | 5:90f059fdc625 | 12 | 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}; |
pstephens18 | 5:90f059fdc625 | 13 | |
pstephens18 | 11:be164273b969 | 14 | void turn_on(void); // Makes the alarm able to sound |
pstephens18 | 11:be164273b969 | 15 | void turn_off(void); // Turns of the alarm sound |
pstephens18 | 8:edf5f23cb393 | 16 | |
pstephens18 | 8:edf5f23cb393 | 17 | void Sound_Alarm() |
pstephens18 | 8:edf5f23cb393 | 18 | { |
pstephens18 | 12:f42b74f76630 | 19 | while(go) // Plays Star Wars |
pstephens18 | 5:90f059fdc625 | 20 | { |
pstephens18 | 11:be164273b969 | 21 | led1 = !led1; |
pstephens18 | 8:edf5f23cb393 | 22 | wait(.5); |
pstephens18 | 8:edf5f23cb393 | 23 | |
pstephens18 | 5:90f059fdc625 | 24 | for(int i=0; i<= 37;i++) |
pstephens18 | 5:90f059fdc625 | 25 | { |
pstephens18 | 5:90f059fdc625 | 26 | buzzer1.period(2/(frequency[i])); |
pstephens18 | 5:90f059fdc625 | 27 | buzzer2.period(2/(frequency[i])); |
pstephens18 | 5:90f059fdc625 | 28 | buzzer1 = 0.5; |
pstephens18 | 5:90f059fdc625 | 29 | buzzer2 = 0.5; |
pstephens18 | 11:be164273b969 | 30 | if(!go) |
pstephens18 | 11:be164273b969 | 31 | { |
pstephens18 | 11:be164273b969 | 32 | buzzer1 = 0; |
pstephens18 | 11:be164273b969 | 33 | buzzer2 = 0; |
pstephens18 | 12:f42b74f76630 | 34 | reset.attach(&turn_on,60.0); // Exit then Re-enable the alarm system after 60 seconds |
pstephens18 | 11:be164273b969 | 35 | break; |
pstephens18 | 11:be164273b969 | 36 | } |
pstephens18 | 5:90f059fdc625 | 37 | |
pstephens18 | 5:90f059fdc625 | 38 | wait(0.4*beat[i]); |
pstephens18 | 5:90f059fdc625 | 39 | if(beat[i]==0) |
pstephens18 | 5:90f059fdc625 | 40 | {wait(.05);} |
pstephens18 | 5:90f059fdc625 | 41 | |
pstephens18 | 5:90f059fdc625 | 42 | } |
pstephens18 | 5:90f059fdc625 | 43 | |
pstephens18 | 5:90f059fdc625 | 44 | for(int i=0; i<= 49;i++) |
pstephens18 | 5:90f059fdc625 | 45 | { |
pstephens18 | 5:90f059fdc625 | 46 | buzzer1.period(2/(frequency2[i])); |
pstephens18 | 5:90f059fdc625 | 47 | buzzer2.period(2/(frequency2[i])); |
pstephens18 | 5:90f059fdc625 | 48 | buzzer1 = 0.5; |
pstephens18 | 5:90f059fdc625 | 49 | buzzer2 = 0.5; |
pstephens18 | 5:90f059fdc625 | 50 | |
pstephens18 | 11:be164273b969 | 51 | if(!go) |
pstephens18 | 11:be164273b969 | 52 | { |
pstephens18 | 11:be164273b969 | 53 | buzzer1 = 0; |
pstephens18 | 11:be164273b969 | 54 | buzzer2 = 0; |
pstephens18 | 12:f42b74f76630 | 55 | reset.attach(&turn_on,60.0); // Exit then Re-enable the alarm system after 60 seconds |
pstephens18 | 11:be164273b969 | 56 | break; |
pstephens18 | 11:be164273b969 | 57 | } |
pstephens18 | 8:edf5f23cb393 | 58 | |
pstephens18 | 5:90f059fdc625 | 59 | wait(0.4*beat2[i]); |
pstephens18 | 5:90f059fdc625 | 60 | if(beat2[i]==0) |
pstephens18 | 5:90f059fdc625 | 61 | {wait(.05);} |
pstephens18 | 5:90f059fdc625 | 62 | |
pstephens18 | 5:90f059fdc625 | 63 | } |
pstephens18 | 8:edf5f23cb393 | 64 | |
pstephens18 | 5:90f059fdc625 | 65 | } |
pstephens18 | 5:90f059fdc625 | 66 | |
pstephens18 | 5:90f059fdc625 | 67 | } |
pstephens18 | 5:90f059fdc625 | 68 | |
pstephens18 | 5:90f059fdc625 | 69 | void turn_off() |
pstephens18 | 5:90f059fdc625 | 70 | { |
pstephens18 | 11:be164273b969 | 71 | go = false; |
pstephens18 | 11:be164273b969 | 72 | led1 =0; |
pstephens18 | 11:be164273b969 | 73 | led3 =0; |
pstephens18 | 5:90f059fdc625 | 74 | } |
pstephens18 | 11:be164273b969 | 75 | |
pstephens18 | 11:be164273b969 | 76 | void turn_on() |
pstephens18 | 11:be164273b969 | 77 | { |
pstephens18 | 11:be164273b969 | 78 | go = true; |
pstephens18 | 11:be164273b969 | 79 | led3 =1; |
pstephens18 | 11:be164273b969 | 80 | } |
pstephens18 | 11:be164273b969 | 81 |