Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BMP180 N5110 mbed
main.cpp
00001 /** 00002 @file main.cpp 00003 @brief code implementation 00004 */ 00005 00006 #include "main.h" 00007 #include "mbed.h" 00008 #include "BMP180.h" 00009 #include "N5110.h" 00010 #include "PowerControl/PowerControl.h" 00011 #include "PowerControl/EthernetPowerControl.h" 00012 00013 /** timer associated with temperature */ 00014 void timerTExpired(){ 00015 timerTFlag = 1; 00016 } 00017 /** timer associated with pressure */ 00018 void timerPExpired(){ 00019 timerPFlag = 1; 00020 } 00021 /** timer associated with error */ 00022 void timerEExpired(){ 00023 timerEFlag = 1; 00024 } 00025 /** timer associated with light */ 00026 void timerLExpired(){ 00027 timerLFlag = 1; 00028 } 00029 /** function to set flag to 1 when button pressed */ 00030 void buttonPressed(){ 00031 buttonFlag=1; 00032 } 00033 /** function called when error occurs */ 00034 void error(){ 00035 timerE.attach(&timerEExpired,2.0); 00036 while(1){ 00037 if (timerEFlag){ 00038 timerEFlag=0; 00039 buzzer.period(0.01); 00040 buzzer=0.5; 00041 led=1; 00042 wait(1.0); 00043 led=0; 00044 } 00045 } 00046 } 00047 /** function to write data to file */ 00048 void writeDataToFile(int data) { 00049 leds=3; //turn on LEDs for feedback 00050 FILE *fp=fopen("/local/log.txt","a"); //open log.txt for appending 00051 //if the file doesn't exist it is created, if it exists, data is appended to the end 00052 fprintf(fp,"Counter=%i\n",data); //print string to file 00053 fclose(fp); //close file 00054 leds=0; //turn off LEDs to signify file access has finished 00055 } 00056 /** function to call the temperature */ 00057 void callTemp(){ 00058 measurement(); 00059 clearCells(); 00060 lcd.printString("Menu",0,5); 00061 char bufferT[14]; 00062 int length=sprintf(bufferT,"T = %.2f C",temperature); 00063 if (length<=14){ 00064 lcd.printString(bufferT,10,2); 00065 } 00066 if (temperature>30){ 00067 error(); 00068 } 00069 } 00070 /** function to call pressure */ 00071 void callPress(){ 00072 measurement(); 00073 clearCells(); 00074 lcd.printString("Menu",0,5); 00075 char bufferP[14]; 00076 int length=sprintf(bufferP,"P = %.2f mb",pressure); 00077 if (length<=14){ 00078 lcd.printString(bufferP,0,2); 00079 } 00080 if (pressure<337){ 00081 error(); 00082 } 00083 } 00084 /** function to call light */ 00085 void callLight(){ 00086 float light = ((1-LDR)*100); 00087 clearCells(); 00088 lcd.printString("Menu",0,5); 00089 char bufferL[14]; 00090 int length=sprintf(bufferL,"L = %.2f %%",light); 00091 if (length<=14){ 00092 lcd.printString(bufferL,10,2); 00093 } 00094 if (light>90){ 00095 error(); 00096 } 00097 } 00098 /** function to reads temperature */ 00099 void readTemp(){ 00100 callT.attach(&callTemp,0.1); 00101 timerT.attach(&timerTExpired,60); 00102 while(1){ 00103 if (timerTFlag){ 00104 timerTFlag=0; 00105 measurement(); 00106 clearCells(); 00107 lcd.printString("Menu",0,5); 00108 char bufferT[14]; 00109 int length=sprintf(bufferT,"T = %.2f C",temperature); 00110 writeDataToFile(counter); //write current value to disk 00111 counter++; //increment counter 00112 if (length<=14){ 00113 lcd.printString(bufferT,10,2); 00114 } 00115 if (temperature>30){ 00116 error(); 00117 } 00118 } 00119 if (buttonFlag){ 00120 buttonFlag=0; 00121 menu(); 00122 } 00123 } 00124 } 00125 /** function to read pressure */ 00126 void readPress(){ 00127 callP.attach(&callPress,0.1); 00128 timerP.attach(&timerPExpired,1800); 00129 while(1){ 00130 if (timerPFlag){ 00131 timerPFlag=0; 00132 measurement(); 00133 clearCells(); 00134 lcd.printString("Menu",0,5); 00135 char bufferP[14]; 00136 int length=sprintf(bufferP,"P = %.2f mb",pressure); 00137 writeDataToFile(counter); //write current value to disk 00138 counter++; //increment counter 00139 if (length<=14){ 00140 lcd.printString(bufferP,0,2); 00141 } 00142 if (pressure<337){ 00143 error(); 00144 } 00145 } 00146 if (buttonFlag){ 00147 buttonFlag=0; 00148 menu(); 00149 } 00150 } 00151 } 00152 /** function to read light */ 00153 void readLight(){ 00154 callL.attach(&callLight,0.1); 00155 timerL.attach(&timerLExpired,60); 00156 while(1){ 00157 if (timerLFlag){ 00158 timerLFlag=0; 00159 clearCells(); 00160 float light = ((1-LDR)*100); 00161 lcd.printString("Menu",0,5); 00162 char bufferL[14]; 00163 int length=sprintf(bufferL,"L = %.2f %%",light); 00164 writeDataToFile(counter); //write current value to disk 00165 counter++; //increment counter 00166 if (length<=14){ 00167 lcd.printString(bufferL,10,2); 00168 } 00169 if (light>90){ 00170 error(); 00171 } 00172 } 00173 if (buttonFlag){ 00174 buttonFlag=0; 00175 menu(); 00176 } 00177 } 00178 } 00179 /** function to take BMP180 meaasurement */ 00180 void measurement(){ 00181 Measurement measurement; 00182 measurement=bmp180.readValues(); 00183 temperature=measurement.temperature; 00184 pressure=measurement.pressure; 00185 } 00186 /** function to plot temperature graph */ 00187 void tempGraph(){ 00188 timerT.attach(&timerTExpired,2); 00189 int j=0; //start graph on left hand side of screen 00190 while(1){ 00191 lcd.printString("Menu",0,5); 00192 if (timerTFlag){ 00193 timerTFlag=0; 00194 clearCells(); 00195 float tempArray[84]; //create array of temperature values 00196 measurement(); //read in the measured values of temperature 00197 tempArray[j]=(temperature/47); 00198 j++; //add one to j so that the next point plotted moves across the screen by one pixel 00199 lcd.plotArray(tempArray); //plot the array 00200 writeDataToFile(counter); //write current value to disk 00201 counter++; //increment counter 00202 wait(0.1); //wait one second before plotting the next point 00203 if (j>83){ 00204 j=0; 00205 } 00206 if (temperature>30){ 00207 error(); 00208 } 00209 } 00210 if (buttonFlag){ 00211 buttonFlag=0; 00212 menu(); 00213 } 00214 } 00215 } 00216 /** function to plot pressure graph */ 00217 void pressGraph(){ 00218 timerP.attach(&timerPExpired,2); 00219 int j=0; 00220 while(1){ 00221 lcd.printString("Menu",0,5); 00222 if (timerPFlag){ 00223 timerPFlag=0; 00224 clearCells(); 00225 float pressArray[84]; 00226 measurement(); 00227 pressArray[j]=(pressure/1100); 00228 j++; 00229 lcd.plotArray(pressArray); 00230 writeDataToFile(counter); //write current value to disk 00231 counter++; //increment counter 00232 wait(0.1); 00233 if (j>83){ 00234 j=0; 00235 } 00236 if (pressure<337){ 00237 error(); 00238 } 00239 } 00240 if (buttonFlag){ 00241 buttonFlag=0; 00242 menu(); 00243 } 00244 } 00245 } 00246 /** function to plot light graph */ 00247 void lightGraph(){ 00248 timerL.attach(&timerLExpired,2); 00249 int j=0; 00250 while(1){ 00251 lcd.printString("Menu",0,5); 00252 if (timerLFlag){ 00253 timerLFlag=0; 00254 clearCells(); 00255 float lightArray[84]; 00256 float light = ((1-LDR)*100); 00257 lightArray[j]=((light/100)+0.2); 00258 j++; 00259 lcd.plotArray(lightArray); 00260 writeDataToFile(counter); //write current value to disk 00261 counter++; //increment counter 00262 wait(0.1); 00263 if (j>83){ 00264 j=0; 00265 } 00266 if (light>90){ 00267 error(); 00268 } 00269 } 00270 if (buttonFlag){ 00271 buttonFlag=0; 00272 menu(); 00273 } 00274 } 00275 } 00276 /** function to display menu options */ 00277 void menu(){ 00278 buttonleft.rise(&buttonPressed); 00279 while(1) { 00280 wait (0.1); 00281 lcd.normalMode(); //normal LCD colour mode 00282 lcd.setBrightness(0.5); //LCD backlight set to 50% brightness 00283 if (POT>(2.0/3.0)) { 00284 clearCells(); 00285 lcd.printString("Temperature",10,1); 00286 lcd.printString(">",80,2); 00287 lcd.printString("Graph",0,5); 00288 lcd.printString("Current",43,5); 00289 if (buttonFlag){ //left button takes the user to the graph option 00290 buttonFlag=0; 00291 clearCells(); 00292 tempGraph(); 00293 } 00294 if (BUT2>0.9) { //right button takes the user to the current reading 00295 clearCells(); 00296 readTemp(); 00297 } 00298 } 00299 if ((POT>1.0/3.0)&&(POT<2.0/3.0)) { 00300 clearCells(); 00301 lcd.printString("Pressure",20,1); 00302 lcd.printString("<",0,2); 00303 lcd.printString(">",80,2); 00304 lcd.printString("Graph",0,5); 00305 lcd.printString("Current",43,5); 00306 if (buttonFlag) { 00307 buttonFlag=0; 00308 clearCells(); 00309 pressGraph(); 00310 } 00311 if (BUT2>0.9) { 00312 clearCells(); 00313 readPress(); 00314 } 00315 } 00316 if (POT<(1.0/3.0)) { 00317 clearCells(); 00318 lcd.printString("Light",30,1); 00319 lcd.printString("<",0,2); 00320 lcd.printString("Graph",0,5); 00321 lcd.printString("Current",43,5); 00322 if (buttonFlag) { 00323 buttonFlag=0; 00324 clearCells(); 00325 lightGraph(); 00326 } 00327 if (BUT2>0.9) { 00328 clearCells(); 00329 readLight(); 00330 } 00331 } 00332 } 00333 } 00334 /** function to clear cells */ 00335 void clearCells () 00336 { 00337 //loop through cells and clear 00338 for (int i=0; i<nx; i++) { 00339 for (int j=0; j<ny; j++) { 00340 lcd.clearPixel(i,j); 00341 } 00342 } 00343 lcd.refresh (); //must refresh to write buffer to display 00344 } 00345 /** main function */ 00346 int main(){ 00347 while(1){ 00348 PHY_PowerDown(); //powers down the ethernet link 00349 lcd.init(); 00350 bmp180.init(); 00351 menu(); 00352 } 00353 }
Generated on Thu Jul 14 2022 02:40:59 by
1.7.2