Amir Asemanpayeh / Mbed 2 deprecated WeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

Revision:
8:8b810c29eb7b
Parent:
7:328dc986484f
Child:
9:d1691d971f63
--- a/main.cpp	Sun Apr 12 13:24:23 2015 +0000
+++ b/main.cpp	Mon Apr 20 13:16:26 2015 +0000
@@ -15,6 +15,7 @@
 
 
 Ticker timer; //Create ticker object for temperature and pressure readings
+Ticker dataLoggerTimer ; // a ticker object for the data logger functions
 Timeout powerSaverTimeout; // create a timeout object for the poweerSaver function
 
 int setTimeFlag = 0; // flag for real time clock ISR
@@ -27,9 +28,13 @@
 int powerSaverFlag = 0 ;
 int lcdPowerFlag = 1 ; // Indicates if the LCD is switched on or powered off
 int dataLoggerFlag = 0 ;
+int saveToFileFlag = 0 ;
 
 
-
+int temperatureC  ;
+int temperatureF  ;
+float pressureMB  ;
+float pressureATM ;
 int choice=1 ;
 int length;
 int year=2015;
@@ -40,8 +45,23 @@
 int sec=1;
 int UNIXdate;
 int powerSaverTime = 60;
-int dataLoggerTime = 60;
-
+int dataLoggerTime = 2;
+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
 
 
 
@@ -94,7 +114,6 @@
 
 
 
-
 void serialISR()
 {
     //reads rx string into buffer when a serial interrupt occurs
@@ -104,6 +123,22 @@
 }
 
 
+void timerExpired() // ISR which sets the timer flag (flag used to display the buffers on LCD)
+{
+    timerFlag=1;
+}
+
+
+
+void dataLoggerTimerExpired ()
+{
+    leds = 0;
+    dataLoggerFlag = 1 ;
+
+}
+
+
+
 
 void setTime() // sets the time every second
 {
@@ -126,14 +161,11 @@
 
     if (setTimeFlag) {
         setTimeFlag=0;
+        setTime();
     }
 }
 
 
-void timerExpired() // ISR which sets the timer flag (flag used to display the buffers on LCD)
-{
-    timerFlag=1;
-}
 
 
 
@@ -146,8 +178,8 @@
     measurement = bmp180.readValues();
     // serial.printf("T = %.2f C P = %.2f mb\n",measurement.temperature,measurement.pressure);
 
-    int temperatureC = measurement.temperature;
-    int temperatureF = (temperatureC*(9/5))+32;
+    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
@@ -158,8 +190,8 @@
         // it is important the format specifier ensures the length will fit in the buffer
     }
 
-    float pressureMB = measurement.pressure;  // same idea with floats
-    float pressureATM = pressureMB*0.0009869;
+    pressureMB = measurement.pressure;  // same idea with floats
+    pressureATM = pressureMB*0.0009869;
 
 
     if (unitFlag==1 || unitFlag==3 ) {
@@ -171,8 +203,8 @@
         // it is important the format specifier ensures the length will fit in the buffer
     }
 
+}
 
-}
 
 
 
@@ -244,26 +276,205 @@
 
 }
 
