Amir Asemanpayeh / Mbed 2 deprecated WeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

Revision:
15:c6f7c4768cf2
Parent:
14:f3eb0fb6ecd5
Child:
16:5ce95c8f4e89
--- a/main.cpp	Thu Apr 23 13:26:11 2015 +0000
+++ b/main.cpp	Thu Apr 23 20:48:42 2015 +0000
@@ -18,6 +18,8 @@
 Ticker dataLoggerTimer ; // a ticker object for the data logger functions
 Timeout powerSaverTimeout; // create a timeout object for the poweerSaver function
 
+
+int i= 0 ; //integer used to indicate the cell number with in an array
 int setTimeFlag = 0; // flag for real time clock ISR
 int timerFlag = 1; // Flag for tempesrature pressure reading ISR
 int button1Flag = 0;
@@ -29,33 +31,26 @@
 int lcdPowerFlag = 1 ; // Indicates if the LCD is switched on or powered off
 int dataLoggerFlag = 0 ;
 int saveToFileFlag = 0 ;
-
+int runLoggerFlag = 0 ; // if the logger is switched on allows user to pick this option
 
-int temperatureC  ;
-int temperatureF  ;
-float pressureMB  ;
-float pressureATM ;
-int choice=1 ;
-int length=14;
 int UNIXdate;
 int powerSaverTime = 60;
 int dataLoggerTime = 60;
-int avgTC ; // an integer to store the average value of the temperature in celsious
-int avgTF ;// int to save the average value of the temperature in farenheit
-int avgPB ; // int to store the average value of pressure in mBar
-int avgPA ; // int to save the average pressure in atm
-int maxTC ;
-int minTC ;
-int maxTF ;
-int minTF ;
-int maxPB ;
-int minPB ;
-int maxPA ;
-int minPA ;
-float arrayTC[83]= {5} ; // An array to store the values of temperature in c
-float arrayTF[83]= {5} ; // An array to store the values of temperature in farenh
-float arrayPB[83]= {5} ; // An array to store the values of pressure in dB
-float arrayPA[83]= {5} ; // An array to store the values of pressure in atm
+
+int temperature;
+float pressure;
+int averageTemperature ;
+int averagePressure ;
+int minTemperature ;
+int maxTemperature ;
+int minPressure;
+int maxPressure;
+
+
+
+
+float arrayT[83] ; // An array to store the values of temperature to draw graphs
+float arrayP[83] ; // An array to store the values of pressure to draw graphs
 
 
 
@@ -85,6 +80,12 @@
 
 
 
+
+void savePower();
+void loggerData();
+
+
+
 struct  State {
     int output;
     char title[14];
@@ -193,7 +194,6 @@
 
 void dataLoggerTimerExpired ()
 {
-    leds = 0;
     dataLoggerFlag = 1 ;
 
 }
@@ -248,34 +248,59 @@
     measurement = bmp180.readValues();
     // serial.printf("T = %.2f C P = %.2f mb\n",measurement.temperature,measurement.pressure);
 
-    temperatureC = measurement.temperature;
-    temperatureF = (temperatureC*(9/5))+32;
 
     if (unitFlag==1 || unitFlag==2 ) {
-        length = sprintf(bufferT,"T = %0.1d C",temperatureC); // print formatted data to buffer
+
+        temperature = measurement.temperature;
+        int length = sprintf(bufferT,"T = %0.1d C",temperature); // print formatted data to buffer
         // it is important the format specifier ensures the length will fit in the buffer
     }
     if (unitFlag==3 || unitFlag==4 ) {
-        length = sprintf(bufferT,"T = %0.1d F",temperatureF); // print formatted data to buffer
+        temperature =( measurement.temperature*(9/5))+32;
+        int length = sprintf(bufferT,"T = %0.1d F",temperature); // print formatted data to buffer
         // it is important the format specifier ensures the length will fit in the buffer
     }
 
-    pressureMB = measurement.pressure;  // same idea with floats
-    pressureATM = pressureMB*0.0009869;
+
 
 
     if (unitFlag==1 || unitFlag==3 ) {
-        length = sprintf(bufferP,"P = %.2f mb",pressureMB); // print formatted data to buffer
+        pressure = measurement.pressure;  // same idea with float
+        int  length = sprintf(bufferP,"P = %.2f mb",pressure); // print formatted data to buffer
         // it is important the format specifier ensures the length will fit in the buffer
     }
     if (unitFlag==2 || unitFlag==4 ) {
-        length = sprintf(bufferP,"P = %.2f atm",pressureATM); // print formatted data to buffer
+        pressure = measurement.pressure*0.0009869;  // same idea with floats
+        int length = sprintf(bufferP,"P = %.2f atm",pressure); // print formatted data to buffer
         // it is important the format specifier ensures the length will fit in the buffer
     }
 
 }
 
 
