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.
Dependencies: MaxSonar RTC-DS1307 TextLCD mbed
Diff: main.cpp
- Revision:
- 3:89dae25e597b
- Parent:
- 2:d3a3a15016f3
- Child:
- 4:f7b32fcc75f8
diff -r d3a3a15016f3 -r 89dae25e597b main.cpp
--- a/main.cpp Thu May 25 14:43:55 2017 +0000
+++ b/main.cpp Fri May 26 08:50:44 2017 +0000
@@ -1,7 +1,6 @@
#include "mbed.h"
#include "TextLCD.h"
#include "Rtc_Ds1307.h"
-#include "MaxSonar.h"
Rtc_Ds1307 rtc(PTE0, PTE1);
DigitalOut red(LED1);
@@ -12,6 +11,12 @@
Timer t;
void displayTime();
+int setAlrm(bool ho);
+bool hitIt(int hor, int min, Rtc_Ds1307::Time_rtc teim);
+
+int Halrm = -1;
+int Malrm = 0;
+
int main() {
@@ -20,17 +25,6 @@
rtc.startClock();
- 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){
wait_ms(50); //wait so button states do not overlap
@@ -54,6 +48,14 @@
lcd.cls();
lcd.printf("settin' alarm!");
wait(2);
+
+ Halrm = setAlrm(true);
+ if(Halrm >= 0){
+ Malrm = setAlrm(false);
+ lcd.cls();
+ lcd.printf("Alarm: %d:%d", Halrm,Malrm);
+ wait(2);
+ }
break;
}
}
@@ -89,7 +91,100 @@
if (rtc.getTime(tm) ) {
lcd.cls();
- lcd.printf("The time is :\n%02d:%02d:%02d", tm.hour, tm.min, tm.sec);
+ lcd.printf("The time is :\n%02d:%02d:%02d", tm.hour, tm.min, tm.sec); //display time
+
+ if(hitIt(Halrm, Malrm, tm)){
+ lcd.cls();
+ lcd.printf("You should be alarmed");
+ wait(4);
}
+
+ }
red = !red;
- }
\ No newline at end of file
+}
+
+int setAlrm(bool ho){
+
+ int mod;
+ float spd;
+ if(ho){
+ mod = 24;
+ spd = 0.5;
+ }else{
+ mod = 60;
+ spd = 0.25;
+ }
+
+ int homin = 0;
+ bool sat = false;
+ bool DC = false;
+
+ while(!sat){
+
+ while(!pin.read() && !sat){
+
+ lcd.cls();
+ if(ho) lcd.printf("Hour: %02d\nDC to confirm", homin);
+ else lcd.printf("Minutes: %02d\nDC to confirm", homin);
+
+ wait(0.2);
+ if(pin.read()){ //once it's 1 start the detection loop to decide whether this is a hold or a double click
+
+ 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 - confirm selection
+ lcd.cls();
+ if(ho) lcd.printf("Hour: %02d\nSet!", homin);
+ else lcd.printf("Minutes: %02d\nSet!", homin);
+ sat = true;
+ DC = true;
+ wait(2);
+ break;
+ }
+ }
+ break;
+ }
+ }
+ if(!pin.read() && !DC){ //if unpressed - single click detected - cancel alarm
+ lcd.cls();
+ lcd.printf("Alarm Canceled!");
+ homin = -1;
+ return homin;
+ }
+
+ while(pin.read()){ //still pressed? Execute Button HOLD case, exit hold when no longer held
+
+ homin = (homin + 1)%mod;
+ lcd.cls();
+ if(ho) lcd.printf("Hour: %02d", homin);
+ else lcd.printf("Minutes: %02d", homin); //display date
+ wait(spd);
+
+ }
+ }
+ }
+
+ }
+
+ return homin;
+}
+
+bool hitIt(int hor, int min, Rtc_Ds1307::Time_rtc teim){
+ int x = teim.hour - hor;
+ if(x == 0){
+ int y = teim.min - min;
+ if(y == 0){
+ return true;
+ }
+ }
+ return false;
+}
\ No newline at end of file