Team Scream Machine / Mbed 2 deprecated aclock

Dependencies:   MaxSonar RTC-DS1307 TextLCD mbed

Revision:
2:d3a3a15016f3
Parent:
1:aa2b8929a968
Child:
3:89dae25e597b
--- a/main.cpp	Thu May 25 10:42:27 2017 +0000
+++ b/main.cpp	Thu May 25 14:43:55 2017 +0000
@@ -1,29 +1,89 @@
 #include "mbed.h"
 #include "TextLCD.h"
 #include "Rtc_Ds1307.h"
+#include "MaxSonar.h"
 
 Rtc_Ds1307 rtc(PTE0, PTE1);
-DigitalOut r(LED1);
+DigitalOut red(LED1);
 TextLCD lcd(PTE5, PTE4, PTE3, PTE2, PTB11, PTB10); // rs, e, d4-d7
+DigitalIn pin(PTE29); //button
 Serial pc(USBTX, USBRX);
 Rtc_Ds1307::Time_rtc tim = {};
+Timer t;
 
 void displayTime();
 
 int main() {
     
-    Ticker cloo;
-    rtc.startClock();  
+    Ticker cloo; //declare a ticker to update clock every sec
+    cloo.attach(&displayTime, 1); //the ticker runs the function that wipes the screen and displays the updated time
+    
+    rtc.startClock(); 
     
-    cloo.attach(&displayTime, 1);
+    MaxSonar *range;
+    float r;
+      
+    range = new MaxSonar(MS_LV, MS_ANALOG, PTE23, PTC2);
+    range->setVoltage(5);
+    range->setUnits(MS_CM);
+    
+    range->triggerRead();
+    wait_ms(49);
+    r = range->read();
     
     while(1){
-        r = !r;
-        wait(0.2);
+        
+        wait_ms(50); //wait so button states do not overlap
+        
+        if(pin.read()){                     //once it's 1 start the detection loop to decide whether this is a hold or a double click
+            
+            cloo.detach();                  //stop the ticker while performing button operations
+            
+            t.reset();                     
+            t.start();                      //start timer
+            
+            while(t.read() < 1){            //if it's not unpressed in a second - HOLD case
+                                    
+                if(!pin.read()){            //if it goes low, check for double click
+                
+                    t.reset();
+               
+                    while(t.read() < 1){      //listen for 1 sec for a second click
+                    
+                        if(pin.read()){          //if x = 1 execute DOUBLE CLICK - set alarm
+                            lcd.cls();
+                            lcd.printf("settin' alarm!");
+                            wait(2);
+                            break;
+                        }
+                    }
+                break;
+            }
         }
+        
+        
+        
+        while(pin.read()){                  //still pressed? Execute Button HOLD case, exit hold when no longer held
+            
+            rtc.getTime(tim);
+            
+            lcd.cls();
+            lcd.printf("Today is the :\n%02d/%02d/%02d", tim.date, tim.mon, tim.year); //display date 
+            wait(1);
+            
+        }
+            
+         cloo.attach(&displayTime, 1);      //re-enable ticker after date release   
+         
+    }
+        
+    t.stop();
+    
+    }
+    
 }
 
-void displayTime(){
+void displayTime(){ //get the time from the rtc chip and display it on the LCD
     
     Rtc_Ds1307::Time_rtc tm = tim;
     
@@ -31,4 +91,5 @@
         lcd.cls();
         lcd.printf("The time is :\n%02d:%02d:%02d", tm.hour, tm.min, tm.sec);
         }
+     red = !red;   
     }
\ No newline at end of file