lcd ล่าสุด 11/12/17 8.41น.

Dependencies:   LCD4884 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "LCD4884.h"
00003 //#include "string.h"
00004 //using namespace std;
00005 LCD4884 object;
00006 AnalogIn analog(A0);
00007 char b[10];
00008 char hour[3];
00009 char min[3];
00010 int state_menu=0,cont=0,state_FM=0,state_foodMa=0,state_weightF=0,state_nextpage=0,state_foodNor=0;
00011 int setT_mode=0,HI_T=0,cont_ST1=0,hour1=0,min1=0,setT_num1=0;
00012 int menu_HI[5]={0,0,0,0,0};
00013 int set_time1[6]={0,0,0,0,0,0};
00014 int fooda_HI=0;
00015 int foodTO_HI=0;
00016 int hisT_HI=0;
00017 int manual_HI[3]={0,0,0};
00018 int mm_count=0;
00019 int foodM_HI[5]={0,0,0,0,0};
00020 int foodM_count=0;
00021 int set_weightF=0,weight_tank=200,weight_pan=400;
00022 int nextpage_HI[3]={0,0,0};
00023 int nextpage_count=0;
00024 int foodN_HI[5]={0,0,0,0,0};
00025 int foodN_count = 0,set_weightNor=0;
00026 char str_set_weightF[5],str_weight_tank[5],str_weight_pan[5];
00027 char day[3],month[3],week[3];
00028 char food_tot[5];
00029 void set_time();
00030 void CST();
00031 void manual_mode();
00032 void food_M();
00033 void next_page();
00034 void setFoodN_HI()
00035 {
00036     switch(foodN_count) {
00037             case 1:
00038                 foodN_HI[0]=1;
00039                 foodN_HI[1]=0;
00040                 foodN_HI[2]=0;
00041                 foodN_HI[3]=0;
00042                 foodN_HI[4]=0;
00043                 break;
00044             case 2:
00045                 foodN_HI[0]=0;
00046                 foodN_HI[1]=1;
00047                 foodN_HI[2]=0;
00048                 foodN_HI[3]=0;
00049                 foodN_HI[4]=0;
00050                 break;
00051             case 3:
00052                 foodN_HI[0]=0;
00053                 foodN_HI[1]=0;
00054                 foodN_HI[2]=1;
00055                 foodN_HI[3]=0;
00056                 foodN_HI[4]=0;
00057                 break;
00058             case 4:
00059                 foodN_HI[0]=0;
00060                 foodN_HI[1]=0;
00061                 foodN_HI[2]=0;
00062                 foodN_HI[3]=1;
00063                 foodN_HI[4]=0;
00064                 break;
00065             case 5:
00066                 foodN_HI[0]=0;
00067                 foodN_HI[1]=0;
00068                 foodN_HI[2]=0;
00069                 foodN_HI[3]=0;
00070                 foodN_HI[4]=1;
00071                 break;
00072     }
00073 }
00074 void set_foodN()
00075 {
00076     float a=analog.read();
00077     if(state_foodNor==0){
00078         object.LCD_clear();
00079         object.LCD_write_string(0,0,"SET_FOOD", MENU_NORMAL);
00080         object.LCD_write_string(0,1,"- 200g",foodN_HI[0]);
00081         object.LCD_write_string(0,2,"- 300g", foodN_HI[1]);
00082         object.LCD_write_string(0,3,"- 400g", foodN_HI[2]);
00083         object.LCD_write_string(0,4,"- 500g", foodN_HI[3]);
00084         object.LCD_write_string(60,5,"back", foodN_HI[4]);
00085         if(a>0.3 && a<0.5){
00086             if(foodN_count==5){
00087                 foodN_count=5;
00088             }else{
00089                 foodN_count++;
00090             }
00091         }else if(a==1){
00092             if(foodN_count==1){
00093                 foodN_count=1;
00094             }else{
00095                 foodN_count--;
00096             }
00097         }else if(a>0.2&&a<0.25){
00098             if(foodN_count==1){
00099                 state_foodNor=1;
00100             }else if(foodN_count==2){
00101                 state_foodNor=2;
00102             }else if(foodN_count==3){
00103                 state_foodNor=3;
00104             }else if(foodN_count==4){
00105                 state_foodNor=4;
00106             }else if(foodN_count==5){               //ปุ่มback
00107                 state_foodNor=5;
00108             }
00109         }
00110     }else if(state_foodNor==1){
00111         set_weightNor=200;             //ตัวแปรเก็บน้ำหนักปกติที่ตั้งค่า
00112         state_nextpage=0;
00113         object.LCD_clear();
00114         next_page();
00115     }else if(state_foodNor==2){
00116         set_weightNor=300;
00117         state_nextpage=0;
00118         object.LCD_clear();
00119         next_page();
00120     }else if(state_foodNor==3){
00121         set_weightNor=400;
00122         state_nextpage=0;
00123         object.LCD_clear();
00124         next_page();
00125     }else if(state_foodNor==4){
00126         set_weightNor=500;
00127         state_nextpage=0;
00128         object.LCD_clear();
00129         next_page();
00130     }else if(state_foodNor==5){
00131         object.LCD_clear();
00132         state_nextpage=0;
00133         next_page();
00134     }setFoodN_HI();
00135 }
00136 void weight_F()
00137 {
00138     float a=analog.read();
00139     if(state_weightF==0){
00140         sprintf(str_weight_tank,"%d",weight_tank);
00141         sprintf(str_weight_pan,"%d",weight_pan);
00142         object.LCD_clear();
00143         object.LCD_write_string(0,0,"READ_WEIGHT", MENU_NORMAL);
00144         object.LCD_write_string(0,2,"tank: ", MENU_NORMAL);
00145         object.LCD_write_string(36,2,str_weight_tank, MENU_NORMAL);
00146         object.LCD_write_string(0,3,"pan: ", MENU_NORMAL);
00147         object.LCD_write_string(30,3,str_weight_pan, MENU_NORMAL);
00148         object.LCD_write_string(60,5,"back", hisT_HI);
00149         if(a>0.3 && a<0.5){
00150             hisT_HI=1;
00151         }else if(a>0.2&&a<0.25){
00152             if(hisT_HI==1){
00153                 state_weightF=1;
00154             }
00155         }    
00156     }else if(state_weightF==1){
00157         object.LCD_clear();
00158         state_FM=0;
00159         manual_mode();
00160     }
00161         
00162 }
00163 void food_MHI()
00164 {
00165     switch(foodM_count) {
00166             case 1:
00167                 foodM_HI[0]=1;
00168                 foodM_HI[1]=0;
00169                 foodM_HI[2]=0;
00170                 foodM_HI[3]=0;
00171                 foodM_HI[4]=0;
00172                 break;
00173             case 2:
00174                 foodM_HI[0]=0;
00175                 foodM_HI[1]=1;
00176                 foodM_HI[2]=0;
00177                 foodM_HI[3]=0;
00178                 foodM_HI[4]=0;
00179                 break;
00180             case 3:
00181                 foodM_HI[0]=0;
00182                 foodM_HI[1]=0;
00183                 foodM_HI[2]=1;
00184                 foodM_HI[3]=0;
00185                 foodM_HI[4]=0;
00186                 break;
00187             case 4:
00188                 foodM_HI[0]=0;
00189                 foodM_HI[1]=0;
00190                 foodM_HI[2]=0;
00191                 foodM_HI[3]=1;
00192                 foodM_HI[4]=0;
00193                 break;
00194             case 5:
00195                 foodM_HI[0]=0;
00196                 foodM_HI[1]=0;
00197                 foodM_HI[2]=0;
00198                 foodM_HI[3]=0;
00199                 foodM_HI[4]=1;
00200                 break;
00201     }
00202 }
00203 void food_M()
00204 { 
00205     float a=analog.read();
00206     if(state_foodMa==0){
00207         object.LCD_clear();
00208         object.LCD_write_string(0,0,"FOOD_MANUAL", MENU_NORMAL);
00209         object.LCD_write_string(0,1,"- 100g",foodM_HI[0]);
00210         object.LCD_write_string(0,2,"- 200g", foodM_HI[1]);
00211         object.LCD_write_string(0,3,"- 300g", foodM_HI[2]);
00212         object.LCD_write_string(0,4,"- 400g", foodM_HI[3]);
00213         object.LCD_write_string(60,5,"back", foodM_HI[4]);
00214         if(a>0.3 && a<0.5){
00215             if(foodM_count==5){
00216                 foodM_count=5;
00217             }else{
00218                 foodM_count++;
00219             }
00220         }else if(a==1){
00221             if(foodM_count==1){
00222                 foodM_count=1;
00223             }else{
00224                 foodM_count--;
00225             }
00226         }else if(a>0.2&&a<0.25){
00227             if(foodM_count==1){
00228                 state_foodMa=1;
00229             }else if(foodM_count==2){
00230                 state_foodMa=2;
00231             }else if(foodM_count==3){
00232                 state_foodMa=3;
00233             }else if(foodM_count==4){
00234                 state_foodMa=4;
00235             }else if(foodM_count==5){               //ปุ่มback
00236                 state_foodMa=5;
00237             }
00238         }
00239     }else if(state_foodMa==1){
00240         set_weightF=100;             //ตัวแปรเก็บน้ำหนักที่ตั้งค่า
00241         state_FM=0;
00242         object.LCD_clear();
00243         manual_mode();
00244     }else if(state_foodMa==2){
00245         set_weightF=200;
00246         state_FM=0;
00247         object.LCD_clear();
00248         manual_mode();
00249     }else if(state_foodMa==3){
00250         set_weightF=300;
00251         state_FM=0;
00252         object.LCD_clear();
00253         manual_mode();
00254     }else if(state_foodMa==4){
00255         set_weightF=400;
00256         state_FM=0;
00257         object.LCD_clear();
00258         manual_mode();
00259     }else if(state_foodMa==5){
00260         object.LCD_clear();
00261         state_FM=0;
00262         manual_mode();
00263     }food_MHI();
00264 }
00265 void mm_HI()
00266 {
00267     switch(mm_count) {
00268             case 1:
00269                 manual_HI[0]=1;
00270                 manual_HI[1]=0;
00271                 manual_HI[2]=0;
00272                 break;
00273             case 2:
00274                 manual_HI[0]=0;
00275                 manual_HI[1]=1;
00276                 manual_HI[2]=0;
00277                 break;
00278             case 3:
00279                 manual_HI[0]=0;
00280                 manual_HI[1]=0;
00281                 manual_HI[2]=1;
00282                 break;
00283     }
00284 }
00285 void manual_mode()
00286 {
00287     float a=analog.read();
00288     if(state_FM==0){
00289         sprintf(str_set_weightF,"%d",set_weightF);
00290         object.LCD_clear();
00291         object.LCD_write_string(0,0,"MANUAL_MODE", MENU_NORMAL);
00292         object.LCD_write_string(58,1,str_set_weightF, MENU_NORMAL);
00293         object.LCD_write_string(0,2,"1.food", manual_HI[0]);
00294         object.LCD_write_string(0,3,"2.read_weight", manual_HI[1]);
00295         object.LCD_write_string(60,5,"back", manual_HI[2]);
00296         if(a>0.3 && a<0.5){
00297             if(mm_count==3){
00298                 mm_count=3;
00299             }else{
00300                 mm_count++;
00301             }
00302         }else if(a==1){
00303             if(mm_count==1){
00304                 mm_count=1;
00305             }else{
00306                 mm_count--;
00307             }
00308         }else if(a>0.2&&a<0.25){
00309             if(mm_count==1){
00310                 state_FM=1;
00311             }else if(mm_count==2){
00312                 state_FM=2;
00313             }else if(mm_count==3){
00314                 state_FM=3;
00315             }
00316         }
00317     }else if(state_FM==1){
00318         object.LCD_clear();
00319         food_M();
00320     }else if(state_FM==2){
00321         object.LCD_clear();
00322         weight_F();
00323     }else if(state_FM==3){
00324         object.LCD_clear();
00325         state_menu = 0;
00326     } mm_HI();
00327    
00328 }
00329 void his_T()
00330 {
00331     float a=analog.read();
00332     object.LCD_clear();
00333     object.LCD_write_string(0,0,"HISTORY_TIME", MENU_NORMAL);
00334     object.LCD_write_string(60,5,"back", hisT_HI);
00335     if(a>0.3 && a<0.5){
00336         hisT_HI=1;
00337     }else if(a>0.2&&a<0.25){
00338         if(hisT_HI==1){
00339             object.LCD_clear();
00340             state_menu = 0;
00341         }
00342     }
00343 }
00344 void food_total()
00345 {
00346     float a=analog.read();
00347     object.LCD_clear();
00348     object.LCD_write_string(0,0,"FOOD_TOTAL", MENU_NORMAL);
00349     object.LCD_write_string(0,2,"total:", MENU_NORMAL);
00350     object.LCD_write_string(42,2,food_tot, MENU_NORMAL);  //charอาหารคงเหลือ
00351     object.LCD_write_string(60,5,"back", foodTO_HI);
00352     if(a>0.3 && a<0.5){
00353         foodTO_HI=1;
00354     }else if(a>0.2&&a<0.25){
00355         if(foodTO_HI==1){
00356             object.LCD_clear();
00357             state_menu = 0;
00358         }
00359     }
00360 }
00361 void food_data()
00362 {
00363     float a=analog.read();
00364     object.LCD_clear();
00365     object.LCD_write_string(0,0,"FOOD_DATA", MENU_NORMAL);
00366     object.LCD_write_string(0,1,"day:", MENU_NORMAL);
00367     object.LCD_write_string(30,1,day, MENU_NORMAL);    //charสถิตวัน
00368     object.LCD_write_string(0,2,"week:", MENU_NORMAL);
00369     object.LCD_write_string(42,2,week, MENU_NORMAL);  //charสถิตสัปดาห์
00370     object.LCD_write_string(0,3,"month:", MENU_NORMAL);
00371     object.LCD_write_string(36,3,month, MENU_NORMAL);  //charสถิตเดือน
00372     object.LCD_write_string(60,5,"back", fooda_HI);
00373     if(a>0.3 && a<0.5){
00374         fooda_HI=1;
00375     }else if(a>0.2&&a<0.25){
00376         if(fooda_HI==1){
00377             object.LCD_clear();
00378             state_menu = 0;
00379         }
00380     }
00381 }
00382 void Time_S()
00383 {
00384     float a=analog.read();
00385     if(cont_ST1==5){
00386         if(a==1){
00387             if(hour1==23){
00388                 hour1=0;
00389             }else{
00390                 hour1++;
00391             }
00392             wait(0.7);
00393         }else if(a>0.3 && a<0.5){
00394             if(hour1==0){
00395                 hour1=0;
00396             }else{
00397                 hour1--;
00398             }
00399             wait(0.7);
00400         } 
00401     }else if(cont_ST1==6){
00402         if(a==1){
00403             if(min1==59){
00404                 min1=0;
00405             }else{
00406                 min1++;
00407             }
00408             wait(0.7);
00409         }else if(a>0.3 && a<0.5){
00410             if(min1==0){
00411                 min1=0;
00412             }else{
00413                 min1--;
00414             }
00415             wait(0.7);
00416         }set_time();
00417     }
00418 }
00419 void CST()
00420 {
00421     float a=analog.read();
00422     switch(cont_ST1) {
00423             case 1:
00424                 set_time1[0]=1;
00425                 set_time1[1]=1;
00426                 set_time1[2]=0;
00427                 set_time1[3]=0;
00428                 set_time1[4]=0;
00429                 set_time1[5]=0;
00430                 break;
00431             case 2:
00432                 set_time1[0]=0;
00433                 set_time1[1]=0;
00434                 set_time1[2]=1;
00435                 set_time1[3]=1;
00436                 set_time1[4]=0;
00437                 set_time1[5]=0;
00438                 break;
00439             case 3:
00440                 set_time1[0]=0;
00441                 set_time1[1]=0;
00442                 set_time1[2]=0;
00443                 set_time1[3]=0;
00444                 set_time1[4]=1;
00445                 set_time1[5]=0;
00446                 break;
00447             case 4:
00448                 set_time1[0]=0;
00449                 set_time1[1]=0;
00450                 set_time1[2]=0;
00451                 set_time1[3]=0;
00452                 set_time1[4]=0;
00453                 set_time1[5]=1;
00454                 break;
00455             case 5:
00456                 set_time1[0]=0;
00457                 set_time1[1]=1;
00458                 set_time1[2]=0;
00459                 set_time1[3]=0;
00460                 set_time1[4]=0;
00461                 set_time1[5]=0;
00462                 break;
00463             case 6:
00464                 set_time1[0]=0;
00465                 set_time1[1]=0;
00466                 set_time1[2]=0;
00467                 set_time1[3]=1;
00468                 set_time1[4]=0;
00469                 set_time1[5]=0;
00470                 break;
00471     }
00472 }
00473 void set_time()
00474 {
00475     float a=analog.read();
00476     sprintf(hour,"%d",hour1);
00477     sprintf(min,"%d",min1);
00478     if(setT_mode==0){
00479         object.LCD_clear();
00480         object.LCD_write_string(0,0,"SET_TIME", MENU_NORMAL);
00481         object.LCD_write_string(0,2,"hr:", set_time1[0]);
00482         object.LCD_write_string(30,2,hour, set_time1[1]);
00483         object.LCD_write_string(0,3,"min:", set_time1[2]);
00484         object.LCD_write_string(36,3,min, set_time1[3]);
00485         object.LCD_write_string(0,5,"back", set_time1[4]);
00486         object.LCD_write_string(60,5,"OK", set_time1[5]);
00487         if(a>0.3 && a<0.5){
00488             if(cont_ST1<=4){
00489                 if(cont_ST1==3){
00490                     cont_ST1=3;
00491                 }else if(cont_ST1==4){
00492                     cont_ST1=4;
00493                 }else{
00494                     cont_ST1++;
00495                 }
00496             }
00497         }else if(a>0.73&&a<0.75){
00498             if(cont_ST1==3){
00499                 cont_ST1=4;
00500             }
00501         }else if(a>0.2&&a<0.25){
00502             if(cont_ST1==3){
00503                 state_menu = 0;
00504             }else if(cont_ST1==4){
00505                 setT_mode=1;             //จำเวลา
00506                 state_menu = 0;
00507             }else if(cont_ST1==1){
00508                 cont_ST1=5;
00509             }else if(cont_ST1==2) {
00510                 cont_ST1=6;
00511             }else if(cont_ST1==5){
00512                 cont_ST1=1;
00513             }else if(cont_ST1==6){
00514                 cont_ST1=2;
00515             }
00516         }else if(a==1){
00517             if(cont_ST1==3){
00518                 cont_ST1=2;
00519             }else if(cont_ST1==4){
00520                 cont_ST1=2;
00521             }else if(cont_ST1==2){
00522                 cont_ST1=1;
00523             }
00524         }else if(a>0&&a<0.1){
00525             if(cont_ST1==4){
00526                 cont_ST1=3;
00527             }
00528         }
00529         if(cont_ST1==5){
00530             Time_S();
00531         }
00532         CST();
00533     } 
00534 }
00535 void next_page_HI()
00536 {
00537     switch(nextpage_count) {
00538             case 1:
00539                 nextpage_HI[0]=1;
00540                 nextpage_HI[1]=0;
00541                 nextpage_HI[2]=0;
00542                 break;
00543             case 2:
00544                 nextpage_HI[0]=0;
00545                 nextpage_HI[1]=1;
00546                 nextpage_HI[2]=0;
00547                 break;
00548             case 3:
00549                 nextpage_HI[0]=0;
00550                 nextpage_HI[1]=0;
00551                 nextpage_HI[2]=1;
00552                 break;
00553     }
00554 }
00555 void next_page()
00556 {
00557     float a=analog.read();
00558     if(state_nextpage==0){
00559         object.LCD_clear();
00560         object.LCD_write_string(0,0,"5.MANUAL_MODE", nextpage_HI[0]);
00561         object.LCD_write_string(0,1,"6.SET_FOOD", nextpage_HI[1]);
00562         object.LCD_write_string(60,5,"back", nextpage_HI[2]);
00563         if(a>0.3 && a<0.5){
00564             if(nextpage_count==3){
00565                 nextpage_count=3;
00566             }else{
00567                 nextpage_count++;
00568             }
00569         }else if(a==1){
00570             if(nextpage_count==1){
00571                 nextpage_count=1;
00572             }else{
00573                 nextpage_count--;
00574             }
00575         }else if(a>0.2&&a<0.25){
00576             if(nextpage_count==1){
00577                 state_nextpage=1;
00578             }else if(nextpage_count==2){
00579                 state_nextpage=2;
00580             }else if(nextpage_count==3){
00581                 state_nextpage=3;
00582             }
00583         }
00584     }else if(state_nextpage==1){
00585         manual_mode();
00586     }else if(state_nextpage==2){
00587         set_foodN();
00588     }else if(state_nextpage==3){
00589         object.LCD_clear();
00590         state_menu = 0;
00591     }next_page_HI();
00592     
00593 }
00594 void choose_menu()
00595 {
00596     float a=analog.read();
00597     switch(cont) {
00598             case 0:
00599                 menu_HI[0]=1;
00600                 menu_HI[1]=0;
00601                 menu_HI[2]=0;
00602                 menu_HI[3]=0;
00603                 menu_HI[4]=0;
00604                 break;
00605             case 1:
00606                 menu_HI[0]=0;
00607                 menu_HI[1]=1;
00608                 menu_HI[2]=0;
00609                 menu_HI[3]=0;
00610                 menu_HI[4]=0;
00611                 break;
00612             case 2:
00613                 menu_HI[0]=0;
00614                 menu_HI[1]=0;
00615                 menu_HI[2]=1;
00616                 menu_HI[3]=0;
00617                 menu_HI[4]=0;
00618                 break;
00619             case 3:
00620                 menu_HI[0]=0;
00621                 menu_HI[1]=0;
00622                 menu_HI[2]=0;
00623                 menu_HI[3]=1;
00624                 menu_HI[4]=0;
00625                 break;
00626             case 4:
00627                 menu_HI[0]=0;
00628                 menu_HI[1]=0;
00629                 menu_HI[2]=0;
00630                 menu_HI[3]=0;
00631                 menu_HI[4]=1;
00632                 break;
00633     }
00634 }
00635 int main()
00636 {
00637     object.LCD_init();
00638     while(1) {
00639         if(state_menu == 0){
00640             object.LCD_clear();
00641             float a=analog.read();
00642             sprintf(b,"%f",a);
00643             object.LCD_write_string(36,0,b, MENU_NORMAL);
00644             object.LCD_write_string(0,0,"MENU", MENU_NORMAL);
00645             object.LCD_write_string(0,1,"1.SET_TIME", menu_HI[0]);
00646             object.LCD_write_string(0,2,"2.FOOD_DATA", menu_HI[1]);
00647             object.LCD_write_string(0,3,"3.FOOD_TOTAL", menu_HI[2]);
00648             object.LCD_write_string(0,4,"4.HIS_T", menu_HI[3]);
00649             //object.LCD_write_string(0,5,"5.MANUAL_MODE", menu_HI[4]);
00650             object.LCD_write_string(60,5,"next", menu_HI[4]);
00651             if( a>0.3 && a<0.5){
00652                 if(cont==4){
00653                     cont=0;
00654                 }else{
00655                     cont++;
00656                 }
00657                 choose_menu();
00658             }else if(a==1){
00659                 if(cont==-1){
00660                     cont=4;    
00661                 }else{
00662                     cont--;
00663                 }
00664                 choose_menu();
00665             }else if(a>0.2&&a<0.25){
00666                 if(cont==0){
00667                     state_menu=1;
00668                 }else if(cont==1){
00669                     state_menu=2;
00670                 }else if(cont==2){
00671                     state_menu=3;
00672                 }else if(cont==3){
00673                     state_menu=4;
00674                 }else if(cont==4){
00675                     state_menu=5;
00676                 } 
00677             }
00678         }else if(state_menu==1){
00679             set_time();
00680         }else if(state_menu==2){
00681             food_data();
00682         }else if(state_menu==3){
00683             food_total();
00684         }else if(state_menu==4){
00685             his_T();
00686         }else if(state_menu=5){
00687             next_page();
00688         }wait(0.2);
00689     }   
00690 }