Clock program with alarm
Dependencies: MaxSonar RTC-DS1307 TextLCD mbed
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 #include "Rtc_Ds1307.h" 00004 00005 Rtc_Ds1307 rtc(PTE0, PTE1); 00006 DigitalOut red(LED1); 00007 DigitalOut tog(PTE3); 00008 TextLCD lcd(PTE30, PTE29, PTE23, PTE22, PTE21, PTE20); // rs, e, d4-d7 00009 DigitalIn pin(PTA1); //button 00010 Serial pc(USBTX, USBRX); 00011 Rtc_Ds1307::Time_rtc tim = {}; 00012 PwmOut boop(PTD4); //alarm buzzer 00013 Timer t; 00014 00015 void displayTime(); 00016 int setAlrm(bool ho); 00017 bool hitIt(int hor, int min, Rtc_Ds1307::Time_rtc teim); 00018 00019 int Halrm = -1; 00020 int Malrm = 0; 00021 00022 00023 int main() { 00024 00025 tog.write(1); 00026 boop.write(0); //initialise buzzer 00027 boop.period(5); 00028 00029 Ticker cloo; //declare a ticker to update clock every sec 00030 cloo.attach(&displayTime, 1); //the ticker runs the function that wipes the screen and displays the updated time 00031 00032 rtc.startClock(); 00033 00034 while(1){ 00035 00036 wait_ms(50); //wait so button states do not overlap 00037 00038 if(pin.read()){ //once it's 1 start the detection loop to decide whether this is a hold or a double click 00039 00040 cloo.detach(); //stop the ticker while performing button operations 00041 00042 t.reset(); 00043 t.start(); //start timer 00044 00045 while(t.read() < 1){ //if it's not unpressed in a second - HOLD case 00046 00047 if(!pin.read()){ //if it goes low, check for double click 00048 00049 t.reset(); 00050 00051 while(t.read() < 1){ //listen for 1 sec for a second click 00052 00053 if(pin.read()){ //if x = 1 execute DOUBLE CLICK - set alarm 00054 lcd.cls(); 00055 lcd.printf("settin' alarm!"); 00056 wait(2); 00057 00058 Halrm = setAlrm(true); 00059 if(Halrm >= 0){ 00060 Malrm = setAlrm(false); 00061 lcd.cls(); 00062 lcd.printf("Alarm: %d:%d", Halrm,Malrm); 00063 wait(2); 00064 } 00065 break; 00066 } 00067 } 00068 break; 00069 } 00070 } 00071 00072 00073 00074 while(pin.read()){ //still pressed? Execute Button HOLD case, exit hold when no longer held 00075 00076 rtc.getTime(tim); 00077 00078 lcd.cls(); 00079 lcd.printf("Today is the :\n%02d/%02d/%02d", tim.date, tim.mon, tim.year); //display date 00080 wait(1); 00081 00082 } 00083 00084 cloo.attach(&displayTime, 1); //re-enable ticker after date release 00085 00086 } 00087 00088 t.stop(); 00089 00090 } 00091 00092 } 00093 00094 void displayTime(){ //get the time from the rtc chip and display it on the LCD 00095 00096 Rtc_Ds1307::Time_rtc tm = tim; 00097 00098 if (rtc.getTime(tm) ) { 00099 lcd.cls(); 00100 lcd.printf("The time is :\n%02d:%02d:%02d", tm.hour, tm.min, tm.sec); //display time 00101 00102 if(hitIt(Halrm, Malrm, tm)){ //check if the time is right for alarmage 00103 boop.write(0.5); 00104 while(!pin.read()){ 00105 lcd.cls(); 00106 lcd.printf("You should be alarmed"); 00107 wait(0.2); 00108 } 00109 boop.write(0); 00110 } 00111 00112 } 00113 red = !red; 00114 } 00115 00116 int setAlrm(bool ho){ 00117 00118 int mod; 00119 float spd; 00120 if(ho){ 00121 mod = 24; 00122 spd = 0.5; 00123 }else{ 00124 mod = 60; 00125 spd = 0.25; 00126 } 00127 00128 int homin = 0; 00129 bool sat = false; 00130 bool DC = false; 00131 00132 while(!sat){ 00133 00134 while(!pin.read() && !sat){ 00135 00136 lcd.cls(); 00137 if(ho) lcd.printf("Hour: %02d\nDC to confirm", homin); 00138 else lcd.printf("Minutes: %02d\nDC to confirm", homin); 00139 00140 wait(0.2); 00141 if(pin.read()){ //once it's 1 start the detection loop to decide whether this is a hold or a double click 00142 00143 t.reset(); 00144 t.start(); //start timer 00145 00146 while(t.read() < 1){ //if it's not unpressed in a second - HOLD case 00147 00148 if(!pin.read()){ //if it goes low, check for double click 00149 00150 t.reset(); 00151 00152 while(t.read() < 1){ //listen for 1 sec for a second click 00153 00154 if(pin.read()){ //if x = 1 execute DOUBLE CLICK - confirm selection 00155 lcd.cls(); 00156 if(ho) lcd.printf("Hour: %02d\nSet!", homin); 00157 else lcd.printf("Minutes: %02d\nSet!", homin); 00158 sat = true; 00159 DC = true; 00160 wait(2); 00161 break; 00162 } 00163 } 00164 break; 00165 } 00166 } 00167 if(!pin.read() && !DC){ //if unpressed - single click detected - cancel alarm 00168 lcd.cls(); 00169 lcd.printf("Alarm Canceled!"); 00170 homin = -1; 00171 return homin; 00172 } 00173 00174 while(pin.read()){ //still pressed? Execute Button HOLD case, exit hold when no longer held 00175 00176 homin = (homin + 1)%mod; //increment the value while the button is held 00177 lcd.cls(); 00178 if(ho) lcd.printf("Hour: %02d", homin); 00179 else lcd.printf("Minutes: %02d", homin); 00180 wait(spd); 00181 00182 } 00183 } 00184 } 00185 00186 } 00187 00188 return homin; 00189 } 00190 00191 //this function checks whether it's time to sound the alarms 00192 //if the minute and the hour is the same as that of the alarm, return true 00193 bool hitIt(int hor, int min, Rtc_Ds1307::Time_rtc teim){ 00194 int x = teim.hour - hor; 00195 if(x == 0){ 00196 int y = teim.min - min; 00197 if(y == 0){ 00198 return true; 00199 } 00200 } 00201 return false; 00202 }
Generated on Sun Jul 17 2022 21:00:33 by
1.7.2