pp

Dependencies:   mbed

Committer:
Peeraya
Date:
Wed Dec 09 02:09:36 2015 +0000
Revision:
5:60262753a05f
Parent:
4:a61cdd4e7afa
Super Cute Alarm Clock

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Peeraya 0:b2064b3558b6 1 #include "mbed.h"
Peeraya 0:b2064b3558b6 2 #include "DS1307.h"
Peeraya 0:b2064b3558b6 3 #include "DigitDisplay.h"
NamSom 4:a61cdd4e7afa 4 #include "Game.h"
Peeraya 0:b2064b3558b6 5
Peeraya 0:b2064b3558b6 6 DS1307 myrtc(I2C_SDA,I2C_SCL);
Peeraya 0:b2064b3558b6 7 DigitDisplay display(D7,D8);
NamSom 4:a61cdd4e7afa 8 Game game;
Peeraya 0:b2064b3558b6 9 DigitalOut buzzer(D6);
NamSom 1:ba7154d2d7ca 10 BusIn b(D9,D10,D11);
NamSom 1:ba7154d2d7ca 11 BusOut led(D2,D3,D4,D5);
Peeraya 3:dc88b52d526c 12 AnalogIn analog_value(A2);
NamSom 4:a61cdd4e7afa 13 const bool bcd[16][4] = {{0,0,0,0},{0,0,0,1},{0,0,1,0},{0,0,1,1},{0,1,0,0},{0,1,0,1},{0,1,1,0},{0,1,1,1},
NamSom 4:a61cdd4e7afa 14 {1,0,0,0},{1,0,0,1},{1,0,1,0},{1,0,1,1},{1,1,0,0},{1,1,0,1},{1,1,1,0},{1,1,1,1}};
Peeraya 3:dc88b52d526c 15
Peeraya 3:dc88b52d526c 16 void DisplayTime();
Peeraya 3:dc88b52d526c 17
NamSom 1:ba7154d2d7ca 18 int mode=0;
NamSom 1:ba7154d2d7ca 19 int main()
NamSom 1:ba7154d2d7ca 20 {
NamSom 1:ba7154d2d7ca 21 b.mode(PullUp);
Peeraya 5:60262753a05f 22 int p_ldr=0,cr_ldr=0,sn=0,j=0;
Peeraya 5:60262753a05f 23 int sec=0,min=39,hour=21,day=19,date=7,month=12,year=15;
Peeraya 3:dc88b52d526c 24 int n=0,opt=1,p=0,ten_hour,nu_hour,ten_min,nu_min,hour_a=7,min_a=0;
Peeraya 3:dc88b52d526c 25 float mea;
Peeraya 5:60262753a05f 26 int state=0, wrong=0;
Peeraya 5:60262753a05f 27 int hexnum[4],game2num[10],game2level=5;
Peeraya 5:60262753a05f 28 int currenthex=0, checkbcd=0, gameround=0, changehex=1, changenum=1,currentnum=0,checkloop=0;
Peeraya 5:60262753a05f 29 bool imawake=0;
Peeraya 5:60262753a05f 30 int fix=0,match=0,fail=1,delay=0,i;
Peeraya 3:dc88b52d526c 31
Peeraya 5:60262753a05f 32 //myrtc.settime(sec,min,hour,day,date,month,year);
Peeraya 3:dc88b52d526c 33 //DisplayTime();
Peeraya 3:dc88b52d526c 34 //display.Displayset((hour-(hour%10))/10,hour%10,(min-(min%10))/10,min%10);
NamSom 1:ba7154d2d7ca 35 while(1)
Peeraya 5:60262753a05f 36 {
Peeraya 5:60262753a05f 37 myrtc.gettime(&sec,&min,&hour,&day,&date,&month,&year);
Peeraya 5:60262753a05f 38 //state=2;
Peeraya 5:60262753a05f 39 if(state==0)
NamSom 4:a61cdd4e7afa 40 {
Peeraya 5:60262753a05f 41 if(sn==0){led=0b0001;}
Peeraya 5:60262753a05f 42 if(sn==1){led=0b0011;}
Peeraya 5:60262753a05f 43 if(sn==2){led=0b0111;}
Peeraya 5:60262753a05f 44 if(sn==3){led=0b1111;}
Peeraya 5:60262753a05f 45 printf("In state1\n");
Peeraya 5:60262753a05f 46 printf("houra/mind = %d : %d",hour,min);
NamSom 4:a61cdd4e7afa 47 mea=analog_value.read();
NamSom 4:a61cdd4e7afa 48 if(b==0b011){n=1;}
NamSom 4:a61cdd4e7afa 49 else if(b==0b101){n=2;}
NamSom 4:a61cdd4e7afa 50 else if(b==0b110){n=3;}
NamSom 4:a61cdd4e7afa 51 else n=0;
NamSom 4:a61cdd4e7afa 52 display.getButtonState(n);
NamSom 4:a61cdd4e7afa 53
NamSom 4:a61cdd4e7afa 54 wait_ms(100);
NamSom 4:a61cdd4e7afa 55
NamSom 4:a61cdd4e7afa 56 //printf("%d\n",mode);
NamSom 4:a61cdd4e7afa 57
NamSom 4:a61cdd4e7afa 58 wait_ms(100);
NamSom 4:a61cdd4e7afa 59
NamSom 4:a61cdd4e7afa 60 if(display.checkButton3()==1)
NamSom 4:a61cdd4e7afa 61 {
NamSom 4:a61cdd4e7afa 62 p++;
NamSom 4:a61cdd4e7afa 63 if(p==10){p=0;}
NamSom 4:a61cdd4e7afa 64 printf("%d\n",p);
NamSom 4:a61cdd4e7afa 65 if(p%2==0){opt=1;}
NamSom 4:a61cdd4e7afa 66 if(p%2==1){opt=2;}
NamSom 4:a61cdd4e7afa 67 }
NamSom 4:a61cdd4e7afa 68
NamSom 4:a61cdd4e7afa 69 if(opt==1)
NamSom 4:a61cdd4e7afa 70 {
NamSom 4:a61cdd4e7afa 71 display.setColon(1);
NamSom 4:a61cdd4e7afa 72 display.Displayset((hour-(hour%10))/10,hour%10,(min-(min%10))/10,min%10);
NamSom 4:a61cdd4e7afa 73 }
NamSom 4:a61cdd4e7afa 74 if(opt==2)
NamSom 4:a61cdd4e7afa 75 {
NamSom 4:a61cdd4e7afa 76 ten_hour=(hour_a-(hour_a%10))/10;
NamSom 4:a61cdd4e7afa 77 nu_hour=hour_a%10;
NamSom 4:a61cdd4e7afa 78 ten_min=(min_a-(min_a%10))/10;
NamSom 4:a61cdd4e7afa 79 nu_min=min_a%10;
NamSom 4:a61cdd4e7afa 80 display.Displayset(ten_hour,nu_hour,ten_min,nu_min);
NamSom 1:ba7154d2d7ca 81
NamSom 4:a61cdd4e7afa 82 if(display.checkButton1()==1){hour_a++;}
Peeraya 5:60262753a05f 83 if(display.checkButton2()==1){min_a=min_a+5;}
NamSom 4:a61cdd4e7afa 84
NamSom 4:a61cdd4e7afa 85 if(hour_a==24){hour_a=0;}
NamSom 4:a61cdd4e7afa 86 if(min_a==60){min_a=0;}
NamSom 4:a61cdd4e7afa 87
NamSom 4:a61cdd4e7afa 88 }
Peeraya 5:60262753a05f 89 if(hour==hour_a&&min==min_a&&imawake==0){buzzer=1; state=1;printf("wait check");}
Peeraya 5:60262753a05f 90 else if(imawake==1 && min!=min_a) imawake=0;
NamSom 4:a61cdd4e7afa 91 }
Peeraya 5:60262753a05f 92
Peeraya 5:60262753a05f 93
Peeraya 5:60262753a05f 94 if(state==1)
Peeraya 3:dc88b52d526c 95 {
Peeraya 5:60262753a05f 96 //####SNOOZE##
Peeraya 5:60262753a05f 97 mea=analog_value.read();
Peeraya 5:60262753a05f 98 if(mea<0.65){cr_ldr=0;}
Peeraya 5:60262753a05f 99 if(mea>=0.65){cr_ldr=1;}
Peeraya 5:60262753a05f 100
Peeraya 5:60262753a05f 101 printf("%f",mea);
Peeraya 5:60262753a05f 102
Peeraya 5:60262753a05f 103 wait_ms(100);
Peeraya 5:60262753a05f 104 printf("%d : %d\n",p_ldr,cr_ldr);
Peeraya 5:60262753a05f 105 if(b==0b011){n=1;}
Peeraya 5:60262753a05f 106 else if(b==0b101){n=2;}
Peeraya 5:60262753a05f 107 else if(b==0b110){n=3;}
Peeraya 5:60262753a05f 108 else n=0;
Peeraya 5:60262753a05f 109 display.getButtonState(n);
Peeraya 5:60262753a05f 110
Peeraya 5:60262753a05f 111 if(sn<3 && p_ldr==0 && cr_ldr==1)
NamSom 4:a61cdd4e7afa 112 {
Peeraya 5:60262753a05f 113 buzzer=0;
Peeraya 5:60262753a05f 114 sn++;
Peeraya 5:60262753a05f 115 if(min_a<55){min_a = min_a+5;state=0;}
Peeraya 5:60262753a05f 116 else if(min_a>=55){min_a = (min_a+5)%60 ; hour_a=hour_a+1;state=0;}
Peeraya 5:60262753a05f 117 printf("In loop");
Peeraya 5:60262753a05f 118 }
Peeraya 5:60262753a05f 119
Peeraya 5:60262753a05f 120 printf("h/m %d : %d\n",hour,min);
Peeraya 5:60262753a05f 121 printf("h_a/h_m %d : %d\n",hour_a,min_a);
Peeraya 5:60262753a05f 122 printf("state : %d\n",state);
Peeraya 5:60262753a05f 123 printf("snooze : %d",sn);
Peeraya 5:60262753a05f 124 if(display.checkButton1()==1||display.checkButton2()==1||display.checkButton3()==1){state=2;}
Peeraya 5:60262753a05f 125
Peeraya 5:60262753a05f 126 p_ldr=cr_ldr;
Peeraya 5:60262753a05f 127 if(sn==0){led=0b0001;}
Peeraya 5:60262753a05f 128 if(sn==1){led=0b0011;}
Peeraya 5:60262753a05f 129 if(sn==2){led=0b0111;}
Peeraya 5:60262753a05f 130 if(sn==3){led=0b1111;}
NamSom 4:a61cdd4e7afa 131 }
Peeraya 5:60262753a05f 132
Peeraya 5:60262753a05f 133 if(state==2)
Peeraya 5:60262753a05f 134 {
Peeraya 5:60262753a05f 135 mea=analog_value.read();
Peeraya 5:60262753a05f 136 //state = 1;
Peeraya 5:60262753a05f 137 int gameRandom = (int)(analog_value.read())%3;
Peeraya 5:60262753a05f 138 if(gameRandom==0)
NamSom 4:a61cdd4e7afa 139 {
Peeraya 5:60262753a05f 140 if(b==0b011){n=1;}
Peeraya 5:60262753a05f 141 else if(b==0b101){n=2;}
Peeraya 5:60262753a05f 142 else if(b==0b110){n=3;}
Peeraya 5:60262753a05f 143 else n=0;
Peeraya 5:60262753a05f 144
Peeraya 5:60262753a05f 145 if(sn==0){delay=5000;}
Peeraya 5:60262753a05f 146 if(sn==1){delay=4500;}
Peeraya 5:60262753a05f 147 if(sn==2){delay=4000;}
Peeraya 5:60262753a05f 148 if(sn==3){delay=3500;}
Peeraya 5:60262753a05f 149
Peeraya 5:60262753a05f 150 if(fail==1)
Peeraya 5:60262753a05f 151 {
Peeraya 5:60262753a05f 152 fix=(int(mea*1000))%99;
Peeraya 5:60262753a05f 153 fail=0;
Peeraya 5:60262753a05f 154 }
Peeraya 5:60262753a05f 155 match++;
Peeraya 5:60262753a05f 156 if(match==100){match=0;}
Peeraya 5:60262753a05f 157
Peeraya 5:60262753a05f 158 for(i=0;i<delay;i++)
Peeraya 5:60262753a05f 159 {
Peeraya 5:60262753a05f 160 display.Displayset((fix-(fix%10))/10,fix%10,(match-(match%10))/10,match%10);
Peeraya 5:60262753a05f 161 display.getButtonState(n);
Peeraya 5:60262753a05f 162 if(display.checkButton1()==1)
Peeraya 5:60262753a05f 163 {
Peeraya 5:60262753a05f 164
Peeraya 5:60262753a05f 165 if(fix==match||fix-match==1||match-fix==1){fail=1;gameround++;display.A();}
Peeraya 5:60262753a05f 166 else{fail=1;display.F();}
Peeraya 5:60262753a05f 167 wait_ms(100);
Peeraya 5:60262753a05f 168 i=delay;
Peeraya 5:60262753a05f 169 match=0;
Peeraya 5:60262753a05f 170 if(gameround==3){gameround=0;buzzer=0;state=0;imawake=1;sn=0;break;}
Peeraya 5:60262753a05f 171 }
Peeraya 5:60262753a05f 172 }
Peeraya 5:60262753a05f 173
Peeraya 5:60262753a05f 174 }
Peeraya 5:60262753a05f 175 //######################
Peeraya 5:60262753a05f 176 if(gameRandom==1)
Peeraya 5:60262753a05f 177 {
Peeraya 5:60262753a05f 178 if(wrong>3) game2level = 10;
Peeraya 5:60262753a05f 179 else if(sn==0) game2level = 5;
Peeraya 5:60262753a05f 180 else if(sn==1) game2level = 6;
Peeraya 5:60262753a05f 181 else if(sn==2) game2level = 7;
Peeraya 5:60262753a05f 182 else if(sn==3) game2level = 8;
Peeraya 5:60262753a05f 183 if (changenum==1)
Peeraya 5:60262753a05f 184 {
Peeraya 5:60262753a05f 185 for(int i=0;i<game2level;i++)
Peeraya 5:60262753a05f 186 {
Peeraya 5:60262753a05f 187 game2num[i] = (int)(analog_value.read()*1000)%3+1;
Peeraya 5:60262753a05f 188 }
Peeraya 5:60262753a05f 189 for(int i=0;i<game2level;i++)
Peeraya 5:60262753a05f 190 {
Peeraya 5:60262753a05f 191 display.Displayset(0xFF,0xFF,game2num[i],0xFF);
Peeraya 5:60262753a05f 192 wait_ms(400);
Peeraya 5:60262753a05f 193 display.Displayset(0xFF,0xFF,0xFF,0xFF);
Peeraya 5:60262753a05f 194 wait_ms(80);
Peeraya 5:60262753a05f 195 changenum=0;
Peeraya 5:60262753a05f 196 }
Peeraya 5:60262753a05f 197 }
Peeraya 5:60262753a05f 198 checkloop++;
Peeraya 5:60262753a05f 199 if(checkloop==60)
Peeraya 5:60262753a05f 200 {
Peeraya 5:60262753a05f 201 for(int i=0;i<game2level;i++)
Peeraya 5:60262753a05f 202 {
Peeraya 5:60262753a05f 203 display.Displayset(0xFF,0xFF,game2num[i],0xFF);
Peeraya 5:60262753a05f 204 wait_ms(400);
Peeraya 5:60262753a05f 205 display.Displayset(0xFF,0xFF,0xFF,0xFF);
Peeraya 5:60262753a05f 206 wait_ms(80);
Peeraya 5:60262753a05f 207 checkloop=0;
Peeraya 5:60262753a05f 208 currentnum= 0;
Peeraya 5:60262753a05f 209 }
Peeraya 5:60262753a05f 210 }
Peeraya 5:60262753a05f 211 display.Displayset(0xFE,0xFE,0xFE,0xFE);
Peeraya 5:60262753a05f 212 if(b==0b011){n=1;}
Peeraya 5:60262753a05f 213 else if(b==0b101){n=2;}
Peeraya 5:60262753a05f 214 else if(b==0b110){n=3;}
Peeraya 5:60262753a05f 215 else n=0;
Peeraya 5:60262753a05f 216 display.getButtonState(n);
Peeraya 5:60262753a05f 217 wait_ms(100);
Peeraya 5:60262753a05f 218 if((game2num[currentnum]==1 && display.checkButton1()==1)||(game2num[currentnum]==2 && display.checkButton2()==1)||(game2num[currentnum]==3 && display.checkButton3()==1))
NamSom 4:a61cdd4e7afa 219 {
Peeraya 5:60262753a05f 220 if(currentnum<game2level-1) {currentnum++; printf("-_-\n");}
Peeraya 5:60262753a05f 221 else
NamSom 4:a61cdd4e7afa 222 {
Peeraya 5:60262753a05f 223 printf("YEAH\n");
NamSom 4:a61cdd4e7afa 224 gameround++;
Peeraya 5:60262753a05f 225 changenum = 1;
Peeraya 5:60262753a05f 226 currentnum= 0;
NamSom 4:a61cdd4e7afa 227 display.A();
Peeraya 5:60262753a05f 228
Peeraya 5:60262753a05f 229 }
Peeraya 5:60262753a05f 230 }
Peeraya 5:60262753a05f 231 else if((game2num[currentnum]==1 && (display.checkButton2()==1||display.checkButton3()==1))||(game2num[currentnum]==2 && (display.checkButton1()==1||display.checkButton3()==1))||(game2num[currentnum]==3 && (display.checkButton1()==1||display.checkButton2()==1)))
Peeraya 5:60262753a05f 232 {
Peeraya 5:60262753a05f 233 currentnum=0;
Peeraya 5:60262753a05f 234 wrong++;
Peeraya 5:60262753a05f 235 changenum = 1;
Peeraya 5:60262753a05f 236 display.F();
Peeraya 5:60262753a05f 237 }
Peeraya 5:60262753a05f 238 if(gameround==4)
Peeraya 5:60262753a05f 239 {
Peeraya 5:60262753a05f 240 printf("eiei\n");
Peeraya 5:60262753a05f 241 mode=1;
Peeraya 5:60262753a05f 242 state=0;
Peeraya 5:60262753a05f 243 wrong=0;
Peeraya 5:60262753a05f 244 buzzer=0;
Peeraya 5:60262753a05f 245 game2level=5;
Peeraya 5:60262753a05f 246 currentnum=0;
Peeraya 5:60262753a05f 247 gameround=0;
Peeraya 5:60262753a05f 248 changenum=1;
Peeraya 5:60262753a05f 249 imawake=1;
Peeraya 5:60262753a05f 250 sn=0;
Peeraya 5:60262753a05f 251 }
Peeraya 5:60262753a05f 252 }
Peeraya 5:60262753a05f 253 //########################################
Peeraya 5:60262753a05f 254 else if(gameRandom==2)
Peeraya 5:60262753a05f 255 {
Peeraya 5:60262753a05f 256 int gamelevel=game.getlevel();
Peeraya 5:60262753a05f 257 if(changehex==1)
Peeraya 5:60262753a05f 258 {
Peeraya 5:60262753a05f 259 for(int i=0;i<gamelevel;i++)
Peeraya 5:60262753a05f 260 {
Peeraya 5:60262753a05f 261 mea=analog_value.read();
Peeraya 5:60262753a05f 262 hexnum[i] = game.randomHex(mea);
Peeraya 5:60262753a05f 263 }
Peeraya 5:60262753a05f 264 if(gamelevel==1) display.Displayset(0xFF,0xFF,hexnum[0],0xFF);
Peeraya 5:60262753a05f 265 else if(gamelevel==2) display.Displayset(0xFF,hexnum[0],hexnum[1],0xFF);
Peeraya 5:60262753a05f 266 else if(gamelevel==3) display.Displayset(0xFF,hexnum[0],hexnum[1],hexnum[2]);
Peeraya 5:60262753a05f 267 else if(gamelevel==4) display.Displayset(hexnum[0],hexnum[1],hexnum[2],hexnum[3]);
Peeraya 5:60262753a05f 268 changehex = 0;
Peeraya 5:60262753a05f 269 }
Peeraya 5:60262753a05f 270 if(b==0b011){n=1;}
Peeraya 5:60262753a05f 271 else if(b==0b101){n=2;}
Peeraya 5:60262753a05f 272 else if(b==0b110){n=3;}
Peeraya 5:60262753a05f 273 else n=0;
Peeraya 5:60262753a05f 274 display.getButtonState(n);
Peeraya 5:60262753a05f 275 wait_ms(100);
Peeraya 5:60262753a05f 276 if((bcd[hexnum[currenthex]][checkbcd]==0 && display.checkButton1()==1)||(bcd[hexnum[currenthex]][checkbcd]==1 && display.checkButton2()==1))
Peeraya 5:60262753a05f 277 {printf("**%d\t%d\n",hexnum[currenthex],checkbcd);
Peeraya 5:60262753a05f 278 if(checkbcd<3) {checkbcd++; printf("-_-\n");}
Peeraya 5:60262753a05f 279 else
Peeraya 5:60262753a05f 280 {printf("4\n");
Peeraya 5:60262753a05f 281 checkbcd=0;
Peeraya 5:60262753a05f 282 currenthex++;
Peeraya 5:60262753a05f 283 if(currenthex==gamelevel)
Peeraya 5:60262753a05f 284 {printf("YEAH\n");
Peeraya 5:60262753a05f 285 gameround++;
Peeraya 5:60262753a05f 286 changehex = 1;
Peeraya 5:60262753a05f 287 currenthex = 0;
Peeraya 5:60262753a05f 288 display.A();
Peeraya 5:60262753a05f 289 }
Peeraya 5:60262753a05f 290 }
Peeraya 5:60262753a05f 291 }
Peeraya 5:60262753a05f 292 else if((bcd[hexnum[currenthex]][checkbcd]==1 && display.checkButton1()==1)||(bcd[hexnum[currenthex]][checkbcd]==0 && display.checkButton2()==1))
Peeraya 5:60262753a05f 293 {printf("**%d\t%d\n",hexnum[currenthex],checkbcd);
Peeraya 5:60262753a05f 294 printf("bu\n");
Peeraya 5:60262753a05f 295 checkbcd=0;
Peeraya 5:60262753a05f 296 game.pluslevel();
Peeraya 5:60262753a05f 297 currenthex=0;
Peeraya 5:60262753a05f 298 changehex = 1;
Peeraya 5:60262753a05f 299 display.F();
Peeraya 5:60262753a05f 300 }
Peeraya 5:60262753a05f 301
Peeraya 5:60262753a05f 302 if(gameround==4)
Peeraya 5:60262753a05f 303 {
Peeraya 5:60262753a05f 304 printf("eiei\n");
Peeraya 5:60262753a05f 305 mode=1;
Peeraya 5:60262753a05f 306 state=0;
Peeraya 5:60262753a05f 307 buzzer=0;
Peeraya 5:60262753a05f 308 game.resetlevel();
Peeraya 5:60262753a05f 309 checkbcd=0;
Peeraya 5:60262753a05f 310 currenthex=0;
Peeraya 5:60262753a05f 311 gameround=0;
Peeraya 5:60262753a05f 312 changehex=1;
Peeraya 5:60262753a05f 313 imawake=1;
Peeraya 5:60262753a05f 314 sn=0;
NamSom 4:a61cdd4e7afa 315 }
NamSom 4:a61cdd4e7afa 316 }
NamSom 4:a61cdd4e7afa 317
Peeraya 3:dc88b52d526c 318 }
Peeraya 3:dc88b52d526c 319
Peeraya 5:60262753a05f 320 //printf("%f\n",mea);
Peeraya 5:60262753a05f 321 //if(mea>=0.65){min_a=min_a+1;}
Peeraya 3:dc88b52d526c 322
Peeraya 3:dc88b52d526c 323 }
Peeraya 3:dc88b52d526c 324 }