+void wakeUp ()  //Turns the LCD on after the mbed is interrupted
+{
+
+
+    if (lcdPowerFlag == 0) {// if the LCD is powered off
+        serial.printf("lcd is off\n");
+
+        if (button1Flag || button2Flag || button3Flag || button4Flag) { // if any of the buttons are pressed
+
+            serial.printf("one of the buttons are pressed to turn on the lcd\n");
+            lcdPowerFlag = 1 ; // sets the status of the lcd to powered on
+            serial.printf("lcd is on\n");
+            lcd.init();  // turns the LCD on
+            powerSaverTimeout.attach(&savePower, powerSaverTime); // setup a timeiut to call the savePower function
+            button1Flag = 0 ;
+            button2Flag = 0 ;
+            button3Flag = 0 ;
+            button4Flag = 0 ;
+
+        }
+    }
+
+}
 
 
 
@@ -288,7 +313,6 @@
     lcdPowerFlag = 0 ;  // A flag is set to indicate the state of the LCD (is switched off)
     serial.printf("LCD is off\n");
 
-
 }
 
 
@@ -317,65 +341,17 @@
 
 
 
-void wakeUp ()  //Turns the LCD on after the mbed is interrupted
-{
-
-
-    if (lcdPowerFlag == 0) {// if the LCD is powered off
-        serial.printf("lcd is off\n");
-
-        if (button1Flag || button2Flag || button3Flag || button4Flag) { // if any of the buttons are pressed
-
-            serial.printf("one of the buttons are pressed to turn on the lcd\n");
-            lcdPowerFlag = 1 ; // sets the status of the lcd to powered on
-            serial.printf("lcd is on\n");
-            lcd.init();  // turns the LCD on
-            powerSaverTimeout.attach(&savePower, powerSaverTime); // setup a timeiut to call the savePower function
-            button1Flag = 0 ;
-            button2Flag = 0 ;
-            button3Flag = 0 ;
-            button4Flag = 0 ;
-        }
-    }
-
-}
-
-//void setLoggerTimer()
-//{
-//    switch (dataLoggerTime) {
-//        case 1:
-//            dataLoggerTimer.attach(&dataLoggerTimerExpired , 300);
-//            break;
-//        case 2:
-//            dataLoggerTimer.attach(&dataLoggerTimerExpired , 600);
-//            break;
-//        case 3:
-//            dataLoggerTimer.attach(&dataLoggerTimerExpired , 900);
-//            break;
-//        case 4:
-//            dataLoggerTimer.attach(&dataLoggerTimerExpired , 1200);
-//            break;
-//        case 5:
-//            dataLoggerTimer.attach(&dataLoggerTimerExpired , 1500);
-//            break;
-//        case 6:
-//            dataLoggerTimer.attach(&dataLoggerTimerExpired , 1800);
-//            break;
-//        default:
-//            break;
-//    }
-//}
-
 
 void liveData() //
 {
     powerSaverCheck();
     while (1) {
+        updateTime();
         wakeUp();
+                loggerData();
         if (timerFlag) {
             timerFlag=0;
             readData();
-            // lcd.printString("hey",1,1);
             strncpy(buffer1, bufferTime, 14);
             strncpy(buffer2, bufferDate, 14);
             strncpy(buffer3, bufferT, 14);
@@ -394,159 +370,141 @@
 
 
 
-void updateLoggerData() // If the data logger ISR is set then it reads the data and stores to suitable arrays (the arrays are used to plot the desired graphs)
+void loggerData() //  gets the temp and pressure data and stores to suitable arrays (the arrays are used to plot the desired graphs)
 {
-    if (dataLoggerFlag) {
-        dataLoggerFlag=0;
+    if(dataLoggerFlag ==1) {
+    int k ;
+    int sumTemperature = 0;
+    int sumPressure = 0;
+    minTemperature = arrayT[0]; // sets the min to the first value in the array
+    minPressure = arrayP[0];
+    maxTemperature = arrayT[0];
+    maxPressure = arrayP[0];
+    readData();
 
-        int i = 0 ;
-        int k ;
-        int sumTC = 0;
-        int sumTF = 0;
-        int sumPB = 0;
-        int sumPA = 0;
-        int maxTC = 0 ;
-        minTC = 0 ;
-        maxTF = 0 ;
-        minTF = 0 ;
-        maxPB = 0 ;
-        minPB = 0 ;
-        maxPA = 0 ;
-        minPA = 0 ;
-        i++;
-
+        dataLoggerFlag=0;
 //write the data to the arrays (arrays used to plot graphs)
         //read the data from sensor
-        readData();
+
         //Store the values in the array
-        arrayTC[i] = temperatureC;
-        arrayTF[i] = temperatureF;
-        arrayPB[i] = pressureMB;
-        arrayPA[i] = pressureATM;
+        arrayT[i] = temperature;
+        arrayP[i] = pressure/25 ;
+
 
 // calculate the average value of the arrays and save them to an integer
 
-        for (k = 0 ; k<i ; k++) {
-            sumTC += arrayTC[k]; // calculates the sum of the stored values
-            avgTC = sumTC/k ; //calculates the average value
-            sumTF += arrayTF[k]; // calculates the sum of the stored values
-            avgTF = sumTF/k ; //calculates the average value
-            sumPB += arrayPB[k]; // calculates the sum of the stored values
-            avgPB = sumTC/k ; //calculates the average value
-            sumPA += arrayPA[k]; // calculates the sum of the stored values
-            avgPA = sumPA/k ; //calculates the average value
+        for (k = 0 ; k<=i ; k++) { // loops through the arrays
+
+            sumTemperature += arrayT[k]; // calculates the sum of the stored values
+            averageTemperature = sumTemperature/(k+1) ; //calculates the average value
 
-            if(arrayTC[i]>maxTC) { // finds the biggest value of the array
-                maxTC=arrayTC[i];
-            }
-            if(arrayTC[i]<minTC) { // finds the smallest value of the array
-                minTC=arrayTC[i];
+            sumPressure += arrayP[k]; // calculates the sum of the stored values
+            averagePressure = sumPressure/(k+1) ; //calculates the average value
+
+            if(arrayT[k]>maxTemperature) { // checks if any of the values in the array is bigger than the fist value
+                maxTemperature=arrayT[k];  // if any greater values, sets the max value to that
             }
-            if(arrayTF[i]>maxTF) {
-                maxTF=arrayTF[i];
-            }
-            if(arrayTF[i]<minTF) {
-                minTF=arrayTF[i];
-            }
-            if(arrayPB[i]>maxPB) {
-                maxPB=arrayPB[i];
+            if(arrayT[k]<minTemperature) { // finds the smallest value of the array
+                minTemperature=arrayT[k];
             }
-            if(arrayPB[i]<minPB) {
-                minPB=arrayPB[i];
+            if(arrayP[k]>maxPressure) { // checks if any of the values in the array is bigger than the fist value
+                maxPressure=arrayP[k];  // if any greater values, sets the max value to that
             }
-            if(arrayPA[i]>maxPA) {
-                maxPA=arrayPA[i];
-            }
-            if(arrayPA[i]<minPA) {
-                minPA=arrayPA[i];
+            if(arrayP[k]<maxPressure) {
+                minPressure=arrayP[k];
             }
 
-
-
         }
-
+        i++; // moves to the next cell in the array
         if (i>83) {
             lcd.clear();
             i=0;
         }
-
     }
 }
 
 
 
 
+
 void dataLogger()  // shows a summery of data and the graphs
 {
-    leds = 0;
-    choice = 1;
-    char Max[2] = {0};
-    char Min[2] = {0};
-    char Avg[2] = {0};
+    int swipe = 1 ;
     while (1) {
-        updateLoggerData();
-        int min = sprintf (Min, "Min = %d", minTC);//convert integer to buffer str
-        int max = sprintf (Max, "Max = %d", maxTC);//convert integer to buffer str
-        int avg = sprintf (Avg, "Avg = %d", avgTC);//convert integer to buffer str
-        //int mn = sprintf (Min, "Min = %d", minPB);//convert integer to buffer str
-        //int mx = sprintf (Max, "Max = %d", maxPB);//convert integer to buffer str
-        //int ag = sprintf (Avg, "Avg = %d", avgPB);//convert integer to buffer str
+        if (runLoggerFlag) {
+
+
+            loggerData();
+
+            if (button4Flag) {
+                button4Flag = 0 ;
+                swipe ++;
+            }
+            if (swipe > 4) {
+                swipe = 1;
+            }
+
+            strncpy(buffer0, bufferTime, 14);
+            int display5 = sprintf (buffer5, " Back    Next");//convert integer to buffer str
 
-        if (choice < 1) {
-            choice =4 ;
-        }
-        if (choice > 4 ) {
-            choice = 1;
+            switch (swipe) {
+                case 1: {
+                    int display1 = sprintf (buffer1, "Temperature");
+                    int display2 = sprintf (buffer2, "Min = %d", minTemperature);
+                    int display3 = sprintf (buffer3, "Max = %d", maxTemperature);
+                    int display4 = sprintf (buffer4, "Avg = %d", averageTemperature);
+                    display();
+                    break;
+                }
+                case 2 : {
+                    lcd.clear();
+                    lcd.printString("  Temperature",0,0);
+                    lcd.plotArray(arrayT);
+                    break;
+                }
+                case 3 : {
+                    int display11 = sprintf (buffer1, "Pressure");
+                    int display22 = sprintf (buffer2, "Min = %d", minPressure);
+                    int display33 = sprintf (buffer3, "Max = %d", maxPressure);
+                    int display44 = sprintf (buffer4, "Avg = %d", averagePressure);
+                    display();
+                    break;
+                }
+                case 4 : {
+                    lcd.clear();
+                    lcd.printString("   Pressure",0,0);
+                    lcd.plotArray(arrayP);
+                    break;
+                }
+                default :
+                    break;
+            }
+        } else {
+            lcd.clear();
+            lcd.printString("  To Turn On",0,1);
+            lcd.printString("    Go To",0,2);
+            lcd.printString("   Settings",0,3);
+            lcd.printString ("Back",0,5);
+
         }
 
-        switch (choice) {
-            case 1 : {
-                lcd.clear();
-                leds = 1 ;
-                lcd.printString(bufferTime,48,0);
-                lcd.printString("Temperature",36,1);
-                lcd.printString(Min,0,2);
-                lcd.printString(Avg,0,3);
-                lcd.printString(Max,0,4);
-                lcd.printString("Next",40,5);
-
-
-                break;
-            }
-            case 2 :
-                lcd.clear();
-                leds = 2 ;
-                break;
-            case 3 :
-                lcd.clear();
-                leds = 4 ;
-                lcd.printString(bufferTime,48,0);
-                lcd.printString("Temperature",36,1);
-                lcd.printString(Min,0,2);
-                lcd.printString(Avg,0,3);
-                lcd.printString(Max,0,4);
-                lcd.printString("Next",40,5);
-
-                break;
-            case 4 :
-                leds = 8 ;
-                lcd.clear();
-                break;
-            default :
-
-                break;
+        if (button3Flag) {
+            button3Flag=0;
+            break;
         }
+        wait(0.1);
     }
 }
 
+
 void dataLoggerSetting()
 {
-    state = dataLoggerFlag ; //inits the state to the saved flag by the user in the past
+    state = runLoggerFlag ; //inits the state to the saved flag by the user in the past
     powerSaverCheck();
     while (1) {
         wakeUp();
         updateTime();
-
+        loggerData();
         int display0 = sprintf (buffer0, " Back    Save");
         int display1 = sprintf (buffer1, "");
         int display2 = sprintf (buffer2, "Data Logger");
@@ -563,7 +521,7 @@
 
 
 
-        dataLoggerFlag = state ; //sets the flag to the value of state (0 is off, 1 is on)
+        runLoggerFlag = state ; //sets the flag to the value of state (0 is off, 1 is on)
         dataLoggerTime = fsmE[state].output; //sets the timer to to value of output from the fsm
         serial.printf("data logger Time = %i \n dataLoggerFlag = %i \n",dataLoggerTime , dataLoggerFlag);
 
@@ -580,6 +538,13 @@
         }
 
         if (button3Flag) {
+            if (state == 1) { // if the data logger is swiched on
+                dataLoggerTimer.attach(&dataLoggerTimerExpired, dataLoggerTime); // set the timer
+            }
+            if (state == 0) { // if the data logger is swiched off
+                dataLoggerTimer.detach(); // detach the timer
+            }
+            dataLoggerFlag = 1;
             button3Flag=0;
             break;
         }
@@ -606,7 +571,7 @@
     while (1) {
         wakeUp();
         updateTime();
-
+        loggerData();
         int display0 = sprintf (buffer0, " Back    Save");
         int display1 = sprintf (buffer1, "");
         int display2 = sprintf (buffer2, "Power Saver");
@@ -684,6 +649,7 @@
     while(1) {
         wakeUp();
         powerSaverCheck();
+                loggerData();
 
 
         if (button4Flag) {
@@ -707,6 +673,7 @@
     while (1) {
         wakeUp();
         updateTime();
+                loggerData();
         stateminus1=fsmD[state].nextState[0];
         stateminus2=fsmD[stateminus1].nextState[0];
         stateplus1=fsmD[state].nextState[1];
@@ -749,6 +716,7 @@
     while (1) {
         wakeUp();
         updateTime();
+                loggerData();
         stateminus1=fsmC[state].nextState[0];
         stateminus2=fsmC[stateminus1].nextState[0];
         stateplus1=fsmC[state].nextState[1];
@@ -822,6 +790,7 @@
 
         wakeUp();
         updateTime();
+        loggerData();
         strncpy(buffer0, bufferTime, 14);
         int dispaly = sprintf (buffer1, "%s", fsmB[stateminus2].title);//convert integer to buffer str
         int dispaly1 = sprintf (buffer2, "%s", fsmB[stateminus1].title);//convert integer to buffer str
@@ -950,6 +919,7 @@
 
     while(1) {
         updateTime();
+        loggerData();
         startMenu();
         display();
         wait (0.1);