Ozan Sanci / Mbed 2 deprecated TPJ_prjct_Ozan

Dependencies:   N5110 Terminal mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Main.cpp Source File

Main.cpp

00001 /*
00002                   TPJ project 
00003 Automated Solar Power Harvesting System 
00004 
00005 It is created  by Ozan SANCI
00006 
00007 Description: This program controls charging operation of SLA battery(6V-4A) with 12V 2W Solar Panel and  controls:
00008 
00009     -load On/OFF
00010     -ADC real-time voltage on both solar and battery,
00011     -power consumption with ACS712 Current Sensor (-5 to 5A)
00012     -interfacing with user via Nokia 5110 screen as well as HC-05 Bluetooth module. 
00013     -third part Android application, “Bluetooth Electronic” is used. 
00014 
00015 Additional note: The charging algorithms for the final demonstration have been changed slightly: 
00016     Solar Panel and battery voltage thresholds, 6.5V, and 6.4V are changed to 6V. 
00017     Because, instead of solar panel, A dc source is used since solar panel is not able to provide enough power in the Lab.  
00018 
00019 */
00020 
00021 #include"mbed.h"
00022 #include "Terminal.h"
00023 #include "N5110.h"
00024 #include "string"
00025 
00026 /*********************************Pin decleration*************/
00027 Serial pc(USBTX, USBRX);
00028 Serial blue(PTC15,PTC14); 
00029 AnalogIn solar_voltage(A0);
00030 AnalogIn battery_voltage(A1);
00031 AnalogIn load_current(A2);
00032 DigitalOut solar_gate(D5);
00033 DigitalOut load_gate(D4);
00034 DigitalIn  pushBtnLeft(D3);
00035 DigitalIn  pushBtnRight(D2);
00036 DigitalOut blueLed(D0);
00037 /*******************************************************************/
00038 using std::string;
00039 /****************************Nokia5110 Pin Decleration*********/
00040  //SCE,RST,D/C,MOSI,SCLK,LED
00041 N5110 lcd(PTA0,PTC2,PTC4,PTD2,PTD1,PTC3);
00042 /*****************************************************************/      
00043 #define solargate 
00044 #define refVolt 3300
00045 float solarVoltage;
00046 unsigned char sGate = 'A'; //Initially gate is open
00047 unsigned char bGate = 'B';//Initially gate is open
00048 
00049  string gateStat= "LR255G0B0"; // INTIALLY IT IS RED, GREEN IS LR0G255B0
00050 float batteryVoltage;
00051 /********************************************************Function Prototypes************/
00052 
00053 float fnc_solarPanel(void);
00054 bool fnc_chargingstat(float i);
00055 float fnc_batteryVolt(void);
00056 bool fnc_loadstat(float i);
00057 float fnc_load_current(void);
00058 
00059 /************************************************************M A I N************************/
00060 int main(){
00061 bool loadStatus, chargingStatus,screenOne,screenTwo;
00062  solarVoltage = 0.0;
00063  batteryVoltage = 0.0;
00064 float loadCurrent = 0.0;
00065 float usagePower = 0.0;
00066 float powerLeftinW = 0.0;
00067 int powerLeftinH = 0.0;
00068 char outputs[10];//it is used for conversion from solar volt (float) to char in order to print as string. 
00069 char outputb[10];//it is used for conversion from battery charge(float) to char in order to print as string.
00070 char outputc[10];//it is used for conversion from current charge(float) to char in order to print as string. 
00071 char blueInput;
00072 int left, right;
00073 lcd.init();
00074 lcd.normalMode();
00075 lcd.setBrightness(1.0); 
00076 
00077 loadStatus  = false;
00078 chargingStatus = false;
00079 screenOne = true;
00080 screenTwo = false;
00081 right = 1;
00082 left = 0;
00083 blueLed = 0;
00084     while(1){
00085        
00086        if (blue.readable()) {
00087             blueLed = 1;
00088            
00089             blueInput = blue.getc();
00090            switch(blueInput){
00091                case 'B' : bGate = 'B'; break;
00092                case 'b' : bGate = 'b'; break;
00093                case 'A': sGate = 'A';break;
00094                case 'a' : sGate = 'a'; break;
00095                
00096                }}
00097         
00098 
00099             
00100         
00101         
00102         
00103 right = pushBtnRight.read();
00104 left = pushBtnLeft.read();
00105 if(right){screenOne = false; screenTwo = true; }
00106 if(left){screenOne=true;screenTwo=false;}
00107 if(screenOne)
00108 {
00109 
00110 
00111 lcd.clear();
00112 solarVoltage=fnc_solarPanel();//solar voltge 
00113             sprintf(outputs,"SolarV: %.2fV",solarVoltage); 
00114             lcd.printString(outputs,0,0);
00115     //lcd.refresh();
00116     blue.printf("*S%s*",outputs);
00117    
00118 
00119 chargingStatus = fnc_chargingstat(solarVoltage); // return true if volt bigger than 6V else false and sets the gate
00120     if (chargingStatus){lcd.printString("Charging: ON ",0,1);/*lcd.refresh();*/blue.printf("*LR0G255B0*");}
00121         else {lcd.printString("Charging: OFF",0,1);blue.printf("*LR255G0B0*");}
00122      /*lcd.refresh();*/
00123 batteryVoltage = fnc_batteryVolt(); // return battery volt as float
00124             sprintf(outputb,"Battery: %.2fV",batteryVoltage); /*lcd.refresh();*/
00125             lcd.printString(outputb,0,2);
00126   /*lcd.refresh();*/    
00127   blue.printf("*B%s*",outputb);  
00128 
00129 loadStatus = fnc_loadstat(batteryVoltage); // return true if volt higger than 6v else fals end sets the gate
00130     if (loadStatus){lcd.printString("Status: ON  ",0,3);/*lcd.refresh();*/blue.printf("*MR0G255B0*");}
00131         else {lcd.printString("Status: OFF",0,3);blue.printf("*MR255G0B0*");}
00132 /*lcd.refresh();*/
00133 fnc_load_current(); // only return current as float and power calculations are required 
00134 lcd.printString("Power Usage =>",0,5);
00135     lcd.refresh();
00136     fnc_load_current();
00137 loadCurrent = fnc_load_current();
00138       sprintf(outputc,"Load:%.2fmA",loadCurrent); 
00139             
00140             blue.printf("*E%s*",outputc);
00141       usagePower = loadCurrent * batteryVoltage;
00142       
00143       sprintf(outputc,"L-Power:%.2fmW",usagePower); 
00144            
00145             blue.printf("*I%s*",outputc);
00146       powerLeftinW = 24 - usagePower;
00147       if(batteryVoltage <= 5) powerLeftinW=0.0;
00148       sprintf(outputc,"B-Power:%.1fkW",powerLeftinW);
00149       blue.printf("*I%s*",outputc);
00150             
00151        powerLeftinH = 24 / usagePower;
00152        if(batteryVoltage <= 5) powerLeftinH=0.0;
00153        sprintf(outputc,"B-Hour:%dh", powerLeftinH); 
00154            
00155             blue.printf("*N%s*",outputc);
00156        
00157     wait_ms(500);
00158     }//if screenone
00159     
00160     if(screenTwo)
00161     { 
00162     lcd.clear();
00163    
00164 solarVoltage=fnc_solarPanel();//solar voltge 
00165             sprintf(outputs,"SolarV: %.2fV",solarVoltage); 
00166             
00167     blue.printf("*S%s*",outputs);
00168    
00169 
00170 chargingStatus = fnc_chargingstat(solarVoltage); // return true if volt bigger than 6V else false and sets the gate
00171     if (chargingStatus){blue.printf("*LR0G255B0*");}
00172         else {blue.printf("*LR255G0B0*");}
00173     
00174 batteryVoltage = fnc_batteryVolt(); // return battery volt as float
00175             sprintf(outputb,"Battery: %.2fV",batteryVoltage); 
00176          
00177       
00178   blue.printf("*B%s*",outputb);  
00179 
00180 loadStatus = fnc_loadstat(batteryVoltage); // return true if volt higger than 6v else fals end sets the gate
00181     if (loadStatus){blue.printf("*MR0G255B0*");}
00182         else {blue.printf("*MR255G0B0*");}
00183 
00184 fnc_load_current(); // only return current as float and power calculations are required 
00185 //*******************************
00186 
00187       loadCurrent = fnc_load_current();
00188       sprintf(outputc,"Load:%.2fmA",loadCurrent); /*lcd.refresh();*/
00189             lcd.printString(outputc,0,0);/*lcd.refresh();*/
00190             blue.printf("*E%s*",outputc);
00191       usagePower = loadCurrent * batteryVoltage;
00192      
00193       sprintf(outputc,"L-Power:%.2fmW",usagePower); /*lcd.refresh();*/
00194             lcd.printString(outputc,0,1);/*lcd.refresh();*/
00195             blue.printf("*I%s*",outputc);
00196       powerLeftinW = 24 - usagePower;
00197        if(batteryVoltage <= 5) powerLeftinW=0.0;
00198       sprintf(outputc,"B-Power:%.1fkW",powerLeftinW); /*lcd.refresh();*/
00199       
00200       blue.printf("*I%s*",outputc);
00201         lcd.printString(outputc,0,2);/*lcd.refresh();*/
00202        powerLeftinH = 24 / usagePower;
00203         if(batteryVoltage <= 5) powerLeftinH=0.0;
00204        sprintf(outputc,"B-Hour:%dh", powerLeftinH); /*lcd.refresh();*/
00205             lcd.printString(outputc,0,3);/*lcd.refresh();*/
00206             blue.printf("*N%s*",outputc);
00207        lcd.printString("",0,1);/*lcd.refresh();*/
00208        lcd.printString("<= Charger",0,5);
00209        lcd.refresh();
00210      wait_ms(500);
00211         }// if screentwo
00212 
00213     }
00214  }
00215  /******************************************E N D   M A I N*************************/
00216 
00217 
00218 
00219  /******************************************Function Definations********************/
00220  
00221  float fnc_solarPanel(void)//Solar Panel Voltage at A0 in -------------------------------------------
00222  {
00223          float solarVolt = ((solar_voltage.read() * (refVolt/1000.0))*2.63);    
00224          return solarVolt;  
00225  }
00226    //-------------------------------------------------------------------------------------------------------------
00227      
00228      
00229  bool fnc_chargingstat(float i)//it returns true if voltage on solar panel is enough. 
00230  {
00231         if (i<6|| sGate =='a' ) 
00232         {
00233             solar_gate = 0; 
00234             gateStat = "LR255GOBO";
00235          //   blue.printf("*OR255G0B0*");
00236         
00237         return false;  
00238         }
00239         else {
00240           // blue.printf("*OR0G255B0*");
00241 
00242             gateStat= "LROG255BO";
00243          solar_gate = 1; return true;}
00244          
00245  }
00246       //------------------------------------------------------------------------------------------------------------
00247  float fnc_batteryVolt(void) //Returns battery volage as float
00248  {
00249         batteryVoltage = ((battery_voltage.read()*(refVolt/1000.0))*3.7789);
00250         return batteryVoltage;  
00251  }
00252        //--------------------------------------------------------------------------------------------------
00253 
00254  bool fnc_loadstat(float i)
00255  {  //bool stat; 
00256          
00257         if (i <6 ){ blue.printf("*OR255G0B0*");  return false;  }
00258         else{  blue.printf("*OR0G255B0*"); return true;} 
00259  
00260 // return stat; 
00261  }
00262     
00263  //-----------------------------------------------------------------------------------------------------------------------
00264     float fnc_load_current(void)
00265  {                                                
00266        float loadcurrent = load_current.read();
00267        return loadcurrent; 
00268  }  
00269  
00270  //--------------------------------------------------------------------------------------------------
00271