Volkan Esendag / Mbed 2 deprecated mbed_PortableWeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

Committer:
Volcano_498
Date:
Mon Apr 13 14:43:42 2015 +0000
Revision:
4:da904413485a
Parent:
3:70e14f1577f7
Child:
5:6d85cafa1085
PNought value corrected. Now current altitude as calculated by the device is approximately 97 metres.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Volcano_498 0:b30f86a9a1c5 1 /**
Volcano_498 0:b30f86a9a1c5 2 @file main.cpp
Volcano_498 0:b30f86a9a1c5 3 @brief Code for a Portable Weather Station to be built on PCB.
Volcano_498 0:b30f86a9a1c5 4 @brief Design Specs: *A small, portable battery-powered data logger that records sensor data at regular intervals.
Volcano_498 0:b30f86a9a1c5 5 @brief -- Minimises mbed power consumption to the greatest extent possible.
Volcano_498 0:b30f86a9a1c5 6 @brief -- Displays the information to the user.
Volcano_498 0:b30f86a9a1c5 7 @brief -- Features a BMP180 pressure and temperature sensor. (TMP102 is unnecessary as BMP180 also takes temp. readings)
Volcano_498 3:70e14f1577f7 8 @brief -- Features a Nokia 5110 display to print the sensor readings over and display them to the user.
Volcano_498 0:b30f86a9a1c5 9 @brief -- Sensor information is recorded on Flash memory on the mbed.
Volcano_498 0:b30f86a9a1c5 10 @brief -- Graph to display data to the user.
Volcano_498 0:b30f86a9a1c5 11 @brief -- mbed is powered by a Buck regulator (5V output) that is in turn supplied by a 9V PP3 battery which powers the PCB.
Volcano_498 0:b30f86a9a1c5 12 @brief -- Audible and visual alert when a threshold reading is obtained.
Volcano_498 0:b30f86a9a1c5 13 @brief -- Celsius-to-Fahrenheit and Celsius-to-Kelvin conversions of temperature readings and displaying them.
Volcano_498 0:b30f86a9a1c5 14 @brief -- Adjusting the unit the pressure is displayed in: millibars(mb), Pascals(Pa) and Atmospheres (atm).
Volcano_498 0:b30f86a9a1c5 15 @brief -- Displaying a brief splash screen to show changes in the unit display settings.
Volcano_498 0:b30f86a9a1c5 16 @author Volkan Esendag (SID:200795870)
Volcano_498 3:70e14f1577f7 17 @date 11 March 2015 (created) / 13 April 2015(last modified)
Volcano_498 0:b30f86a9a1c5 18 */
Volcano_498 0:b30f86a9a1c5 19
Volcano_498 0:b30f86a9a1c5 20 #include "mbed.h"
Volcano_498 0:b30f86a9a1c5 21 #include "N5110.h"
Volcano_498 0:b30f86a9a1c5 22 #include "BMP180.h"
Volcano_498 0:b30f86a9a1c5 23 //import the PowerControl/EthernetPowerControl header files from the Power Control library
Volcano_498 0:b30f86a9a1c5 24 //to enable the power down features for the microprocessor and the Ethernet feature.
Volcano_498 0:b30f86a9a1c5 25 #include "PowerControl/PowerControl.h"
Volcano_498 0:b30f86a9a1c5 26 #include "PowerControl/EthernetPowerControl.h"
Volcano_498 0:b30f86a9a1c5 27
Volcano_498 1:454dddb8adc2 28 #ifndef USR_POWERDOWN
Volcano_498 0:b30f86a9a1c5 29 #define USR_POWERDOWN (0x104) //defines USB interface powerdown.
Volcano_498 1:454dddb8adc2 30 #endif
Volcano_498 1:454dddb8adc2 31
Volcano_498 2:08f2469728d5 32 #ifndef PNought
Volcano_498 4:da904413485a 33 #define PNought 1013.25 //Po = 101325 Pa or 1 atm or 1013.25 mb.
Volcano_498 2:08f2469728d5 34 #endif
Volcano_498 2:08f2469728d5 35
Volcano_498 2:08f2469728d5 36 /**
Volcano_498 3:70e14f1577f7 37 Custom struct for logging intervals. Fetch state number and select recording interval via the use of a button.
Volcano_498 2:08f2469728d5 38 */
Volcano_498 2:08f2469728d5 39
Volcano_498 3:70e14f1577f7 40 struct RecState {
Volcano_498 2:08f2469728d5 41 int recordState;
Volcano_498 2:08f2469728d5 42 float time;
Volcano_498 2:08f2469728d5 43 };
Volcano_498 3:70e14f1577f7 44 //once struct has been declared, define a pointer type with it.
Volcano_498 2:08f2469728d5 45 typedef const struct RecState RecS;
Volcano_498 2:08f2469728d5 46
Volcano_498 3:70e14f1577f7 47 //RecS object to determine states for logging intervals. Array size 10.
Volcano_498 2:08f2469728d5 48 RecS recInterval[10] = {
Volcano_498 3:70e14f1577f7 49 {0,600.0}, //state 0, 10 minutes
Volcano_498 3:70e14f1577f7 50 {1,1200.0}, //state 1, 20 minutes
Volcano_498 3:70e14f1577f7 51 {2,1800.0}, //state 2, 30 minutes
Volcano_498 3:70e14f1577f7 52 {3,3600.0}, //state 3, 1 hour. Default State.
Volcano_498 3:70e14f1577f7 53 {4,7200.0}, //state 4, 2 hours
Volcano_498 3:70e14f1577f7 54 {5,10800.0}, //state 5, 3 hours
Volcano_498 3:70e14f1577f7 55 {6,14400.0}, //state 6, 4 hours
Volcano_498 3:70e14f1577f7 56 {7,21600.0}, //state 7, 6 hours
Volcano_498 3:70e14f1577f7 57 {8,28800.0}, //state 8, 8 hours
Volcano_498 3:70e14f1577f7 58 {9,43200.0} //state 9, 12 hours
Volcano_498 3:70e14f1577f7 59 };
Volcano_498 3:70e14f1577f7 60
Volcano_498 3:70e14f1577f7 61 //Use the same struct again to create another object, this time for reading display intervals. Array size 6.
Volcano_498 3:70e14f1577f7 62 RecS dispInterval [6] = {
Volcano_498 3:70e14f1577f7 63 {0,1.0}, //state 0, 1 second. Default state.
Volcano_498 3:70e14f1577f7 64 {1,2.0}, //state 1, 2 seconds
Volcano_498 3:70e14f1577f7 65 {2,3.0}, //state 2, 3 seconds
Volcano_498 3:70e14f1577f7 66 {3,4.0}, //state 3, 4 seconds
Volcano_498 3:70e14f1577f7 67 {4,5.0}, //state 4, 5 seconds
Volcano_498 3:70e14f1577f7 68 {5,0.5}, //state 5, 500 milliseconds
Volcano_498 3:70e14f1577f7 69 };
Volcano_498 3:70e14f1577f7 70
Volcano_498 3:70e14f1577f7 71 /**Struct to set the temperature threshold after which point the device gives off a warning.*/
Volcano_498 3:70e14f1577f7 72 struct TempThreshold {
Volcano_498 3:70e14f1577f7 73 int tempState;
Volcano_498 3:70e14f1577f7 74 float thresTemp;
Volcano_498 3:70e14f1577f7 75 };
Volcano_498 3:70e14f1577f7 76
Volcano_498 3:70e14f1577f7 77 //define the TempTyp pointer type using the TempThreshold struct.
Volcano_498 3:70e14f1577f7 78 typedef const struct TempThreshold TempTyp;
Volcano_498 3:70e14f1577f7 79
Volcano_498 3:70e14f1577f7 80 //using this type create an object which contains an array of threshold states. Array size 5.
Volcano_498 3:70e14f1577f7 81 TempTyp tempThres[5] = {
Volcano_498 3:70e14f1577f7 82 {0,30.0}, //state 0, 30'C
Volcano_498 3:70e14f1577f7 83 {1,40.0}, //state 1, 40'C
Volcano_498 3:70e14f1577f7 84 {2,50.0}, //state 2, 50'C. Default threshold.
Volcano_498 3:70e14f1577f7 85 {3,60.0}, //state 3, 60'C
Volcano_498 3:70e14f1577f7 86 {4,70.0} //state 4, 70'C
Volcano_498 3:70e14f1577f7 87 };
Volcano_498 3:70e14f1577f7 88
Volcano_498 2:08f2469728d5 89
Volcano_498 1:454dddb8adc2 90 /**
Volcano_498 1:454dddb8adc2 91 @namespace bmp180
Volcano_498 1:454dddb8adc2 92 @brief A special structed type of I2C object created for the BMP180.
Volcano_498 1:454dddb8adc2 93 @brief For more info, see the BMP180 library by Craig Evans.
Volcano_498 1:454dddb8adc2 94 @see http://developer.mbed.org/users/eencae/code/BMP180/
Volcano_498 1:454dddb8adc2 95 */
Volcano_498 1:454dddb8adc2 96 BMP180 bmp180(p28,p27);
Volcano_498 1:454dddb8adc2 97 //Pins are declared in the public domain and the sensor itself acts on private variables.
Volcano_498 0:b30f86a9a1c5 98
Volcano_498 1:454dddb8adc2 99 /**
Volcano_498 1:454dddb8adc2 100 @namespace buzzerPwm
Volcano_498 1:454dddb8adc2 101 @brief PwmOut object to apply a PWM signal with a duty ratio of 50% to the buzzer as an improvised square wave.
Volcano_498 1:454dddb8adc2 102 @brief Used for an audible feedback should the temperature reading exceed a certain value.
Volcano_498 1:454dddb8adc2 103 */
Volcano_498 1:454dddb8adc2 104 PwmOut buzzerPwm(p24);
Volcano_498 0:b30f86a9a1c5 105
Volcano_498 1:454dddb8adc2 106 /**
Volcano_498 1:454dddb8adc2 107 @namespace redLED
Volcano_498 1:454dddb8adc2 108 @brief PwmOut object to apply a PWM signal to the red visual feedback LED via pin 22.
Volcano_498 1:454dddb8adc2 109 @brief Used for visual feedback to tell the user a certain temperature threshold has been reached.
Volcano_498 1:454dddb8adc2 110 */
Volcano_498 1:454dddb8adc2 111 PwmOut redLED(p22);
Volcano_498 1:454dddb8adc2 112
Volcano_498 1:454dddb8adc2 113 /**
Volcano_498 1:454dddb8adc2 114 @namespace greenLED
Volcano_498 1:454dddb8adc2 115 @brief PwmOut object to apply a PWM signal to the green visual feedback LED via pin 23.
Volcano_498 1:454dddb8adc2 116 @brief Used to let the user know the device is operating normally.
Volcano_498 1:454dddb8adc2 117 */
Volcano_498 1:454dddb8adc2 118 PwmOut greenLED(p23);
Volcano_498 1:454dddb8adc2 119
Volcano_498 1:454dddb8adc2 120 /**
Volcano_498 1:454dddb8adc2 121 @namespace serial
Volcano_498 1:454dddb8adc2 122 @brief Serial object to print readings over a USB cable and display them on a terminal.
Volcano_498 1:454dddb8adc2 123 */
Volcano_498 0:b30f86a9a1c5 124 Serial serial(USBTX,USBRX); //serial object to print readings for debugging WHILE the USB cable is connected.
Volcano_498 0:b30f86a9a1c5 125
Volcano_498 1:454dddb8adc2 126 /**
Volcano_498 1:454dddb8adc2 127 @namespace menuButton
Volcano_498 1:454dddb8adc2 128 @brief Interrupt object to call ISR for the designated menu button when an input to p15 is applied.
Volcano_498 1:454dddb8adc2 129 @namespace buttonOne
Volcano_498 1:454dddb8adc2 130 @brief Interrupt object to call ISR for Button 1 when an input to p16 is applied.
Volcano_498 1:454dddb8adc2 131 @namespace buttonTwo
Volcano_498 1:454dddb8adc2 132 @brief Interrupt object to call ISR for Button 2 when an input to p17 is applied.
Volcano_498 1:454dddb8adc2 133 @namespace buttonThree
Volcano_498 1:454dddb8adc2 134 @brief Interrupt object to call ISR for Button 3 when an input to p18 is applied.
Volcano_498 1:454dddb8adc2 135 */
Volcano_498 0:b30f86a9a1c5 136
Volcano_498 1:454dddb8adc2 137 InterruptIn menuButton(p15); //Interrupt object for the designated menu button.
Volcano_498 1:454dddb8adc2 138 InterruptIn buttonOne(p16); //Interrupt objects for the other buttons.
Volcano_498 1:454dddb8adc2 139 InterruptIn buttonTwo(p17);
Volcano_498 1:454dddb8adc2 140 InterruptIn buttonThree(p18);
Volcano_498 0:b30f86a9a1c5 141
Volcano_498 1:454dddb8adc2 142 /**
Volcano_498 1:454dddb8adc2 143 @namespace potAin
Volcano_498 1:454dddb8adc2 144 @brief Analogue input from potentiometer whose Vout is connected to pin 20.
Volcano_498 1:454dddb8adc2 145 */
Volcano_498 1:454dddb8adc2 146
Volcano_498 1:454dddb8adc2 147 AnalogIn potAin(p20); //Potentiometer feedback pin to the mbed.
Volcano_498 1:454dddb8adc2 148
Volcano_498 1:454dddb8adc2 149 /**
Volcano_498 1:454dddb8adc2 150 @namespace logTimer
Volcano_498 3:70e14f1577f7 151 @brief Ticker object to record/log readings with a specified interval. Can be varied with Interrupt Service Routines.
Volcano_498 1:454dddb8adc2 152 */
Volcano_498 1:454dddb8adc2 153
Volcano_498 1:454dddb8adc2 154 Ticker logTimer; //Ticker object to call ISR for a specified period of time.
Volcano_498 1:454dddb8adc2 155
Volcano_498 3:70e14f1577f7 156 /**
Volcano_498 3:70e14f1577f7 157 @namespace displayTimer
Volcano_498 3:70e14f1577f7 158 @brief Ticker object to display readings on screen with a specified interval. Can be varied with Interrupt Service Routines.
Volcano_498 3:70e14f1577f7 159 */
Volcano_498 3:70e14f1577f7 160 Ticker displayTimer;
Volcano_498 3:70e14f1577f7 161
Volcano_498 1:454dddb8adc2 162 /*
Volcano_498 1:454dddb8adc2 163 @namespace leds
Volcano_498 1:454dddb8adc2 164 @brief GPIO output for status LEDs - used to display error message or as a flash memory overwrite operation feedback.
Volcano_498 1:454dddb8adc2 165 */
Volcano_498 1:454dddb8adc2 166
Volcano_498 1:454dddb8adc2 167 BusOut leds(LED1,LED2,LED3,LED4); //BusOut object for error feedback LEDs.
Volcano_498 1:454dddb8adc2 168 //configure pins of the LCD display...
Volcano_498 1:454dddb8adc2 169
Volcano_498 1:454dddb8adc2 170 /**
Volcano_498 1:454dddb8adc2 171 @namespace splashFlip
Volcano_498 1:454dddb8adc2 172 @brief Calls an ISR only once when attached. Used to delay splash screen for a few seconds before commencing with the program flow.
Volcano_498 1:454dddb8adc2 173 */
Volcano_498 1:454dddb8adc2 174 Timeout splashFlip;
Volcano_498 1:454dddb8adc2 175
Volcano_498 1:454dddb8adc2 176 /**
Volcano_498 1:454dddb8adc2 177 @namespace lcd
Volcano_498 1:454dddb8adc2 178 @brief Object that belongs to the N5110 class. Set up pin outputs for the Nokia 5110 display. Defined in N5110.h.
Volcano_498 1:454dddb8adc2 179 @see https://developer.mbed.org/users/eencae/code/N5110/
Volcano_498 1:454dddb8adc2 180 */
Volcano_498 2:08f2469728d5 181 void printReadings();
Volcano_498 3:70e14f1577f7 182 void clearCells();
Volcano_498 2:08f2469728d5 183
Volcano_498 2:08f2469728d5 184 float temp = 0.0;
Volcano_498 2:08f2469728d5 185 float press = 0.0;
Volcano_498 2:08f2469728d5 186 float altitude = 0.0;
Volcano_498 2:08f2469728d5 187
Volcano_498 3:70e14f1577f7 188 int i = 0;
Volcano_498 3:70e14f1577f7 189 int j = 0;
Volcano_498 3:70e14f1577f7 190
Volcano_498 1:454dddb8adc2 191 N5110 lcd(p7,p8,p9,p10,p11,p13,p21); //VCC,SCE,RST,DC,MOSI,SCLK,BACKLIGHT
Volcano_498 0:b30f86a9a1c5 192
Volcano_498 0:b30f86a9a1c5 193 ///////////The following pieces of code are to configure real-time clock for the data logger.*************
Volcano_498 0:b30f86a9a1c5 194
Volcano_498 1:454dddb8adc2 195 char rxString[16]; // buffer to store received string. Each character is a byte long - hence the char pointer type.
Volcano_498 1:454dddb8adc2 196
Volcano_498 1:454dddb8adc2 197 int setTimeFlag = 0; /*!< set time flag set in serial ISR */
Volcano_498 0:b30f86a9a1c5 198
Volcano_498 1:454dddb8adc2 199 /**
Volcano_498 1:454dddb8adc2 200 Reads string input via serial interrupt and converts it into real time
Volcano_498 1:454dddb8adc2 201 @param rxString - string received from serial
Volcano_498 1:454dddb8adc2 202 @param time - integer that represents time, converted from input string (rxString).
Volcano_498 0:b30f86a9a1c5 203
Volcano_498 1:454dddb8adc2 204 */
Volcano_498 0:b30f86a9a1c5 205 void setTime()
Volcano_498 0:b30f86a9a1c5 206 {
Volcano_498 0:b30f86a9a1c5 207 // print time for debugging
Volcano_498 0:b30f86a9a1c5 208 serial.printf("set_time - %s",rxString);
Volcano_498 0:b30f86a9a1c5 209 // atoi() converts a string to an integer
Volcano_498 0:b30f86a9a1c5 210 int time = atoi(rxString);
Volcano_498 0:b30f86a9a1c5 211 // update the time
Volcano_498 0:b30f86a9a1c5 212 set_time(time);
Volcano_498 0:b30f86a9a1c5 213 }
Volcano_498 0:b30f86a9a1c5 214
Volcano_498 0:b30f86a9a1c5 215 void serialISR()
Volcano_498 0:b30f86a9a1c5 216 {
Volcano_498 0:b30f86a9a1c5 217 // when a serial interrupt occurs, read rx string into buffer. Holds 16 characters. gets implies fetching a string from serial port.
Volcano_498 1:454dddb8adc2 218 serial.gets(rxString,16);
Volcano_498 0:b30f86a9a1c5 219 // set flag
Volcano_498 0:b30f86a9a1c5 220 setTimeFlag = 1;
Volcano_498 0:b30f86a9a1c5 221 }
Volcano_498 0:b30f86a9a1c5 222
Volcano_498 1:454dddb8adc2 223 /**
Volcano_498 1:454dddb8adc2 224 Disables USB interface when the mbed's USB cable isn't attached.
Volcano_498 1:454dddb8adc2 225 @param arg - argument function (pointer type int/uint32_t)
Volcano_498 1:454dddb8adc2 226 @returns __semihost(USR_POWERDOWN, &arg)
Volcano_498 1:454dddb8adc2 227 */
Volcano_498 1:454dddb8adc2 228
Volcano_498 3:70e14f1577f7 229 int semihost_powerdown()
Volcano_498 3:70e14f1577f7 230 {
Volcano_498 3:70e14f1577f7 231
Volcano_498 1:454dddb8adc2 232 uint32_t arg; //variable for return function
Volcano_498 1:454dddb8adc2 233 return __semihost(USR_POWERDOWN, &arg); //return semihost state...
Volcano_498 3:70e14f1577f7 234
Volcano_498 1:454dddb8adc2 235 } //...to power down the USB interface when the USB cable is detached.
Volcano_498 1:454dddb8adc2 236
Volcano_498 1:454dddb8adc2 237 LocalFileSystem local("local"); // create local filesystem
Volcano_498 1:454dddb8adc2 238
Volcano_498 1:454dddb8adc2 239 void writeDataToFile(float data, float dataTwo, char dataThree[30])
Volcano_498 1:454dddb8adc2 240 {
Volcano_498 1:454dddb8adc2 241 time_t seconds = time(NULL); // get current time
Volcano_498 1:454dddb8adc2 242 strftime(dataThree, 30 , "%R %x", localtime(&seconds)); //convert it into a string, from an array size of 30.
Volcano_498 3:70e14f1577f7 243
Volcano_498 1:454dddb8adc2 244 leds = 15; // turn on LEDs for feedback
Volcano_498 1:454dddb8adc2 245 FILE *fp = fopen("/local/tempLog.csv", "a"); // open 'log.txt' for appending. Instance of class FILE.
Volcano_498 1:454dddb8adc2 246 // if the file doesn't exist it is created, if it exists, data is appended to the end
Volcano_498 3:70e14f1577f7 247 fprintf(fp,"%s, %.2f , %.2f \n",dataThree,dataTwo,data); // print string to file
Volcano_498 1:454dddb8adc2 248 fclose(fp); // close file
Volcano_498 1:454dddb8adc2 249 leds = 0; // turn off LEDs to signify file access has finished
Volcano_498 1:454dddb8adc2 250 }
Volcano_498 1:454dddb8adc2 251
Volcano_498 1:454dddb8adc2 252 int menuButtonFlag = 0; /*!< menu button flag set in ISR */
Volcano_498 1:454dddb8adc2 253
Volcano_498 3:70e14f1577f7 254 void menuButtonPressed()
Volcano_498 3:70e14f1577f7 255 {
Volcano_498 1:454dddb8adc2 256 menuButtonFlag = !menuButtonFlag; //if set to 1, set it back to 0. If not set, set the flag.
Volcano_498 1:454dddb8adc2 257 }
Volcano_498 1:454dddb8adc2 258
Volcano_498 1:454dddb8adc2 259 int buttonOneFlag = 0; /*!< Button One flag set in ISR */
Volcano_498 1:454dddb8adc2 260 int buttonOneAltFlag = 0; /*!< Button One Alternate flag set in ISR */
Volcano_498 1:454dddb8adc2 261
Volcano_498 3:70e14f1577f7 262 void buttonOnePressed()
Volcano_498 3:70e14f1577f7 263 {
Volcano_498 3:70e14f1577f7 264 if(menuButtonFlag) { //if menu button has been pressed and main menu entered
Volcano_498 1:454dddb8adc2 265 menuButtonFlag = 0; //set menu button flag to 0 so that one can proceed to the temperature setting menu and go back by pressing again
Volcano_498 1:454dddb8adc2 266 buttonOneAltFlag = !buttonOneAltFlag; //set/reset-if-set alternate flag and proceed to next menu
Volcano_498 3:70e14f1577f7 267 } else {
Volcano_498 1:454dddb8adc2 268 buttonOneFlag = !buttonOneFlag; //set/reset-if-set flag if not navigated to a menu
Volcano_498 1:454dddb8adc2 269 }
Volcano_498 1:454dddb8adc2 270 }
Volcano_498 1:454dddb8adc2 271
Volcano_498 1:454dddb8adc2 272 int buttonTwoFlag = 0; /*!< Button Two flag set in ISR */
Volcano_498 1:454dddb8adc2 273 int buttonTwoAltFlag = 0; /*!< Button Two Alternate flag set in ISR */
Volcano_498 1:454dddb8adc2 274
Volcano_498 3:70e14f1577f7 275 void buttonTwoPressed()
Volcano_498 3:70e14f1577f7 276 {
Volcano_498 3:70e14f1577f7 277 if(menuButtonFlag) {
Volcano_498 1:454dddb8adc2 278 menuButtonFlag = 0;
Volcano_498 3:70e14f1577f7 279 buttonTwoAltFlag = !buttonTwoAltFlag;
Volcano_498 3:70e14f1577f7 280 } else {
Volcano_498 3:70e14f1577f7 281 buttonTwoFlag = !buttonTwoFlag;
Volcano_498 1:454dddb8adc2 282 }
Volcano_498 1:454dddb8adc2 283 }
Volcano_498 1:454dddb8adc2 284
Volcano_498 1:454dddb8adc2 285 int buttonThreeFlag = 0; /*!< Button Three flag set in ISR */
Volcano_498 1:454dddb8adc2 286 int buttonThreeAltFlag = 0; /*!< Button Three Alternate flag set in ISR */
Volcano_498 1:454dddb8adc2 287
Volcano_498 3:70e14f1577f7 288 void buttonThreePressed()
Volcano_498 3:70e14f1577f7 289 {
Volcano_498 3:70e14f1577f7 290 if(menuButtonFlag) {
Volcano_498 1:454dddb8adc2 291 menuButtonFlag = 0;
Volcano_498 3:70e14f1577f7 292 buttonThreeAltFlag = !buttonThreeAltFlag;
Volcano_498 3:70e14f1577f7 293 } else
Volcano_498 1:454dddb8adc2 294 buttonThreeFlag = !buttonThreeFlag;
Volcano_498 1:454dddb8adc2 295 }
Volcano_498 1:454dddb8adc2 296
Volcano_498 1:454dddb8adc2 297 int splashFlag = 1; /*!< Splash flag set to continue with program flow for the main function before proceeding to while(1) */
Volcano_498 1:454dddb8adc2 298
Volcano_498 3:70e14f1577f7 299 void splashDelay()
Volcano_498 3:70e14f1577f7 300 {
Volcano_498 1:454dddb8adc2 301 splashFlag = 0;
Volcano_498 1:454dddb8adc2 302 }
Volcano_498 1:454dddb8adc2 303
Volcano_498 3:70e14f1577f7 304 void displayInitSplash() //display splash screen
Volcano_498 3:70e14f1577f7 305 {
Volcano_498 1:454dddb8adc2 306 lcd.printString("Welcome to",15,1);
Volcano_498 1:454dddb8adc2 307 lcd.printString("Portable Weat.",3,2);
Volcano_498 3:70e14f1577f7 308 lcd.printString("Station",20,3);
Volcano_498 1:454dddb8adc2 309 lcd.printString("by Volkan",20,4);
Volcano_498 1:454dddb8adc2 310 lcd.printString("Esendag",20,5);
Volcano_498 2:08f2469728d5 311 }
Volcano_498 2:08f2469728d5 312
Volcano_498 2:08f2469728d5 313 //1 bar is 100000 Pa. An atm is 101325 Pa. Therefore an mb is 100 Pa.
Volcano_498 2:08f2469728d5 314
Volcano_498 2:08f2469728d5 315 /**
Volcano_498 2:08f2469728d5 316 Fetches readings from the sensor, converts them into its internal parameters and calculates altitude from pressure data.
Volcano_498 2:08f2469728d5 317 Then prints them on the screen and overwrites on the mbed's Flash memory.
Volcano_498 2:08f2469728d5 318 @param temp - temperature reading from the BMP180(˚C).
Volcano_498 2:08f2469728d5 319 @param press - pressure reading from the BMP180(mb).
Volcano_498 2:08f2469728d5 320 @param altitude - altitude calculated from the pressure reading (m).
Volcano_498 2:08f2469728d5 321 */
Volcano_498 2:08f2469728d5 322
Volcano_498 3:70e14f1577f7 323
Volcano_498 0:b30f86a9a1c5 324
Volcano_498 3:70e14f1577f7 325 int main()
Volcano_498 3:70e14f1577f7 326 {
Volcano_498 3:70e14f1577f7 327
Volcano_498 1:454dddb8adc2 328 splashFlip.attach(&splashDelay,3.0); //attach timer and wait for ISR to be called
Volcano_498 3:70e14f1577f7 329
Volcano_498 1:454dddb8adc2 330 // first need to initialise display
Volcano_498 1:454dddb8adc2 331 lcd.init();
Volcano_498 3:70e14f1577f7 332
Volcano_498 1:454dddb8adc2 333 displayInitSplash();
Volcano_498 3:70e14f1577f7 334
Volcano_498 0:b30f86a9a1c5 335 //initialise barometer
Volcano_498 0:b30f86a9a1c5 336 bmp180.init();
Volcano_498 3:70e14f1577f7 337
Volcano_498 1:454dddb8adc2 338 PHY_PowerDown(); //powers down the Ethernet feature.
Volcano_498 1:454dddb8adc2 339
Volcano_498 1:454dddb8adc2 340 int result = semihost_powerdown(); //as a result, power down the USB connection
Volcano_498 1:454dddb8adc2 341 //unless the cable is connected.
Volcano_498 3:70e14f1577f7 342
Volcano_498 1:454dddb8adc2 343 Measurement measurement; // object created for pressure & temperature using the structure declared in BMP180 class
Volcano_498 3:70e14f1577f7 344
Volcano_498 3:70e14f1577f7 345 while(1) {
Volcano_498 3:70e14f1577f7 346
Volcano_498 3:70e14f1577f7 347 if(splashFlag == 0) { //if ISR has been called proceed with program flow
Volcano_498 3:70e14f1577f7 348 splashFlip.detach(); //detach Timeout object
Volcano_498 3:70e14f1577f7 349 clearCells();
Volcano_498 3:70e14f1577f7 350 lcd.refresh();
Volcano_498 3:70e14f1577f7 351
Volcano_498 1:454dddb8adc2 352 //read values (T in degrees Celsius and P in mb).
Volcano_498 1:454dddb8adc2 353 measurement = bmp180.readValues();
Volcano_498 2:08f2469728d5 354 temp = measurement.temperature;
Volcano_498 2:08f2469728d5 355 press = measurement.pressure;
Volcano_498 2:08f2469728d5 356 //formula for calculating altitude from sea level by using atmospheric pressure. Unit in metres.
Volcano_498 2:08f2469728d5 357 //use pow(double a,double b) for indices, not the ^ sign. Just a reminder!
Volcano_498 2:08f2469728d5 358 altitude = 44330.0*(1.0-(pow((press/PNought),(1.0/5.255))));
Volcano_498 2:08f2469728d5 359 printReadings();
Volcano_498 3:70e14f1577f7 360 lcd.refresh();
Volcano_498 3:70e14f1577f7 361
Volcano_498 3:70e14f1577f7 362 //Sleep(); //put the mbed to sleep once the program flow has been completed.
Volcano_498 3:70e14f1577f7 363
Volcano_498 3:70e14f1577f7 364 wait(1.0);
Volcano_498 3:70e14f1577f7 365
Volcano_498 3:70e14f1577f7 366 } //close if
Volcano_498 3:70e14f1577f7 367 } //close while
Volcano_498 1:454dddb8adc2 368 } //terminate main()
Volcano_498 2:08f2469728d5 369
Volcano_498 3:70e14f1577f7 370 void printReadings()
Volcano_498 3:70e14f1577f7 371 {
Volcano_498 2:08f2469728d5 372 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
Volcano_498 3:70e14f1577f7 373 // so can display a string of a maximum 14 characters in length
Volcano_498 3:70e14f1577f7 374 // or create formatted strings - ensure they aren't more than 14 characters long
Volcano_498 3:70e14f1577f7 375
Volcano_498 3:70e14f1577f7 376 int length = sprintf(buffer,"T = %.2f 'C",temp); // print formatted data to buffer
Volcano_498 3:70e14f1577f7 377 // it is important the format specifier ensures the length will fit in the buffer
Volcano_498 3:70e14f1577f7 378 if (length <= 14) { // if string will fit on display
Volcano_498 3:70e14f1577f7 379 lcd.printString(buffer,0,1); // display on screen. Column 0, row 1.
Volcano_498 3:70e14f1577f7 380 }
Volcano_498 3:70e14f1577f7 381
Volcano_498 3:70e14f1577f7 382 length = sprintf(buffer,"P = %.2f mb",press);
Volcano_498 3:70e14f1577f7 383 if (length <= 14) {
Volcano_498 3:70e14f1577f7 384 lcd.printString(buffer,0,2); // Column 0, row 2.
Volcano_498 3:70e14f1577f7 385 }
Volcano_498 3:70e14f1577f7 386
Volcano_498 3:70e14f1577f7 387 length = sprintf(buffer,"A = %.1f m",altitude);
Volcano_498 3:70e14f1577f7 388 if (length <= 14) {
Volcano_498 3:70e14f1577f7 389 lcd.printString(buffer,0,3); //Column 0, row 3.
Volcano_498 3:70e14f1577f7 390 }
Volcano_498 3:70e14f1577f7 391 if (press++) {
Volcano_498 3:70e14f1577f7 392 lcd.printString("Hotter Weather",2,4); //column 2, row 4.
Volcano_498 3:70e14f1577f7 393 lcd.printString("On the Way!",2,5); //column 2 , row 5.
Volcano_498 3:70e14f1577f7 394 }
Volcano_498 3:70e14f1577f7 395 else if (press--) {
Volcano_498 3:70e14f1577f7 396 lcd.printString("The temperature",2,4); //column 2, row 4.
Volcano_498 3:70e14f1577f7 397 lcd.printString("is predicted",2,5); //column 2 , row 5.
Volcano_498 3:70e14f1577f7 398 lcd.printString("to fall!",2,6); //column 2, row 4.
Volcano_498 3:70e14f1577f7 399 }
Volcano_498 3:70e14f1577f7 400 }
Volcano_498 3:70e14f1577f7 401
Volcano_498 3:70e14f1577f7 402 void clearCells()
Volcano_498 3:70e14f1577f7 403 {
Volcano_498 3:70e14f1577f7 404 //loop through cells, and clear them
Volcano_498 3:70e14f1577f7 405 for (int i = 0; i < 83 ; i++) {
Volcano_498 3:70e14f1577f7 406 for (int j = 0; j < 47; j++) {
Volcano_498 3:70e14f1577f7 407 lcd.clearPixel(i,j); //function to "kill" all cells after looping through.
Volcano_498 2:08f2469728d5 408 }
Volcano_498 3:70e14f1577f7 409 }
Volcano_498 3:70e14f1577f7 410 lcd.refresh(); //must refresh to write buffer to display
Volcano_498 2:08f2469728d5 411 }