Alarm Clock

Dependencies:   TextLCD mbed

Fork of SmartRise_MBED by Austin Sloop

Committer:
asloop18
Date:
Tue Jan 26 01:36:13 2016 +0000
Revision:
9:63098da649d1
Parent:
7:5412a6d7ef6d
Child:
12:f42b74f76630
evening 1/25. minor time tweaking

Who changed what in which revision?

UserRevisionLine numberNew contents of line
asloop18 4:33f3750fe70a 1 #include "mbed.h"
asloop18 4:33f3750fe70a 2 #include "TextLCD.h"
asloop18 3:51b248042aa0 3
asloop18 4:33f3750fe70a 4 //IO
asloop18 4:33f3750fe70a 5 TextLCD lcd(p27,p28,p23,p24,p25,p26); //RS E d1,d2,d3,d4
asloop18 4:33f3750fe70a 6 DigitalIn HrAdjustUp(p6);
asloop18 4:33f3750fe70a 7 DigitalIn HrAdjustDown(p7);
asloop18 4:33f3750fe70a 8 DigitalIn MinAdjust(p8); //buttons for setting time and alarm
asloop18 6:73866b51e0b7 9
asloop18 4:33f3750fe70a 10 DigitalOut arm(p12);
asloop18 3:51b248042aa0 11
asloop18 4:33f3750fe70a 12 struct tm* t;
asloop18 4:33f3750fe70a 13 int Ahh,AHH,Amm;
asloop18 4:33f3750fe70a 14 int HH,mm,hh; //HH 24hr || hh 12hr
asloop18 4:33f3750fe70a 15 char d,ad; //am0/pm1
asloop18 4:33f3750fe70a 16 Timer set;
asloop18 4:33f3750fe70a 17
asloop18 4:33f3750fe70a 18 void time_init(void){ //starting time and timers
asloop18 4:33f3750fe70a 19 set.start();
asloop18 6:73866b51e0b7 20 AHH=12;Amm=0;
asloop18 4:33f3750fe70a 21 }
asloop18 4:33f3750fe70a 22
asloop18 6:73866b51e0b7 23 void timeDisplay(void){ //main time management function and display
asloop18 6:73866b51e0b7 24 time_t rawTime;
asloop18 4:33f3750fe70a 25 time(&rawTime);
asloop18 4:33f3750fe70a 26 t = localtime(&rawTime);
asloop18 4:33f3750fe70a 27 HH=t->tm_hour; mm=t->tm_min; //updating local hr and min var
asloop18 4:33f3750fe70a 28
asloop18 6:73866b51e0b7 29 //TIME format
asloop18 4:33f3750fe70a 30 if(HH>12)hh=HH-12; else hh=HH+1; //convert 24 to 12 hr
asloop18 4:33f3750fe70a 31 if(HH>=12) d='P'; else d='A'; //update am/pm
asloop18 6:73866b51e0b7 32 //Alarm format
asloop18 9:63098da649d1 33 if(AHH>12)Ahh=AHH-12; else Ahh=AHH; //convert 24 to 12 hr
asloop18 4:33f3750fe70a 34 if(AHH>=12) ad='P'; else ad='A';
asloop18 6:73866b51e0b7 35
asloop18 4:33f3750fe70a 36 lcd.printf(" %2d/%.2d %2d:%.2d%cM Alarm-%2I:%.2I%cM " , t->tm_mon+1,t->tm_mday,hh,t->tm_min,d,Ahh,Amm,ad); //Print date(month/day), time
asloop18 6:73866b51e0b7 37 }
asloop18 4:33f3750fe70a 38
asloop18 7:5412a6d7ef6d 39 char checkAlarm(void){
asloop18 9:63098da649d1 40 if(HH==AHH&&mm==Amm&&d==ad) return 1;
asloop18 7:5412a6d7ef6d 41 else return 0;
asloop18 7:5412a6d7ef6d 42 }
asloop18 3:51b248042aa0 43
asloop18 4:33f3750fe70a 44 void alarmProg(void){ //Alarm set/adjust function to be called in main, when alarm set switch is enabled
asloop18 4:33f3750fe70a 45 if(set.read()>0.3){
asloop18 4:33f3750fe70a 46 if(HrAdjustUp==1) {AHH=AHH+1;set.reset();}
asloop18 4:33f3750fe70a 47 if(HrAdjustDown==1) {AHH=AHH-1;set.reset();}
asloop18 4:33f3750fe70a 48 if(MinAdjust==1) {Amm=Amm+1;set.reset();}
asloop18 4:33f3750fe70a 49 if(AHH>23) AHH=0; if(AHH<0) AHH=23;
asloop18 4:33f3750fe70a 50 if(AHH>=12) ad='P'; else ad='A';
asloop18 4:33f3750fe70a 51 if(Amm>=60) Amm=0;
asloop18 4:33f3750fe70a 52 }
asloop18 4:33f3750fe70a 53 }
asloop18 3:51b248042aa0 54
asloop18 6:73866b51e0b7 55 void timeProg(void){
asloop18 6:73866b51e0b7 56 time_t seconds = time(NULL);
asloop18 6:73866b51e0b7 57 if(set.read()>0.3){
asloop18 6:73866b51e0b7 58 if(HrAdjustUp==1){
asloop18 6:73866b51e0b7 59 set_time(seconds+60*60);
asloop18 6:73866b51e0b7 60 set.reset();
asloop18 6:73866b51e0b7 61 }
asloop18 6:73866b51e0b7 62 if(HrAdjustDown==1){
asloop18 6:73866b51e0b7 63 set_time(seconds-60*60);
asloop18 6:73866b51e0b7 64 set.reset();
asloop18 6:73866b51e0b7 65 }
asloop18 6:73866b51e0b7 66 if(MinAdjust==1){
asloop18 6:73866b51e0b7 67 set_time(seconds+60);
asloop18 6:73866b51e0b7 68 set.reset();
asloop18 6:73866b51e0b7 69 }
asloop18 6:73866b51e0b7 70 }
asloop18 6:73866b51e0b7 71 }
asloop18 3:51b248042aa0 72