Joshua Liu / Mbed 2 deprecated ControlBoxCode

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 //Serial pc(USBTX, USBRX);
00003 Serial lcd(p28, p27);
00004 
00005 // V+/V- outputs cylinder down
00006 DigitalOut out1(p7);
00007 DigitalOut out2(p8);
00008 DigitalOut out3(p9);
00009 DigitalOut out4(p10);
00010 // V+/V- outputs cylinder up
00011 DigitalOut out5(p11);
00012 DigitalOut out6(p12);
00013 DigitalOut out7(p13);
00014 DigitalOut out8(p14);
00015 // Four push buttons left to right
00016 DigitalIn  pushOne(p21);
00017 DigitalIn  pushTwo(p22);
00018 DigitalIn  pushThree(p23);
00019 DigitalIn  pushFour(p24);
00020 // Run switch where p26 will be kept high
00021 // Can probably connect it to the 3.3V out
00022 DigitalIn  runIn(p25);
00023 DigitalOut runOut(p26);
00024 
00025 float upTime;
00026 float downTime;
00027 float waitTime;
00028 unsigned int count = 0;
00029 typedef char * string;
00030 string strs[5] = {
00031     "Up Time",          // 0
00032     "Down Time",        // 1
00033     "Wait Time",        // 2
00034     "Target Count",     // 3
00035     "Reset Counter"     // 4
00036 };
00037 enum ValveMode {
00038     UP,
00039     DOWN
00040 };
00041 
00042 
00043 
00044 
00045 
00046 void clearLCD()
00047 {
00048     lcd.printf("%c%c",0xFE,0x01);
00049 }
00050 void setLCDpos(int xpos, int ypos)
00051 {
00052     int pos = 0x80;
00053     switch (xpos) {
00054         case 1:
00055             pos +=0;
00056             break;
00057         case 2:
00058             pos +=64;
00059             break;
00060         case 3:
00061             pos +=20;
00062             break;
00063         case 4:
00064             pos +=84;
00065             break;
00066     }
00067     pos += (ypos-1);
00068     lcd.printf("%c%c",0xFE,pos);
00069 }
00070 void setBrightness(int lev)
00071 {
00072     lcd.printf("%c%c",0x7C,lev+127);
00073     wait(.1);
00074 }
00075 
00076 
00077 
00078 void setValve(ValveMode dir, int val)
00079 {
00080     switch(dir) {
00081         case DOWN:
00082             out1 = val;
00083             out2 = val;
00084             out3 = val;
00085             out4 = val;
00086             break;
00087         case UP:
00088             out5 = val;
00089             out6 = val;
00090             out7 = val;
00091             out8 = val;
00092             break;
00093 
00094     }
00095 }
00096 
00097 
00098 /*void lcdShowParameters(int delay, int cycles)
00099 {
00100     clearLCD();
00101     setLCDpos(1,1);
00102     lcd.printf(strs[strPos]);
00103     setLCDpos(2,1);
00104     if (strPos<=2) {
00105         lcd.printf("%f",times[strPos]);
00106     } else if(strPos==3) {
00107         lcd.printf("%d",target);
00108     } else if(strPos==4) {
00109         lcd.printf("Y/N?");
00110     }
00111 }
00112 
00113 
00114 void lcdConfirmStart()
00115 {
00116     clearLCD();
00117     setLCDpos(1,1);
00118     lcd.printf(strs[strPos]);
00119     setLCDpos(2,1);
00120     if (strPos<=2) {
00121         lcd.printf("%f",times[strPos]);
00122     } else if(strPos==3) {
00123         lcd.printf("%d",target);
00124     } else if(strPos==4) {
00125         lcd.printf("Y/N?");
00126     }
00127 }
00128 */
00129 
00130 void printCount (int count)
00131 {
00132     clearLCD();
00133     setLCDpos(1,1);
00134     lcd.printf("CURRENT COUNT IS");
00135     setLCDpos(2,1);
00136     lcd.printf("%d",count);
00137 }
00138 
00139 
00140 int main()
00141 {
00142     unsigned int route1 = 0;
00143     //unsigned int route2 = 0;
00144     float times[3];
00145     times[0] = 1.0;
00146     times[1] = 1.0;
00147     times[2] = 0.2;
00148     int target = 2000;
00149     int strPos = 0;
00150     Timer timer1;
00151 
00152     wait(1);
00153     clearLCD();
00154     //setLCDpos(1,1);
00155     //lcd.printf("Current count is:");
00156     //setLCDpos(2,1);
00157     //lcd.printf("%d",0);
00158     runOut = 1;
00159     while (1) {
00160         if (!runIn) {
00161             if (!route1) {
00162                 clearLCD();
00163                 route1 = 1;
00164                 timer1.start();
00165             }
00166 
00167             else {
00168                 if(pushOne && timer1.read_ms()>500) {
00169                     //Go Left
00170                     if(strPos<1) {
00171                         strPos=4;
00172                     } else {
00173                         strPos--;
00174                     }
00175 
00176                     clearLCD();
00177                     setLCDpos(1,1);
00178                     lcd.printf(strs[strPos]);
00179                     setLCDpos(2,1);
00180                     if (strPos<=2) {
00181                         lcd.printf("%f",times[strPos]);
00182                     } else if(strPos==3) {
00183                         lcd.printf("%d",target);
00184                     } else if(strPos==4) {
00185                         lcd.printf("Y/N?");
00186                     }
00187                     timer1.reset();
00188                 } else if(pushTwo && timer1.read_ms()>100) {
00189                     //Increment
00190                     clearLCD();
00191                     setLCDpos(1,1);
00192                     lcd.printf(strs[strPos]);
00193                     setLCDpos(2,1);
00194                     if(strPos<=2) {
00195                         times[strPos]=times[strPos]+0.1;
00196                         lcd.printf("%f",times[strPos]);
00197                     } else if(strPos==3) {
00198                         target=target+100;
00199                         lcd.printf("%d",target);
00200                     } else if(strPos==4) {
00201                         setLCDpos(2,1);
00202                         lcd.printf("YES");
00203                         count=0;
00204                     }
00205                     timer1.reset();
00206                 } else if(pushThree && timer1.read_ms()>100) {
00207                     //Decrement
00208                     clearLCD();
00209                     setLCDpos(1,1);
00210                     lcd.printf(strs[strPos]);
00211                     setLCDpos(2,1);
00212                     if(strPos<=2) {
00213                         times[strPos]=times[strPos]- 0.1;
00214                         lcd.printf("%f",times[strPos]);
00215                     } else if(strPos==3) {
00216                         target=target-100;
00217                         lcd.printf("%d",target);
00218                     } else if(strPos==4) {
00219                         lcd.printf("NO");
00220                     }
00221                     timer1.reset();
00222                 } else if(pushFour && timer1.read_ms()>150) {
00223                     if(strPos>3) {
00224                         strPos=0;
00225                     } else {
00226                         strPos++;
00227                     }
00228 
00229                     clearLCD();
00230                     setLCDpos(1,1);
00231                     lcd.printf(strs[strPos]);
00232                     setLCDpos(2,1);
00233                     if (strPos<=2) {
00234                         lcd.printf("%f",times[strPos]);
00235                     } else if(strPos==3) {
00236                         lcd.printf("%d",target);
00237                     } else if(strPos==4) {
00238                         lcd.printf("Y/N?");
00239                     }
00240                     timer1.reset();
00241                 }
00242             }
00243         } else if (runIn && count<=target) {
00244             route1=0;
00245             upTime = times[0];
00246             downTime = times[1];
00247             waitTime = times[2];
00248 
00249             setValve(UP, 1);
00250             wait(upTime);
00251             setValve(UP, 0);
00252             wait(waitTime);
00253             setValve(DOWN, 1);
00254             wait(downTime);
00255             setValve(DOWN,0);
00256             wait(waitTime);
00257             
00258             count++;
00259             printCount(count);
00260         } else if (count>target) {
00261             clearLCD();
00262             setLCDpos(1,1);
00263             lcd.printf("TARGET REACHED");
00264             setLCDpos(2,1);
00265             lcd.printf("%d",target);
00266             wait(0.1);
00267         }
00268     }
00269 }
00270