Alarm Clock

Dependencies:   TextLCD mbed

Fork of SmartRise_MBED by Austin Sloop

Revision:
11:be164273b969
Parent:
8:edf5f23cb393
Child:
12:f42b74f76630
diff -r ab7bfdf6a2e7 -r be164273b969 Alarm.cpp
--- a/Alarm.cpp	Tue Jan 26 23:13:48 2016 +0000
+++ b/Alarm.cpp	Wed Jan 27 02:03:43 2016 +0000
@@ -1,21 +1,24 @@
 #include "mbed.h"
 PwmOut buzzer1(p21);
 PwmOut buzzer2(p22);
-DigitalOut led(LED1);
-bool go = true;
+DigitalOut led1(LED1);
+DigitalOut led3(LED3);
+bool go = true;  // variable to control if the alarm sounds
+Timeout reset;  // resets the alarm so that it will not sound in an endless loop
 
 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,/**/};
 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/**/};
 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};
 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};
 
+void turn_on(void);  // Makes the alarm able to sound
+void turn_off(void); // Turns of the alarm sound
 
 void Sound_Alarm()
 {
-
     while(go)
     {
-        led = !led;
+        led1 = !led1;
         wait(.5);
         
         for(int i=0; i<= 37;i++)
@@ -24,7 +27,13 @@
             buzzer2.period(2/(frequency[i]));
             buzzer1 = 0.5;
             buzzer2 = 0.5;
-            if(!go){break;}
+            if(!go)
+            {
+                buzzer1 = 0;
+                buzzer2 = 0;
+                reset.attach(&turn_on,60.0);
+                break;
+            }
             
             wait(0.4*beat[i]); 
             if(beat[i]==0)
@@ -39,7 +48,13 @@
             buzzer1 = 0.5;
             buzzer2 = 0.5;
             
-            if(!go){break;}
+            if(!go)
+            {
+                buzzer1 = 0;
+                buzzer2 = 0;
+                reset.attach(&turn_on,60.0);
+                break;
+            }
             
             wait(0.4*beat2[i]); 
             if(beat2[i]==0)
@@ -49,11 +64,18 @@
         
     }
         
-    go=true;
 }    
 
 void turn_off()
 {
-    go = !go;
-    led =0;    
+    go = false;
+    led1 =0;
+    led3 =0;    
 }
+
+void turn_on()
+{
+    go = true; 
+    led3 =1;     
+}
+