+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 updateData() //
+void updateLiveData() //
 {
-    while (1) {
-        if (timerFlag) {
-            timerFlag=0;
+
+    if (timerFlag ) {
+        timerFlag=0;
+        while (1) {
+            wakeUp();
+
             readData();
             displayData();
+
+            if (button3Flag) {
+                button3Flag = 0;
+                timer.detach();
+                break;
+            }
+
+            Sleep();
         }
-        if (button3Flag) {
-            button3Flag = 0;
-            break;
-        }
-
-        Sleep();
     }
 }
 
 
+
+
+
+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)
+{
+    if (dataLoggerFlag) {
+        dataLoggerFlag=0;
+        lcd.clear();
+
+        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++;
+
+//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;
+
+// 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
+
+            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];
+            }
+            if(arrayTF[i]>maxTF) {
+                maxTF=arrayTF[i];
+            }
+            if(arrayTF[i]<minTF) {
+                minTF=arrayTF[i];
+            }
+            if(arrayPB[i]>maxPB) {
+                maxPB=arrayPB[i];
+            }
+            if(arrayPB[i]<minPB) {
+                minPB=arrayPB[i];
+            }
+            if(arrayPA[i]>maxPA) {
+                maxPA=arrayPA[i];
+            }
+            if(arrayPA[i]<minPA) {
+                minPA=arrayPA[i];
+            }
+
+
+
+        }
+
+        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};
+    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 (choice < 1) {
+            choice =4 ;
+        }
+        if (choice > 4 ) {
+            choice = 1;
+        }
+
+        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;
+        }
+    }
+}
+
 void dataLoggerSetting()
 {
 
@@ -271,9 +482,10 @@
     choice = dataLoggerFlag ;
     powerSaverCheck();
 
+
+
     while (1) {
         wakeUp();
-        int time = sprintf (DataLoggerTime, "%d min", dataLoggerTime/60);//convert integer to buffer str
 
         if (choice < 1) {
             choice = 2;
@@ -281,10 +493,6 @@
         if (choice > 2) {
             choice = 1;
         }
-        if (dataLoggerTime > 1800) {
-            dataLoggerTime = 60 ;
-        }
-
 
         switch (choice) {
             case 1:
@@ -293,15 +501,14 @@
                 lcd.printString(bufferTime,48,0);
                 lcd.printString("Data Logger :",0,1);
                 lcd.printString("ON",0,2);
-                lcd.printString("Interval:",0,3);
+                lcd.printString("Save on RAM :",0,3);
                 lcd.printString(DataLoggerTime,0,4);
                 lcd.printString("Back      Save",0,5);
 
                 if (button4Flag) {
                     button4Flag=0;
+                    saveToFileFlag ++;
                     dataLoggerFlag = 1 ;
-                    dataLoggerTime += 60 ;
-
                 }
 
 
@@ -315,6 +522,7 @@
                 if (button4Flag) {
                     button4Flag=0;
                     dataLoggerFlag = 0 ;
+                    dataLoggerTimer.detach();
 
                 }
 
@@ -382,6 +590,7 @@
                     button4Flag=0;
                     powerSaverFlag = 1 ;
                     powerSaverTime += 60 ;
+                    dataLoggerTimer.attach(&dataLoggerTimerExpired , 5);
 
                 }
 
@@ -823,6 +1032,15 @@
 void startMenu()   //The menu displayed at the beginning
 {
     wakeUp();
+
+    if (choice > 5) {
+        choice = 1 ;
+    }
+    if (choice < 1) {
+        choice = 5 ;
+    }
+
+
     switch (choice) {
         case 1:
 
@@ -836,7 +1054,8 @@
             lcd.drawCircle(5,12,3,1);  // x,y,radius,black fill
             if (button4Flag) {
                 button4Flag=0;
-                updateData();
+                timer.attach(&timerExpired,1.0);
+                updateLiveData();
             }
 
 // rest of code here
@@ -850,7 +1069,10 @@
             lcd.printString("Alarms",0,4);
             lcd.printString("Settings",0,5);
             lcd.drawCircle(5,20,3,1);  // x,y,radius,black fill
-// rest of code here
+            if (button4Flag) {
+                button4Flag=0;
+                dataLogger();
+            }
             break;
         case 3:
             lcd.clear();
@@ -909,28 +1131,24 @@
     // initiliase barometer
     bmp180.init();
     lcd.init();
-    timer.attach(&timerExpired,1.0);
     serial.attach(&serialISR);
     button1.rise(&button1Pressed); // call ISR on rising edge (button1pressed)
     button2.rise(&button2Pressed); // call ISR on rising edge (button2pressed)
     button3.rise(&button3Pressed);
     button4.rise(&button4Pressed);
-
+    dataLoggerTimer.attach(&dataLoggerTimerExpired , dataLoggerTime);
     set_time(UNIXdate); // initialise time from the calculated UNIX time entered by the user
 
 
     powerSaverCheck();
 
     while(1) {
-
-        if (choice > 5) {
-            choice = 1 ;
-        }
-        if (choice < 1) {
-            choice = 5 ;
-        }
         updateTime();
         startMenu();
+        if (dataLoggerFlag) {
+            leds = 15;
+            dataLoggerFlag = 0 ;
+        }
         wait (0.1);
     }
 }