Volkan Esendag / Mbed 2 deprecated mbed_PortableWeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

Revision:
5:6d85cafa1085
Parent:
4:da904413485a
Child:
6:0aca5c17c988
--- a/main.cpp	Mon Apr 13 14:43:42 2015 +0000
+++ b/main.cpp	Mon Apr 13 20:35:21 2015 +0000
@@ -188,6 +188,10 @@
 int i = 0;
 int j = 0;
 
+int dispSetting = 0; //set display setting to default.
+int recSetting = 3;  //set log setting to default.
+int tempSetting = 2; //set temperature threshold to default.
+
 N5110 lcd(p7,p8,p9,p10,p11,p13,p21);    //VCC,SCE,RST,DC,MOSI,SCLK,BACKLIGHT
 
 ///////////The following pieces of code are to configure real-time clock for the data logger.*************
@@ -287,20 +291,46 @@
 
 void buttonThreePressed()
 {
-    if(menuButtonFlag) {
-        menuButtonFlag = 0;
+    if(menuButtonFlag > 0) {
         buttonThreeAltFlag = !buttonThreeAltFlag;
     } else
         buttonThreeFlag = !buttonThreeFlag;
 }
 
-int splashFlag = 1;  /*!< Splash flag set to continue with program flow for the main function before proceeding to while(1) */
+int splashFlag = 1;  /*!< Splash flag set to continue with program flow for the main function before proceeding with program flow */
+
+/**
+Interrupt Service Routine to reset splashFlag when Timeout has been performed.
+No parameters to be entered by, or values to be returned to, the user.
+*/
 
 void splashDelay()
 {
     splashFlag = 0;
 }
 
+int dispTimerFlag = 0; /*!< Display flag set to 0 initially. Used to update values on the display when the ISR is called.*/
+
+/**
+Interrupt Service Routine to set dispTimerFlag when Ticker duration has elapsed.
+No parameters to be entered by, or values to be returned to, the user.
+*/
+void timerExpiDisplay()
+{
+    dispTimerFlag = 1;
+}
+
+int logTimerFlag = 0; /*!< Log flag set to 0 initially. Used to overwrite the Flash Memory when the ISR is called.*/
+
+/**
+Interrupt Service Routine to set logTimerFlag when Ticker duration has elapsed.
+No parameters to be entered by, or values to be returned to, the user.
+*/
+void timerExpiLog()
+{
+    logTimerFlag = 0;
+}
+
 void displayInitSplash()  //display splash screen
 {
     lcd.printString("Welcome to",15,1);
@@ -312,21 +342,15 @@
 
 //1 bar is 100000 Pa. An atm is 101325 Pa. Therefore an mb is 100 Pa.
 
-/**
-Fetches readings from the sensor, converts them into its internal parameters and calculates altitude from pressure data.
-Then prints them on the screen and overwrites on the mbed's Flash memory.
-@param temp - temperature reading from the BMP180(˚C).
-@param press - pressure reading from the BMP180(mb).
-@param altitude - altitude calculated from the pressure reading (m).
-*/
-
-
-
 int main()
 {
 
     splashFlip.attach(&splashDelay,3.0); //attach timer and wait for ISR to be called
 
+    displayTimer.attach(&timerExpiDisplay,1.0); //do the same for display dispInterval[dispSetting].time
+
+    logTimer.attach(&timerExpiLog,recInterval[recSetting].time); //do the same for logging
+
     // first need to initialise display
     lcd.init();
 
@@ -346,27 +370,43 @@
 
         if(splashFlag == 0) { //if ISR has been called proceed with program flow
             splashFlip.detach();    //detach Timeout object
+            lcd.clear();
             clearCells();
             lcd.refresh();
 
-            //read values (T in degrees Celsius and P in mb).
-            measurement = bmp180.readValues();
-            temp = measurement.temperature;
-            press = measurement.pressure;
-            //formula for calculating altitude from sea level by using atmospheric pressure. Unit in metres.
-            //use pow(double a,double b) for indices, not the ^ sign. Just a reminder!
-            altitude = 44330.0*(1.0-(pow((press/PNought),(1.0/5.255))));
-            printReadings();
-            lcd.refresh();
+            if(dispTimerFlag) {
+                dispTimerFlag = 0;
 
-            //Sleep();  //put the mbed to sleep once the program flow has been completed.
-            
-            wait(1.0);
+                //read values (T in degrees Celsius and P in mb).
+                measurement = bmp180.readValues();
+                temp = measurement.temperature;
+                press = measurement.pressure;
+                /*formula for calculating altitude from sea level by using atmospheric pressure. Unit in metres.
+                Use pow(double a,double b) for indices, not the ^ sign. Just a reminder! Also check out this site:
+                http://www.mide.com/products/slamstick/air-pressure-altitude-calculator.php
+                Also to bear in mind is that a metre is 3.2808399 feet; or 1.0936133 yards. Three feet equals a yard.
+                */
+                altitude = 44330.0*(1.0-(pow((press/PNought),(1.0/5.255))));
+                printReadings();
+                lcd.refresh();
+
+            }
+
+            Sleep();  //put the mbed to sleep once the program flow has been completed.
+
 
         } //close if
     } //close while
 } //terminate main()
 
+/**
+Fetches readings from the sensor via the main() function and calculates altitude from pressure data.
+Then prints them on the screen.
+@param temp - temperature reading from the BMP180(˚C).
+@param press - pressure reading from the BMP180(mb).
+@param altitude - altitude calculated from the pressure reading (m).
+*/
+
 void printReadings()
 {
     char buffer[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
@@ -379,7 +419,7 @@
         lcd.printString(buffer,0,1);  // display on screen. Column 0, row 1.
     }
 
-    length = sprintf(buffer,"P = %.2f mb",press);
+    length = sprintf(buffer,"P = %.2f mb",press); //use single letters to represent parameters or string may not fit in the banks!
     if (length <= 14) {
         lcd.printString(buffer,0,2); // Column 0, row 2.
     }
@@ -391,8 +431,7 @@
     if (press++) {
         lcd.printString("Hotter Weather",2,4); //column 2, row 4.
         lcd.printString("On the Way!",2,5);   //column 2 , row 5.
-    }
-    else if (press--) {
+    } else if (press--) {
         lcd.printString("The temperature",2,4); //column 2, row 4.
         lcd.printString("is predicted",2,5);   //column 2 , row 5.
         lcd.printString("to fall!",2,6); //column 2, row 4.
@@ -408,4 +447,4 @@
         }
     }
     lcd.refresh(); //must refresh to write buffer to display
-}
+}
\ No newline at end of file