Volkan Esendag / Mbed 2 deprecated mbed_PortableWeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

Committer:
Volcano_498
Date:
Sun Apr 19 20:55:49 2015 +0000
Revision:
9:693f69e0a175
Parent:
8:29ac7d274ae0
Child:
10:06b35733ca25
Visual and audible feedback added for operational states.

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 7:104ac8e707e6 17 @date 11 March 2015 (created) / 19 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 6:0aca5c17c988 89 char tempUnit [4] = {'C','F','K','R'}; //character that stores temperature unit type.
Volcano_498 6:0aca5c17c988 90
Volcano_498 6:0aca5c17c988 91 char pressUnit[3] = {'M','P','A'}; //character that stores pressure units. M - millibars P - Pascals A - atmospheres
Volcano_498 6:0aca5c17c988 92
Volcano_498 6:0aca5c17c988 93 char AltUnit[3] = {'m','f','y'}; //character that stores altitude units. m - metres f - feet y - yards
Volcano_498 2:08f2469728d5 94
Volcano_498 1:454dddb8adc2 95 /**
Volcano_498 1:454dddb8adc2 96 @namespace bmp180
Volcano_498 1:454dddb8adc2 97 @brief A special structed type of I2C object created for the BMP180.
Volcano_498 1:454dddb8adc2 98 @brief For more info, see the BMP180 library by Craig Evans.
Volcano_498 1:454dddb8adc2 99 @see http://developer.mbed.org/users/eencae/code/BMP180/
Volcano_498 1:454dddb8adc2 100 */
Volcano_498 1:454dddb8adc2 101 BMP180 bmp180(p28,p27);
Volcano_498 1:454dddb8adc2 102 //Pins are declared in the public domain and the sensor itself acts on private variables.
Volcano_498 0:b30f86a9a1c5 103
Volcano_498 1:454dddb8adc2 104 /**
Volcano_498 1:454dddb8adc2 105 @namespace buzzerPwm
Volcano_498 1:454dddb8adc2 106 @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 107 @brief Used for an audible feedback should the temperature reading exceed a certain value.
Volcano_498 1:454dddb8adc2 108 */
Volcano_498 1:454dddb8adc2 109 PwmOut buzzerPwm(p24);
Volcano_498 0:b30f86a9a1c5 110
Volcano_498 1:454dddb8adc2 111 /**
Volcano_498 1:454dddb8adc2 112 @namespace redLED
Volcano_498 1:454dddb8adc2 113 @brief PwmOut object to apply a PWM signal to the red visual feedback LED via pin 22.
Volcano_498 1:454dddb8adc2 114 @brief Used for visual feedback to tell the user a certain temperature threshold has been reached.
Volcano_498 1:454dddb8adc2 115 */
Volcano_498 1:454dddb8adc2 116 PwmOut redLED(p22);
Volcano_498 1:454dddb8adc2 117
Volcano_498 1:454dddb8adc2 118 /**
Volcano_498 1:454dddb8adc2 119 @namespace greenLED
Volcano_498 1:454dddb8adc2 120 @brief PwmOut object to apply a PWM signal to the green visual feedback LED via pin 23.
Volcano_498 1:454dddb8adc2 121 @brief Used to let the user know the device is operating normally.
Volcano_498 1:454dddb8adc2 122 */
Volcano_498 1:454dddb8adc2 123 PwmOut greenLED(p23);
Volcano_498 1:454dddb8adc2 124
Volcano_498 1:454dddb8adc2 125 /**
Volcano_498 1:454dddb8adc2 126 @namespace serial
Volcano_498 1:454dddb8adc2 127 @brief Serial object to print readings over a USB cable and display them on a terminal.
Volcano_498 1:454dddb8adc2 128 */
Volcano_498 0:b30f86a9a1c5 129 Serial serial(USBTX,USBRX); //serial object to print readings for debugging WHILE the USB cable is connected.
Volcano_498 0:b30f86a9a1c5 130
Volcano_498 1:454dddb8adc2 131 /**
Volcano_498 1:454dddb8adc2 132 @namespace menuButton
Volcano_498 1:454dddb8adc2 133 @brief Interrupt object to call ISR for the designated menu button when an input to p15 is applied.
Volcano_498 1:454dddb8adc2 134 @namespace buttonOne
Volcano_498 1:454dddb8adc2 135 @brief Interrupt object to call ISR for Button 1 when an input to p16 is applied.
Volcano_498 1:454dddb8adc2 136 @namespace buttonTwo
Volcano_498 1:454dddb8adc2 137 @brief Interrupt object to call ISR for Button 2 when an input to p17 is applied.
Volcano_498 1:454dddb8adc2 138 @namespace buttonThree
Volcano_498 1:454dddb8adc2 139 @brief Interrupt object to call ISR for Button 3 when an input to p18 is applied.
Volcano_498 1:454dddb8adc2 140 */
Volcano_498 0:b30f86a9a1c5 141
Volcano_498 1:454dddb8adc2 142 InterruptIn menuButton(p15); //Interrupt object for the designated menu button.
Volcano_498 1:454dddb8adc2 143 InterruptIn buttonOne(p16); //Interrupt objects for the other buttons.
Volcano_498 1:454dddb8adc2 144 InterruptIn buttonTwo(p17);
Volcano_498 1:454dddb8adc2 145 InterruptIn buttonThree(p18);
Volcano_498 0:b30f86a9a1c5 146
Volcano_498 1:454dddb8adc2 147 /**
Volcano_498 1:454dddb8adc2 148 @namespace potAin
Volcano_498 1:454dddb8adc2 149 @brief Analogue input from potentiometer whose Vout is connected to pin 20.
Volcano_498 1:454dddb8adc2 150 */
Volcano_498 1:454dddb8adc2 151
Volcano_498 1:454dddb8adc2 152 AnalogIn potAin(p20); //Potentiometer feedback pin to the mbed.
Volcano_498 1:454dddb8adc2 153
Volcano_498 1:454dddb8adc2 154 /**
Volcano_498 1:454dddb8adc2 155 @namespace logTimer
Volcano_498 3:70e14f1577f7 156 @brief Ticker object to record/log readings with a specified interval. Can be varied with Interrupt Service Routines.
Volcano_498 1:454dddb8adc2 157 */
Volcano_498 1:454dddb8adc2 158
Volcano_498 1:454dddb8adc2 159 Ticker logTimer; //Ticker object to call ISR for a specified period of time.
Volcano_498 1:454dddb8adc2 160
Volcano_498 3:70e14f1577f7 161 /**
Volcano_498 3:70e14f1577f7 162 @namespace displayTimer
Volcano_498 3:70e14f1577f7 163 @brief Ticker object to display readings on screen with a specified interval. Can be varied with Interrupt Service Routines.
Volcano_498 3:70e14f1577f7 164 */
Volcano_498 6:0aca5c17c988 165 Ticker displayTimer; //ticker object to display readings
Volcano_498 3:70e14f1577f7 166
Volcano_498 1:454dddb8adc2 167 /*
Volcano_498 1:454dddb8adc2 168 @namespace leds
Volcano_498 1:454dddb8adc2 169 @brief GPIO output for status LEDs - used to display error message or as a flash memory overwrite operation feedback.
Volcano_498 1:454dddb8adc2 170 */
Volcano_498 1:454dddb8adc2 171
Volcano_498 1:454dddb8adc2 172 BusOut leds(LED1,LED2,LED3,LED4); //BusOut object for error feedback LEDs.
Volcano_498 1:454dddb8adc2 173 //configure pins of the LCD display...
Volcano_498 1:454dddb8adc2 174
Volcano_498 1:454dddb8adc2 175 /**
Volcano_498 1:454dddb8adc2 176 @namespace splashFlip
Volcano_498 1:454dddb8adc2 177 @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 178 */
Volcano_498 1:454dddb8adc2 179 Timeout splashFlip;
Volcano_498 1:454dddb8adc2 180
Volcano_498 1:454dddb8adc2 181 /**
Volcano_498 1:454dddb8adc2 182 @namespace lcd
Volcano_498 1:454dddb8adc2 183 @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 184 @see https://developer.mbed.org/users/eencae/code/N5110/
Volcano_498 1:454dddb8adc2 185 */
Volcano_498 6:0aca5c17c988 186 void printReadings(); // declare function to print readings here, which is then defined after the main() function.
Volcano_498 6:0aca5c17c988 187 void clearCells(); //declare function to clear all cells should there be a need for it. Even though lcd.clear() also does the job this can be handy in case of failure.
Volcano_498 2:08f2469728d5 188
Volcano_498 6:0aca5c17c988 189 float temp = 0.0; //declare the temp variable for temperature universally so that it can be shared across functions to represent temperature.
Volcano_498 6:0aca5c17c988 190 float press = 0.0; //do the same for pressure using press.
Volcano_498 6:0aca5c17c988 191 float altitude = 0.0; //and altitude using, well, altitude :P
Volcano_498 2:08f2469728d5 192
Volcano_498 6:0aca5c17c988 193 int i = 0; //represents the column number (horizontal pixel number) of the display.
Volcano_498 6:0aca5c17c988 194 int j = 0; //represents the row number of the display.
Volcano_498 3:70e14f1577f7 195
Volcano_498 5:6d85cafa1085 196 int dispSetting = 0; //set display setting to default.
Volcano_498 5:6d85cafa1085 197 int recSetting = 3; //set log setting to default.
Volcano_498 5:6d85cafa1085 198 int tempSetting = 2; //set temperature threshold to default.
Volcano_498 5:6d85cafa1085 199
Volcano_498 6:0aca5c17c988 200 int tempUnitSetting = 0; //set temperature unit setting to default.
Volcano_498 6:0aca5c17c988 201 int pressUnitSetting = 0; //set pressure unit setting to default.
Volcano_498 6:0aca5c17c988 202 int altUnitSetting = 0; //and do the same for altitude.
Volcano_498 6:0aca5c17c988 203 int subMenuId = 0; //int used to store sub-menu number. For instance pressing the menu button once and then Button One gives Sub-Menu 1.
Volcano_498 6:0aca5c17c988 204
Volcano_498 1:454dddb8adc2 205 N5110 lcd(p7,p8,p9,p10,p11,p13,p21); //VCC,SCE,RST,DC,MOSI,SCLK,BACKLIGHT
Volcano_498 0:b30f86a9a1c5 206
Volcano_498 0:b30f86a9a1c5 207 ///////////The following pieces of code are to configure real-time clock for the data logger.*************
Volcano_498 0:b30f86a9a1c5 208
Volcano_498 1:454dddb8adc2 209 char rxString[16]; // buffer to store received string. Each character is a byte long - hence the char pointer type.
Volcano_498 1:454dddb8adc2 210
Volcano_498 1:454dddb8adc2 211 int setTimeFlag = 0; /*!< set time flag set in serial ISR */
Volcano_498 0:b30f86a9a1c5 212
Volcano_498 1:454dddb8adc2 213 /**
Volcano_498 1:454dddb8adc2 214 Reads string input via serial interrupt and converts it into real time
Volcano_498 1:454dddb8adc2 215 @param rxString - string received from serial
Volcano_498 1:454dddb8adc2 216 @param time - integer that represents time, converted from input string (rxString).
Volcano_498 0:b30f86a9a1c5 217
Volcano_498 1:454dddb8adc2 218 */
Volcano_498 0:b30f86a9a1c5 219 void setTime()
Volcano_498 0:b30f86a9a1c5 220 {
Volcano_498 0:b30f86a9a1c5 221 // print time for debugging
Volcano_498 0:b30f86a9a1c5 222 serial.printf("set_time - %s",rxString);
Volcano_498 0:b30f86a9a1c5 223 // atoi() converts a string to an integer
Volcano_498 0:b30f86a9a1c5 224 int time = atoi(rxString);
Volcano_498 0:b30f86a9a1c5 225 // update the time
Volcano_498 0:b30f86a9a1c5 226 set_time(time);
Volcano_498 0:b30f86a9a1c5 227 }
Volcano_498 0:b30f86a9a1c5 228
Volcano_498 0:b30f86a9a1c5 229 void serialISR()
Volcano_498 0:b30f86a9a1c5 230 {
Volcano_498 0:b30f86a9a1c5 231 // 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 232 serial.gets(rxString,16);
Volcano_498 0:b30f86a9a1c5 233 // set flag
Volcano_498 0:b30f86a9a1c5 234 setTimeFlag = 1;
Volcano_498 0:b30f86a9a1c5 235 }
Volcano_498 0:b30f86a9a1c5 236
Volcano_498 1:454dddb8adc2 237 /**
Volcano_498 1:454dddb8adc2 238 Disables USB interface when the mbed's USB cable isn't attached.
Volcano_498 1:454dddb8adc2 239 @param arg - argument function (pointer type int/uint32_t)
Volcano_498 1:454dddb8adc2 240 @returns __semihost(USR_POWERDOWN, &arg)
Volcano_498 1:454dddb8adc2 241 */
Volcano_498 1:454dddb8adc2 242
Volcano_498 3:70e14f1577f7 243 int semihost_powerdown()
Volcano_498 3:70e14f1577f7 244 {
Volcano_498 3:70e14f1577f7 245
Volcano_498 1:454dddb8adc2 246 uint32_t arg; //variable for return function
Volcano_498 1:454dddb8adc2 247 return __semihost(USR_POWERDOWN, &arg); //return semihost state...
Volcano_498 3:70e14f1577f7 248
Volcano_498 1:454dddb8adc2 249 } //...to power down the USB interface when the USB cable is detached.
Volcano_498 1:454dddb8adc2 250
Volcano_498 1:454dddb8adc2 251 LocalFileSystem local("local"); // create local filesystem
Volcano_498 1:454dddb8adc2 252
Volcano_498 1:454dddb8adc2 253 void writeDataToFile(float data, float dataTwo, char dataThree[30])
Volcano_498 1:454dddb8adc2 254 {
Volcano_498 1:454dddb8adc2 255 time_t seconds = time(NULL); // get current time
Volcano_498 1:454dddb8adc2 256 strftime(dataThree, 30 , "%R %x", localtime(&seconds)); //convert it into a string, from an array size of 30.
Volcano_498 3:70e14f1577f7 257
Volcano_498 1:454dddb8adc2 258 leds = 15; // turn on LEDs for feedback
Volcano_498 1:454dddb8adc2 259 FILE *fp = fopen("/local/tempLog.csv", "a"); // open 'log.txt' for appending. Instance of class FILE.
Volcano_498 1:454dddb8adc2 260 // if the file doesn't exist it is created, if it exists, data is appended to the end
Volcano_498 3:70e14f1577f7 261 fprintf(fp,"%s, %.2f , %.2f \n",dataThree,dataTwo,data); // print string to file
Volcano_498 1:454dddb8adc2 262 fclose(fp); // close file
Volcano_498 1:454dddb8adc2 263 leds = 0; // turn off LEDs to signify file access has finished
Volcano_498 1:454dddb8adc2 264 }
Volcano_498 1:454dddb8adc2 265
Volcano_498 1:454dddb8adc2 266 int menuButtonFlag = 0; /*!< menu button flag set in ISR */
Volcano_498 1:454dddb8adc2 267
Volcano_498 1:454dddb8adc2 268 int buttonOneFlag = 0; /*!< Button One flag set in ISR */
Volcano_498 1:454dddb8adc2 269 int buttonOneAltFlag = 0; /*!< Button One Alternate flag set in ISR */
Volcano_498 1:454dddb8adc2 270
Volcano_498 3:70e14f1577f7 271 void buttonOnePressed()
Volcano_498 3:70e14f1577f7 272 {
Volcano_498 6:0aca5c17c988 273 if(menuButtonFlag > 0) { //if menu button has been pressed and main menu entered
Volcano_498 1:454dddb8adc2 274 buttonOneAltFlag = !buttonOneAltFlag; //set/reset-if-set alternate flag and proceed to next menu
Volcano_498 6:0aca5c17c988 275 }
Volcano_498 6:0aca5c17c988 276 else {
Volcano_498 1:454dddb8adc2 277 buttonOneFlag = !buttonOneFlag; //set/reset-if-set flag if not navigated to a menu
Volcano_498 1:454dddb8adc2 278 }
Volcano_498 1:454dddb8adc2 279 }
Volcano_498 1:454dddb8adc2 280
Volcano_498 1:454dddb8adc2 281 int buttonTwoFlag = 0; /*!< Button Two flag set in ISR */
Volcano_498 1:454dddb8adc2 282 int buttonTwoAltFlag = 0; /*!< Button Two Alternate flag set in ISR */
Volcano_498 1:454dddb8adc2 283
Volcano_498 3:70e14f1577f7 284 void buttonTwoPressed()
Volcano_498 3:70e14f1577f7 285 {
Volcano_498 6:0aca5c17c988 286 if(menuButtonFlag > 0) {
Volcano_498 3:70e14f1577f7 287 buttonTwoAltFlag = !buttonTwoAltFlag;
Volcano_498 6:0aca5c17c988 288 }
Volcano_498 6:0aca5c17c988 289 else {
Volcano_498 3:70e14f1577f7 290 buttonTwoFlag = !buttonTwoFlag;
Volcano_498 1:454dddb8adc2 291 }
Volcano_498 1:454dddb8adc2 292 }
Volcano_498 1:454dddb8adc2 293
Volcano_498 1:454dddb8adc2 294 int buttonThreeFlag = 0; /*!< Button Three flag set in ISR */
Volcano_498 1:454dddb8adc2 295 int buttonThreeAltFlag = 0; /*!< Button Three Alternate flag set in ISR */
Volcano_498 1:454dddb8adc2 296
Volcano_498 3:70e14f1577f7 297 void buttonThreePressed()
Volcano_498 3:70e14f1577f7 298 {
Volcano_498 5:6d85cafa1085 299 if(menuButtonFlag > 0) {
Volcano_498 3:70e14f1577f7 300 buttonThreeAltFlag = !buttonThreeAltFlag;
Volcano_498 6:0aca5c17c988 301 }
Volcano_498 6:0aca5c17c988 302 else
Volcano_498 1:454dddb8adc2 303 buttonThreeFlag = !buttonThreeFlag;
Volcano_498 1:454dddb8adc2 304 }
Volcano_498 1:454dddb8adc2 305
Volcano_498 7:104ac8e707e6 306 void menuButtonPressed()
Volcano_498 7:104ac8e707e6 307 {
Volcano_498 7:104ac8e707e6 308 if(buttonOneAltFlag == 0 && buttonTwoAltFlag == 0 && buttonThreeAltFlag == 0){ //if no flag is set and therefore no menu accessed
Volcano_498 7:104ac8e707e6 309 menuButtonFlag++; //increment the flag to access different menu states.
Volcano_498 7:104ac8e707e6 310
Volcano_498 7:104ac8e707e6 311 if(menuButtonFlag > 2) { //if menu button has been clicked three times
Volcano_498 7:104ac8e707e6 312 menuButtonFlag = 0; //go back to the measurements menu
Volcano_498 7:104ac8e707e6 313 }
Volcano_498 7:104ac8e707e6 314 }
Volcano_498 7:104ac8e707e6 315 }
Volcano_498 7:104ac8e707e6 316
Volcano_498 5:6d85cafa1085 317 int splashFlag = 1; /*!< Splash flag set to continue with program flow for the main function before proceeding with program flow */
Volcano_498 5:6d85cafa1085 318
Volcano_498 5:6d85cafa1085 319 /**
Volcano_498 5:6d85cafa1085 320 Interrupt Service Routine to reset splashFlag when Timeout has been performed.
Volcano_498 5:6d85cafa1085 321 No parameters to be entered by, or values to be returned to, the user.
Volcano_498 5:6d85cafa1085 322 */
Volcano_498 1:454dddb8adc2 323
Volcano_498 3:70e14f1577f7 324 void splashDelay()
Volcano_498 3:70e14f1577f7 325 {
Volcano_498 1:454dddb8adc2 326 splashFlag = 0;
Volcano_498 1:454dddb8adc2 327 }
Volcano_498 1:454dddb8adc2 328
Volcano_498 5:6d85cafa1085 329 int dispTimerFlag = 0; /*!< Display flag set to 0 initially. Used to update values on the display when the ISR is called.*/
Volcano_498 5:6d85cafa1085 330
Volcano_498 5:6d85cafa1085 331 /**
Volcano_498 5:6d85cafa1085 332 Interrupt Service Routine to set dispTimerFlag when Ticker duration has elapsed.
Volcano_498 5:6d85cafa1085 333 No parameters to be entered by, or values to be returned to, the user.
Volcano_498 5:6d85cafa1085 334 */
Volcano_498 5:6d85cafa1085 335 void timerExpiDisplay()
Volcano_498 5:6d85cafa1085 336 {
Volcano_498 5:6d85cafa1085 337 dispTimerFlag = 1;
Volcano_498 5:6d85cafa1085 338 }
Volcano_498 5:6d85cafa1085 339
Volcano_498 5:6d85cafa1085 340 int logTimerFlag = 0; /*!< Log flag set to 0 initially. Used to overwrite the Flash Memory when the ISR is called.*/
Volcano_498 5:6d85cafa1085 341
Volcano_498 5:6d85cafa1085 342 /**
Volcano_498 5:6d85cafa1085 343 Interrupt Service Routine to set logTimerFlag when Ticker duration has elapsed.
Volcano_498 5:6d85cafa1085 344 No parameters to be entered by, or values to be returned to, the user.
Volcano_498 5:6d85cafa1085 345 */
Volcano_498 5:6d85cafa1085 346 void timerExpiLog()
Volcano_498 5:6d85cafa1085 347 {
Volcano_498 6:0aca5c17c988 348 logTimerFlag = 1;
Volcano_498 5:6d85cafa1085 349 }
Volcano_498 5:6d85cafa1085 350
Volcano_498 3:70e14f1577f7 351 void displayInitSplash() //display splash screen
Volcano_498 3:70e14f1577f7 352 {
Volcano_498 1:454dddb8adc2 353 lcd.printString("Welcome to",15,1);
Volcano_498 1:454dddb8adc2 354 lcd.printString("Portable Weat.",3,2);
Volcano_498 3:70e14f1577f7 355 lcd.printString("Station",20,3);
Volcano_498 1:454dddb8adc2 356 lcd.printString("by Volkan",20,4);
Volcano_498 1:454dddb8adc2 357 lcd.printString("Esendag",20,5);
Volcano_498 2:08f2469728d5 358 }
Volcano_498 2:08f2469728d5 359
Volcano_498 6:0aca5c17c988 360 void displayMenuOne(){
Volcano_498 6:0aca5c17c988 361 lcd.printString("Settings Menu",0,0);
Volcano_498 6:0aca5c17c988 362 lcd.printString("One",0,1);
Volcano_498 6:0aca5c17c988 363 lcd.printString("Use Buttons",0,2);
Volcano_498 6:0aca5c17c988 364 lcd.printString("To change",0,3);
Volcano_498 6:0aca5c17c988 365 lcd.printString("Settings",0,4);
Volcano_498 6:0aca5c17c988 366 lcd.refresh();
Volcano_498 6:0aca5c17c988 367 }
Volcano_498 6:0aca5c17c988 368
Volcano_498 6:0aca5c17c988 369 void displayMenuTwo(){
Volcano_498 6:0aca5c17c988 370 lcd.printString("Settings Menu",0,0);
Volcano_498 6:0aca5c17c988 371 lcd.printString("Two",0,1);
Volcano_498 6:0aca5c17c988 372 lcd.printString("Use menuButton",0,2);
Volcano_498 6:0aca5c17c988 373 lcd.printString("To Go Back To",0,3);
Volcano_498 6:0aca5c17c988 374 lcd.printString("Display menu",0,4);
Volcano_498 6:0aca5c17c988 375 lcd.refresh();
Volcano_498 6:0aca5c17c988 376 }
Volcano_498 6:0aca5c17c988 377
Volcano_498 6:0aca5c17c988 378 void displayTempUnit(){
Volcano_498 6:0aca5c17c988 379 lcd.printString("Use Button 2",0,0);
Volcano_498 6:0aca5c17c988 380 lcd.printString("To decrease",0,1);
Volcano_498 6:0aca5c17c988 381 lcd.printString("Temp. Setting;",0,2);
Volcano_498 6:0aca5c17c988 382 lcd.printString("Button 3",0,3);
Volcano_498 6:0aca5c17c988 383 lcd.printString("To increase.",0,4);
Volcano_498 6:0aca5c17c988 384
Volcano_498 6:0aca5c17c988 385 char bufferSt[14]; //buffer to store string
Volcano_498 6:0aca5c17c988 386 sprintf(bufferSt,"current:%c",tempUnit[tempUnitSetting]); //write the typed string and the current unit setting onto the buffer
Volcano_498 6:0aca5c17c988 387
Volcano_498 6:0aca5c17c988 388 lcd.printString(bufferSt,0,5); //print the buffer
Volcano_498 6:0aca5c17c988 389 lcd.refresh(); //needs to refresh to write the string buffers to the display
Volcano_498 6:0aca5c17c988 390 }
Volcano_498 6:0aca5c17c988 391
Volcano_498 6:0aca5c17c988 392 void displayPressUnit(){
Volcano_498 6:0aca5c17c988 393 lcd.printString("Use Button 1",0,0);
Volcano_498 6:0aca5c17c988 394 lcd.printString("To decrease",0,1);
Volcano_498 6:0aca5c17c988 395 lcd.printString("Press Setting;",0,2);
Volcano_498 6:0aca5c17c988 396 lcd.printString("Button 3",0,3);
Volcano_498 6:0aca5c17c988 397 lcd.printString("To increase.",0,4);
Volcano_498 6:0aca5c17c988 398
Volcano_498 6:0aca5c17c988 399 char bufferSt[14]; //buffer to store string
Volcano_498 6:0aca5c17c988 400 sprintf(bufferSt,"current:%c",pressUnit[pressUnitSetting]);
Volcano_498 6:0aca5c17c988 401
Volcano_498 6:0aca5c17c988 402 lcd.printString(bufferSt,0,5);
Volcano_498 6:0aca5c17c988 403 lcd.refresh();
Volcano_498 6:0aca5c17c988 404 }
Volcano_498 6:0aca5c17c988 405
Volcano_498 6:0aca5c17c988 406 void displayDispMenu(){
Volcano_498 6:0aca5c17c988 407 lcd.printString("Use Button 2",0,0);
Volcano_498 6:0aca5c17c988 408 lcd.printString("To decrease",0,1);
Volcano_498 6:0aca5c17c988 409 lcd.printString("Time Setting;",0,2);
Volcano_498 6:0aca5c17c988 410 lcd.printString("Button 3",0,3);
Volcano_498 6:0aca5c17c988 411 lcd.printString("To increase.",0,4);
Volcano_498 6:0aca5c17c988 412
Volcano_498 6:0aca5c17c988 413 char bufferSt[14]; //buffer to store string
Volcano_498 6:0aca5c17c988 414 //just a universally applicable if-else if statement to create a string out of current time setting should more time settings be added...
Volcano_498 6:0aca5c17c988 415 if(dispInterval[dispSetting].time >= 1.0){ //if time setting is a second or more
Volcano_498 6:0aca5c17c988 416 sprintf(bufferSt,"set:%.1f s",dispInterval[dispSetting].time);
Volcano_498 6:0aca5c17c988 417 }
Volcano_498 6:0aca5c17c988 418 else if(dispInterval[dispSetting].time < 1.0){ //if time setting is less than a second
Volcano_498 6:0aca5c17c988 419 float tempTime = dispInterval[dispSetting].time * 1000.0; //convert time to milliseconds
Volcano_498 6:0aca5c17c988 420 sprintf(bufferSt,"set:%.1f ms",tempTime);
Volcano_498 6:0aca5c17c988 421 }
Volcano_498 6:0aca5c17c988 422 lcd.printString(bufferSt,0,5);
Volcano_498 6:0aca5c17c988 423 lcd.refresh();
Volcano_498 6:0aca5c17c988 424 }
Volcano_498 6:0aca5c17c988 425
Volcano_498 6:0aca5c17c988 426 void displayThresholdTemp(){
Volcano_498 6:0aca5c17c988 427 lcd.printString("Use Button 1",0,0);
Volcano_498 6:0aca5c17c988 428 lcd.printString("To decrease",0,1);
Volcano_498 6:0aca5c17c988 429 lcd.printString("Temp Thresh;",0,2);
Volcano_498 6:0aca5c17c988 430 lcd.printString("Button 3",0,3);
Volcano_498 6:0aca5c17c988 431 lcd.printString("To increase.",0,4);
Volcano_498 6:0aca5c17c988 432
Volcano_498 6:0aca5c17c988 433 char bufferSt[14]; //buffer to store string
Volcano_498 6:0aca5c17c988 434 sprintf(bufferSt,"current:%.1f",tempThres[tempSetting].thresTemp);
Volcano_498 6:0aca5c17c988 435
Volcano_498 6:0aca5c17c988 436 lcd.printString(bufferSt,0,5);
Volcano_498 6:0aca5c17c988 437 lcd.refresh();
Volcano_498 6:0aca5c17c988 438 }
Volcano_498 6:0aca5c17c988 439
Volcano_498 6:0aca5c17c988 440 void displayAltitudeUnit(){
Volcano_498 6:0aca5c17c988 441 lcd.printString("Use Button 1",0,0);
Volcano_498 6:0aca5c17c988 442 lcd.printString("To decrease",0,1);
Volcano_498 6:0aca5c17c988 443 lcd.printString("Altitude Set;",0,2);
Volcano_498 6:0aca5c17c988 444 lcd.printString("Button 2",0,3);
Volcano_498 6:0aca5c17c988 445 lcd.printString("To increase.",0,4);
Volcano_498 6:0aca5c17c988 446
Volcano_498 6:0aca5c17c988 447 char bufferSt[14]; //buffer to store string
Volcano_498 6:0aca5c17c988 448 sprintf(bufferSt,"current:%c",AltUnit[altUnitSetting]);
Volcano_498 6:0aca5c17c988 449
Volcano_498 6:0aca5c17c988 450 lcd.printString(bufferSt,0,5);
Volcano_498 6:0aca5c17c988 451 lcd.refresh();
Volcano_498 6:0aca5c17c988 452 }
Volcano_498 6:0aca5c17c988 453
Volcano_498 8:29ac7d274ae0 454 void displayMeasurementTitle(){
Volcano_498 8:29ac7d274ae0 455 // origin x,y,width,height,type
Volcano_498 8:29ac7d274ae0 456 lcd.drawRect(0,0,83,6,0); // transparent, just outline
Volcano_498 8:29ac7d274ae0 457 lcd.drawCircle(10,3,2,0); // x,y,radius,transparent with outline
Volcano_498 8:29ac7d274ae0 458 lcd.drawCircle(14,3,2,1); // x,y,radius,black fill
Volcano_498 8:29ac7d274ae0 459 lcd.drawCircle(18,3,2,0); // x,y,radius,transparent with outline
Volcano_498 8:29ac7d274ae0 460 lcd.drawCircle(22,3,2,1); // x,y,radius,black fill
Volcano_498 8:29ac7d274ae0 461 lcd.drawCircle(26,3,2,0); // x,y,radius,transparent with outline
Volcano_498 8:29ac7d274ae0 462 lcd.drawCircle(30,3,2,1); // x,y,radius,black fill
Volcano_498 8:29ac7d274ae0 463 lcd.drawCircle(34,3,2,0); // x,y,radius,transparent with outline
Volcano_498 8:29ac7d274ae0 464 lcd.drawCircle(38,3,2,1); // x,y,radius,black fill
Volcano_498 8:29ac7d274ae0 465 lcd.drawCircle(34,3,2,0); // x,y,radius,transparent with outline
Volcano_498 8:29ac7d274ae0 466 lcd.drawCircle(46,3,2,1); // x,y,radius,black fill
Volcano_498 8:29ac7d274ae0 467 lcd.drawCircle(50,3,2,0); // x,y,radius,transparent with outline
Volcano_498 8:29ac7d274ae0 468 lcd.drawCircle(54,3,2,1); // x,y,radius,black fill
Volcano_498 8:29ac7d274ae0 469 lcd.drawCircle(58,3,2,0); // x,y,radius,transparent with outline
Volcano_498 8:29ac7d274ae0 470 lcd.drawCircle(62,3,2,1); // x,y,radius,black fill
Volcano_498 8:29ac7d274ae0 471 lcd.drawCircle(66,3,2,0); // x,y,radius,transparent with outline
Volcano_498 8:29ac7d274ae0 472 lcd.drawCircle(70,3,2,1); // x,y,radius,black fill
Volcano_498 8:29ac7d274ae0 473 lcd.drawCircle(74,3,2,0); // x,y,radius,transparent with outline
Volcano_498 8:29ac7d274ae0 474
Volcano_498 8:29ac7d274ae0 475 // x0,y0,x1,y1,type 0-white,1-black,2-dotted
Volcano_498 8:29ac7d274ae0 476 lcd.drawLine(0,0,6,6,1);
Volcano_498 8:29ac7d274ae0 477 lcd.drawLine(0,6,6,0,1);
Volcano_498 8:29ac7d274ae0 478 /*
Volcano_498 8:29ac7d274ae0 479 * *
Volcano_498 8:29ac7d274ae0 480 * *
Volcano_498 8:29ac7d274ae0 481 * *
Volcano_498 8:29ac7d274ae0 482 * Black line with intercept; (3,3).
Volcano_498 8:29ac7d274ae0 483 * *
Volcano_498 8:29ac7d274ae0 484 * *
Volcano_498 8:29ac7d274ae0 485 * *
Volcano_498 8:29ac7d274ae0 486 */
Volcano_498 8:29ac7d274ae0 487 lcd.drawLine(77,0,83,6,1);
Volcano_498 8:29ac7d274ae0 488 lcd.drawLine(77,6,83,0,1);
Volcano_498 8:29ac7d274ae0 489 }
Volcano_498 8:29ac7d274ae0 490
Volcano_498 2:08f2469728d5 491 //1 bar is 100000 Pa. An atm is 101325 Pa. Therefore an mb is 100 Pa.
Volcano_498 2:08f2469728d5 492
Volcano_498 3:70e14f1577f7 493 int main()
Volcano_498 3:70e14f1577f7 494 {
Volcano_498 3:70e14f1577f7 495
Volcano_498 1:454dddb8adc2 496 splashFlip.attach(&splashDelay,3.0); //attach timer and wait for ISR to be called
Volcano_498 3:70e14f1577f7 497
Volcano_498 6:0aca5c17c988 498 displayTimer.attach(&timerExpiDisplay,dispInterval[dispSetting].time); //do the same for display dispInterval[dispSetting].time
Volcano_498 6:0aca5c17c988 499
Volcano_498 6:0aca5c17c988 500 //logTimer.attach(&timerExpiLog,recInterval[recSetting].time);
Volcano_498 6:0aca5c17c988 501
Volcano_498 6:0aca5c17c988 502 menuButton.rise(&menuButtonPressed); //event generated on rising edge (a positive spike in voltage), indicated by .rise
Volcano_498 5:6d85cafa1085 503
Volcano_498 6:0aca5c17c988 504 buttonOne.rise(&buttonOnePressed);
Volcano_498 6:0aca5c17c988 505
Volcano_498 6:0aca5c17c988 506 buttonTwo.rise(&buttonTwoPressed);
Volcano_498 6:0aca5c17c988 507
Volcano_498 6:0aca5c17c988 508 buttonThree.rise(&buttonThreePressed);
Volcano_498 5:6d85cafa1085 509
Volcano_498 1:454dddb8adc2 510 // first need to initialise display
Volcano_498 1:454dddb8adc2 511 lcd.init();
Volcano_498 3:70e14f1577f7 512
Volcano_498 1:454dddb8adc2 513 displayInitSplash();
Volcano_498 3:70e14f1577f7 514
Volcano_498 0:b30f86a9a1c5 515 //initialise barometer
Volcano_498 0:b30f86a9a1c5 516 bmp180.init();
Volcano_498 3:70e14f1577f7 517
Volcano_498 1:454dddb8adc2 518 PHY_PowerDown(); //powers down the Ethernet feature.
Volcano_498 1:454dddb8adc2 519
Volcano_498 1:454dddb8adc2 520 int result = semihost_powerdown(); //as a result, power down the USB connection
Volcano_498 1:454dddb8adc2 521 //unless the cable is connected.
Volcano_498 3:70e14f1577f7 522
Volcano_498 1:454dddb8adc2 523 Measurement measurement; // object created for pressure & temperature using the structure declared in BMP180 class
Volcano_498 3:70e14f1577f7 524
Volcano_498 3:70e14f1577f7 525 while(1) {
Volcano_498 3:70e14f1577f7 526
Volcano_498 3:70e14f1577f7 527 if(splashFlag == 0) { //if ISR has been called proceed with program flow
Volcano_498 3:70e14f1577f7 528 splashFlip.detach(); //detach Timeout object
Volcano_498 5:6d85cafa1085 529 lcd.clear();
Volcano_498 3:70e14f1577f7 530 clearCells();
Volcano_498 6:0aca5c17c988 531 lcd.refresh(); //need to refresh to write buffer on lcd
Volcano_498 3:70e14f1577f7 532
Volcano_498 5:6d85cafa1085 533 if(dispTimerFlag) {
Volcano_498 5:6d85cafa1085 534 //read values (T in degrees Celsius and P in mb).
Volcano_498 8:29ac7d274ae0 535 displayMeasurementTitle();
Volcano_498 5:6d85cafa1085 536 measurement = bmp180.readValues();
Volcano_498 5:6d85cafa1085 537 temp = measurement.temperature;
Volcano_498 5:6d85cafa1085 538 press = measurement.pressure;
Volcano_498 5:6d85cafa1085 539 /*formula for calculating altitude from sea level by using atmospheric pressure. Unit in metres.
Volcano_498 5:6d85cafa1085 540 Use pow(double a,double b) for indices, not the ^ sign. Just a reminder! Also check out this site:
Volcano_498 5:6d85cafa1085 541 http://www.mide.com/products/slamstick/air-pressure-altitude-calculator.php
Volcano_498 5:6d85cafa1085 542 */
Volcano_498 5:6d85cafa1085 543 altitude = 44330.0*(1.0-(pow((press/PNought),(1.0/5.255))));
Volcano_498 6:0aca5c17c988 544 dispTimerFlag = 0;
Volcano_498 6:0aca5c17c988 545
Volcano_498 5:6d85cafa1085 546 printReadings();
Volcano_498 5:6d85cafa1085 547 lcd.refresh();
Volcano_498 8:29ac7d274ae0 548
Volcano_498 9:693f69e0a175 549 if(temp > tempThres[tempSetting].thresTemp){ //if temperature exceeds set threshold
Volcano_498 9:693f69e0a175 550 redLED = 0.95; //apply a PWM signal and light up the red LED
Volcano_498 9:693f69e0a175 551 buzzerPwm = 0.5; //apply a PWM signal to buzzer with 50% duty ratio to imitate a square wave.
Volcano_498 9:693f69e0a175 552 buzzerPwm.period_us(500); //period of 500 us or 0.5 ms equals a frequency of 2000 Hz.
Volcano_498 9:693f69e0a175 553 greenLED = 0.0; //if green LED has been set, reset it.
Volcano_498 9:693f69e0a175 554 }
Volcano_498 9:693f69e0a175 555 else{ //if not, light up green LED to signify that the device is operating normally.
Volcano_498 9:693f69e0a175 556 greenLED = 1.0;
Volcano_498 9:693f69e0a175 557 buzzerPwm = 0.0; //if PWM signal is set and threshold exceeded once, stop the buzzer until threshold is reached again.
Volcano_498 9:693f69e0a175 558 redLED = 0.0; //if red LED has been lit up previously, turn it off.
Volcano_498 9:693f69e0a175 559 }
Volcano_498 9:693f69e0a175 560 } //terminate dispTimerFlag
Volcano_498 6:0aca5c17c988 561
Volcano_498 6:0aca5c17c988 562 if(menuButtonFlag == 1){ //if menu button has been pressed once
Volcano_498 6:0aca5c17c988 563
Volcano_498 6:0aca5c17c988 564 lcd.clear(); //clear the lcd display
Volcano_498 6:0aca5c17c988 565 displayMenuOne(); //display the menu strings function
Volcano_498 7:104ac8e707e6 566 subMenuId = 0; //initially set subMenuId to zero in case it is not
Volcano_498 6:0aca5c17c988 567 while(buttonOneAltFlag){ //if Button One is pressed AND the menu button is;
Volcano_498 7:104ac8e707e6 568 if(subMenuId == 0){ //if initially the UI wasn't in a sub-menu
Volcano_498 7:104ac8e707e6 569 lcd.clear(); //clear lcd
Volcano_498 7:104ac8e707e6 570 }
Volcano_498 6:0aca5c17c988 571 displayTempUnit(); //display unit change menu
Volcano_498 7:104ac8e707e6 572 lcd.refresh();
Volcano_498 7:104ac8e707e6 573 //this helps avoid vertical swipe blurs and re-overwrites on the display while in sub-menu.
Volcano_498 6:0aca5c17c988 574 subMenuId = 1; //set sub-menu number to avoid confusions for the processor as it might change other settings!
Volcano_498 6:0aca5c17c988 575
Volcano_498 6:0aca5c17c988 576 if(buttonTwoAltFlag && subMenuId == 1){ //if added to the above conditions button 2 is pressed
Volcano_498 6:0aca5c17c988 577 tempUnitSetting--; //decrease the unit setting
Volcano_498 6:0aca5c17c988 578 buttonTwoAltFlag = 0; //reset flag to avoid repeated unit changes on loop
Volcano_498 6:0aca5c17c988 579 if(tempUnitSetting < 0) //if it goes below 0
Volcano_498 6:0aca5c17c988 580 tempUnitSetting = 3; //go back to the highest value
Volcano_498 6:0aca5c17c988 581 }
Volcano_498 6:0aca5c17c988 582 if(buttonThreeAltFlag && subMenuId == 1){ //if otherwise button 3 has been pressed
Volcano_498 6:0aca5c17c988 583 tempUnitSetting++; //increase temp setting
Volcano_498 6:0aca5c17c988 584 buttonThreeAltFlag = 0;
Volcano_498 6:0aca5c17c988 585 if(tempUnitSetting > 3) //if the upper limit has been exceeded (3)
Volcano_498 6:0aca5c17c988 586 tempUnitSetting = 0; //reset it to zero
Volcano_498 6:0aca5c17c988 587 }
Volcano_498 6:0aca5c17c988 588 } //close button one alt
Volcano_498 6:0aca5c17c988 589 buttonOneAltFlag = 0;
Volcano_498 7:104ac8e707e6 590 lcd.clear();
Volcano_498 7:104ac8e707e6 591 displayMenuOne(); //display the menu strings function
Volcano_498 7:104ac8e707e6 592 subMenuId = 0; //reset sub-menu ID when loop has finished executing so it can be applied to other sub-menus.
Volcano_498 6:0aca5c17c988 593
Volcano_498 6:0aca5c17c988 594 while(buttonTwoAltFlag){ //if Button Two flag is set AND the menu button is;
Volcano_498 7:104ac8e707e6 595 if(subMenuId == 0){
Volcano_498 7:104ac8e707e6 596 lcd.clear(); //clear lcd
Volcano_498 7:104ac8e707e6 597 }
Volcano_498 6:0aca5c17c988 598 displayPressUnit(); //display unit change menu
Volcano_498 7:104ac8e707e6 599 lcd.refresh();
Volcano_498 6:0aca5c17c988 600 subMenuId = 2;
Volcano_498 6:0aca5c17c988 601
Volcano_498 6:0aca5c17c988 602 if(buttonOneAltFlag && subMenuId == 2){ //if added to the above conditions button 1 is pressed
Volcano_498 6:0aca5c17c988 603 pressUnitSetting--; //decrease the unit setting
Volcano_498 6:0aca5c17c988 604 buttonOneAltFlag = 0; //reset flag to avoid repeated unit changes on loop
Volcano_498 6:0aca5c17c988 605 if(pressUnitSetting < 0) //if it goes below 0
Volcano_498 6:0aca5c17c988 606 pressUnitSetting = 2; //go back to the highest value
Volcano_498 6:0aca5c17c988 607 }
Volcano_498 6:0aca5c17c988 608 if(buttonThreeAltFlag && subMenuId == 2){ //if otherwise button 3 has been pressed
Volcano_498 6:0aca5c17c988 609 pressUnitSetting++; //increase pressure setting
Volcano_498 6:0aca5c17c988 610 buttonThreeAltFlag = 0;
Volcano_498 6:0aca5c17c988 611 if(pressUnitSetting > 2) //if the upper limit has been exceeded (2)
Volcano_498 6:0aca5c17c988 612 pressUnitSetting = 0; //reset it to zero
Volcano_498 6:0aca5c17c988 613 }
Volcano_498 6:0aca5c17c988 614 } //close button two alt
Volcano_498 6:0aca5c17c988 615 buttonTwoAltFlag = 0;
Volcano_498 7:104ac8e707e6 616 lcd.clear();
Volcano_498 7:104ac8e707e6 617 displayMenuOne(); //display the menu strings function
Volcano_498 7:104ac8e707e6 618 subMenuId = 0;
Volcano_498 6:0aca5c17c988 619 } //close menu button flag
Volcano_498 6:0aca5c17c988 620
Volcano_498 7:104ac8e707e6 621 subMenuId = 0; //reset sub-menu ID after going through first menu in case it is not already
Volcano_498 7:104ac8e707e6 622 if(menuButtonFlag == 2){ //if menu button has been pressed twice
Volcano_498 6:0aca5c17c988 623
Volcano_498 6:0aca5c17c988 624 lcd.clear(); //clear the lcd display
Volcano_498 6:0aca5c17c988 625 displayMenuTwo(); //display the menu strings function
Volcano_498 6:0aca5c17c988 626 while(buttonOneAltFlag){ //if Button One is pressed AND the menu button is;
Volcano_498 7:104ac8e707e6 627 if(subMenuId == 0){
Volcano_498 7:104ac8e707e6 628 lcd.clear(); //clear lcd
Volcano_498 7:104ac8e707e6 629 }
Volcano_498 6:0aca5c17c988 630 displayDispMenu(); //display unit change menu
Volcano_498 7:104ac8e707e6 631 lcd.refresh();
Volcano_498 6:0aca5c17c988 632 subMenuId = 4;
Volcano_498 7:104ac8e707e6 633 lcd.refresh();
Volcano_498 6:0aca5c17c988 634 if(buttonTwoAltFlag && subMenuId == 4){ //if added to the above conditions button 2 is pressed
Volcano_498 6:0aca5c17c988 635 dispSetting--; //decrease setting
Volcano_498 6:0aca5c17c988 636 buttonTwoAltFlag = 0; //reset flag to avoid repeated unit changes on loop
Volcano_498 6:0aca5c17c988 637 if(dispSetting < 0) //if it goes below 0
Volcano_498 6:0aca5c17c988 638 dispSetting = 5; //go back to the highest value
Volcano_498 6:0aca5c17c988 639 }
Volcano_498 6:0aca5c17c988 640 if(buttonThreeAltFlag && subMenuId == 4){ //if otherwise button 3 has been pressed
Volcano_498 6:0aca5c17c988 641 dispSetting++; //increase setting
Volcano_498 6:0aca5c17c988 642 buttonThreeAltFlag = 0;
Volcano_498 6:0aca5c17c988 643 if(dispSetting > 5) //if the upper limit has been exceeded (3)
Volcano_498 6:0aca5c17c988 644 dispSetting = 0; //reset it to zero
Volcano_498 6:0aca5c17c988 645 }
Volcano_498 6:0aca5c17c988 646 } //close button one alt
Volcano_498 6:0aca5c17c988 647 buttonOneAltFlag = 0;
Volcano_498 7:104ac8e707e6 648 lcd.clear(); //clear the lcd display
Volcano_498 7:104ac8e707e6 649 displayMenuTwo(); //display the menu strings function
Volcano_498 7:104ac8e707e6 650 subMenuId = 0;
Volcano_498 6:0aca5c17c988 651
Volcano_498 6:0aca5c17c988 652 while(buttonTwoAltFlag){ //if Button Two flag is set AND the menu button is;
Volcano_498 7:104ac8e707e6 653 if(subMenuId == 0){
Volcano_498 7:104ac8e707e6 654 lcd.clear(); //clear lcd
Volcano_498 7:104ac8e707e6 655 }
Volcano_498 6:0aca5c17c988 656 displayThresholdTemp(); //display unit change menu
Volcano_498 7:104ac8e707e6 657 lcd.refresh();
Volcano_498 6:0aca5c17c988 658 subMenuId = 5;
Volcano_498 6:0aca5c17c988 659 if(buttonOneAltFlag && subMenuId == 5){ //if added to the above conditions button 1 is pressed
Volcano_498 6:0aca5c17c988 660 tempSetting--; //decrease setting
Volcano_498 6:0aca5c17c988 661 buttonOneAltFlag = 0; //reset flag to avoid repeated unit changes on loop
Volcano_498 6:0aca5c17c988 662 if(tempSetting < 0) //if it goes below 0
Volcano_498 6:0aca5c17c988 663 tempSetting = 4; //go back to the highest value
Volcano_498 6:0aca5c17c988 664 }
Volcano_498 6:0aca5c17c988 665 if(buttonThreeAltFlag && subMenuId == 5){ //if otherwise button 3 has been pressed
Volcano_498 6:0aca5c17c988 666 tempSetting++; //increase setting
Volcano_498 6:0aca5c17c988 667 buttonThreeAltFlag = 0;
Volcano_498 6:0aca5c17c988 668 if(tempSetting > 4) //if the upper limit has been exceeded (2)
Volcano_498 6:0aca5c17c988 669 tempSetting = 0; //reset it to zero
Volcano_498 6:0aca5c17c988 670 }
Volcano_498 6:0aca5c17c988 671 } //close button two alt
Volcano_498 6:0aca5c17c988 672 buttonTwoAltFlag = 0;
Volcano_498 7:104ac8e707e6 673 lcd.clear(); //clear the lcd display
Volcano_498 7:104ac8e707e6 674 displayMenuTwo(); //display the menu strings function
Volcano_498 7:104ac8e707e6 675 subMenuId = 0;
Volcano_498 6:0aca5c17c988 676
Volcano_498 6:0aca5c17c988 677 while(buttonThreeAltFlag){ //if Button Three flag is set AND the menu button is;
Volcano_498 7:104ac8e707e6 678 if(subMenuId == 0){
Volcano_498 7:104ac8e707e6 679 lcd.clear(); //clear lcd
Volcano_498 7:104ac8e707e6 680 }
Volcano_498 6:0aca5c17c988 681 displayAltitudeUnit(); //display unit change menu
Volcano_498 7:104ac8e707e6 682 lcd.refresh();
Volcano_498 6:0aca5c17c988 683 subMenuId = 6;
Volcano_498 6:0aca5c17c988 684 if(buttonOneAltFlag && subMenuId == 6){ //if added to the above conditions button 1 is pressed
Volcano_498 6:0aca5c17c988 685 altUnitSetting--; //decrease the unit setting
Volcano_498 6:0aca5c17c988 686 buttonOneAltFlag = 0; //reset flag to avoid repeated unit changes on loop
Volcano_498 6:0aca5c17c988 687 if(altUnitSetting < 0) //if it goes below 0
Volcano_498 6:0aca5c17c988 688 altUnitSetting = 2; //go back to the highest value
Volcano_498 6:0aca5c17c988 689 }
Volcano_498 6:0aca5c17c988 690 if(buttonTwoAltFlag && subMenuId == 6){ //if otherwise button 2 has been pressed
Volcano_498 6:0aca5c17c988 691 altUnitSetting++; //increase pressure setting
Volcano_498 6:0aca5c17c988 692 buttonTwoAltFlag = 0;
Volcano_498 6:0aca5c17c988 693 if(altUnitSetting > 2) //if the upper limit has been exceeded (2)
Volcano_498 6:0aca5c17c988 694 altUnitSetting = 0; //reset it to zero
Volcano_498 6:0aca5c17c988 695 }
Volcano_498 6:0aca5c17c988 696 } //close button three alt
Volcano_498 6:0aca5c17c988 697 buttonThreeAltFlag = 0;
Volcano_498 7:104ac8e707e6 698 lcd.clear(); //clear the lcd display
Volcano_498 7:104ac8e707e6 699 displayMenuTwo(); //display the menu strings function
Volcano_498 7:104ac8e707e6 700 subMenuId = 0;
Volcano_498 6:0aca5c17c988 701 } //close menu button flag
Volcano_498 6:0aca5c17c988 702
Volcano_498 5:6d85cafa1085 703
Volcano_498 5:6d85cafa1085 704
Volcano_498 5:6d85cafa1085 705 Sleep(); //put the mbed to sleep once the program flow has been completed.
Volcano_498 5:6d85cafa1085 706
Volcano_498 3:70e14f1577f7 707
Volcano_498 3:70e14f1577f7 708 } //close if
Volcano_498 3:70e14f1577f7 709 } //close while
Volcano_498 1:454dddb8adc2 710 } //terminate main()
Volcano_498 2:08f2469728d5 711
Volcano_498 5:6d85cafa1085 712 /**
Volcano_498 5:6d85cafa1085 713 Fetches readings from the sensor via the main() function and calculates altitude from pressure data.
Volcano_498 5:6d85cafa1085 714 Then prints them on the screen.
Volcano_498 5:6d85cafa1085 715 @param temp - temperature reading from the BMP180(˚C).
Volcano_498 5:6d85cafa1085 716 @param press - pressure reading from the BMP180(mb).
Volcano_498 5:6d85cafa1085 717 @param altitude - altitude calculated from the pressure reading (m).
Volcano_498 5:6d85cafa1085 718 */
Volcano_498 5:6d85cafa1085 719
Volcano_498 8:29ac7d274ae0 720 float prevPress;
Volcano_498 8:29ac7d274ae0 721
Volcano_498 3:70e14f1577f7 722 void printReadings()
Volcano_498 3:70e14f1577f7 723 {
Volcano_498 2:08f2469728d5 724 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
Volcano_498 3:70e14f1577f7 725 // so can display a string of a maximum 14 characters in length
Volcano_498 3:70e14f1577f7 726 // or create formatted strings - ensure they aren't more than 14 characters long
Volcano_498 7:104ac8e707e6 727
Volcano_498 7:104ac8e707e6 728 if(tempUnitSetting == 0){ //if Celsius has been selected as the temperature unit
Volcano_498 7:104ac8e707e6 729 int length = sprintf(buffer,"T = %.2f 'C",temp); // print formatted data to buffer
Volcano_498 7:104ac8e707e6 730 // it is important the format specifier ensures the length will fit in the buffer
Volcano_498 7:104ac8e707e6 731 if (length <= 14) { // if string will fit on display
Volcano_498 7:104ac8e707e6 732 lcd.printString(buffer,0,1); // display on screen. Column 0, row 1.
Volcano_498 7:104ac8e707e6 733 }
Volcano_498 7:104ac8e707e6 734 } //close unit setting 0
Volcano_498 7:104ac8e707e6 735 else if(tempUnitSetting == 1){ //Fahrenheit
Volcano_498 7:104ac8e707e6 736 float tempTemp = (temp*1.8) + 32;
Volcano_498 7:104ac8e707e6 737 int length = sprintf(buffer,"T = %.2f F",tempTemp); // print formatted data to buffer
Volcano_498 7:104ac8e707e6 738 // it is important the format specifier ensures the length will fit in the buffer
Volcano_498 7:104ac8e707e6 739 if (length <= 14) { // if string will fit on display
Volcano_498 7:104ac8e707e6 740 lcd.printString(buffer,0,1); // display on screen. Column 0, row 1.
Volcano_498 7:104ac8e707e6 741 }
Volcano_498 7:104ac8e707e6 742 } //close unit setting 1
Volcano_498 7:104ac8e707e6 743 else if(tempUnitSetting == 2){ //Kelvin
Volcano_498 7:104ac8e707e6 744 float tempTemp = temp + 273.15;
Volcano_498 7:104ac8e707e6 745 int length = sprintf(buffer,"T = %.1f K",tempTemp); // print formatted data to buffer
Volcano_498 7:104ac8e707e6 746 // it is important the format specifier ensures the length will fit in the buffer
Volcano_498 7:104ac8e707e6 747 if (length <= 14) { // if string will fit on display
Volcano_498 7:104ac8e707e6 748 lcd.printString(buffer,0,1); // display on screen. Column 0, row 1.
Volcano_498 7:104ac8e707e6 749 }
Volcano_498 7:104ac8e707e6 750 } //close unit setting 2
Volcano_498 7:104ac8e707e6 751
Volcano_498 7:104ac8e707e6 752 else if(tempUnitSetting == 3){ //Rankine
Volcano_498 7:104ac8e707e6 753 float tempTemp = (temp + 273.15)*1.8;
Volcano_498 7:104ac8e707e6 754 int length = sprintf(buffer,"T = %.1f 'R",tempTemp); // print formatted data to buffer
Volcano_498 3:70e14f1577f7 755 // it is important the format specifier ensures the length will fit in the buffer
Volcano_498 7:104ac8e707e6 756 if (length <= 14) { // if string will fit on display
Volcano_498 7:104ac8e707e6 757 lcd.printString(buffer,0,1); // display on screen. Column 0, row 1.
Volcano_498 7:104ac8e707e6 758 }
Volcano_498 7:104ac8e707e6 759 } //close unit setting 3
Volcano_498 7:104ac8e707e6 760
Volcano_498 7:104ac8e707e6 761 if(pressUnitSetting == 0){ //if pressure is to be displayed in mb
Volcano_498 7:104ac8e707e6 762 int length = sprintf(buffer,"P = %.2f mb",press); //use single letters to represent parameters or string may not fit in the banks!
Volcano_498 7:104ac8e707e6 763 if (length <= 14) {
Volcano_498 7:104ac8e707e6 764 lcd.printString(buffer,0,2); // Column 0, row 2.
Volcano_498 7:104ac8e707e6 765 }
Volcano_498 7:104ac8e707e6 766 } //close unit setting 0
Volcano_498 7:104ac8e707e6 767
Volcano_498 7:104ac8e707e6 768 else if(pressUnitSetting == 1){ //Pa
Volcano_498 7:104ac8e707e6 769 float tempPress = press*100; //convert from mb to Pa
Volcano_498 7:104ac8e707e6 770 int length = sprintf(buffer,"P = %.0f Pa",tempPress); //use single letters to represent parameters or string may not fit in the banks!
Volcano_498 7:104ac8e707e6 771 if (length <= 14) {
Volcano_498 7:104ac8e707e6 772 lcd.printString(buffer,0,2); // Column 0, row 2.
Volcano_498 7:104ac8e707e6 773 }
Volcano_498 7:104ac8e707e6 774 } //close unit setting 1
Volcano_498 7:104ac8e707e6 775 else if(pressUnitSetting == 2){ //atm
Volcano_498 7:104ac8e707e6 776 float tempPress = press/1013.25; //an atm is 1013.25 mb; therefore dividing press by that gives pressure in atm.
Volcano_498 7:104ac8e707e6 777 int length = sprintf(buffer,"P = %.1f atm",tempPress); //use single letters to represent parameters or string may not fit in the banks!
Volcano_498 7:104ac8e707e6 778 if (length <= 14) {
Volcano_498 7:104ac8e707e6 779 lcd.printString(buffer,0,2); // Column 0, row 2.
Volcano_498 7:104ac8e707e6 780 }
Volcano_498 7:104ac8e707e6 781 } //close unit setting 2
Volcano_498 7:104ac8e707e6 782
Volcano_498 7:104ac8e707e6 783 //Now for the altitude display settings. Bear in mind that a metre is 3.2808399 feet; or 1.0936133 yards. Three feet equals a yard.
Volcano_498 7:104ac8e707e6 784 if(altUnitSetting == 0){ //if metres have been selected
Volcano_498 7:104ac8e707e6 785 int length = sprintf(buffer,"A = %.1f m",altitude);
Volcano_498 7:104ac8e707e6 786 if (length <= 14) {
Volcano_498 7:104ac8e707e6 787 lcd.printString(buffer,0,3); //Column 0, row 3.
Volcano_498 7:104ac8e707e6 788 }
Volcano_498 7:104ac8e707e6 789 } // close unit setting 0
Volcano_498 7:104ac8e707e6 790 else if(altUnitSetting == 1){ //feet
Volcano_498 7:104ac8e707e6 791 float tempAlt = altitude*3.2808399; //convert to feet
Volcano_498 7:104ac8e707e6 792 int length = sprintf(buffer,"A = %.1f ft",tempAlt);
Volcano_498 7:104ac8e707e6 793 if (length <= 14) {
Volcano_498 7:104ac8e707e6 794 lcd.printString(buffer,0,3); //Column 0, row 3.
Volcano_498 7:104ac8e707e6 795 }
Volcano_498 7:104ac8e707e6 796 } //close unit setting 1
Volcano_498 7:104ac8e707e6 797 else if(altUnitSetting == 2){ //yards
Volcano_498 7:104ac8e707e6 798 float tempAlt = altitude*1.0936133; //convert to yards
Volcano_498 7:104ac8e707e6 799 int length = sprintf(buffer,"A = %.1f ft",tempAlt);
Volcano_498 7:104ac8e707e6 800 if (length <= 14) {
Volcano_498 7:104ac8e707e6 801 lcd.printString(buffer,0,3); //Column 0, row 3.
Volcano_498 7:104ac8e707e6 802 }
Volcano_498 7:104ac8e707e6 803 } //close unit setting 2
Volcano_498 7:104ac8e707e6 804
Volcano_498 8:29ac7d274ae0 805 if (press > prevPress) {
Volcano_498 3:70e14f1577f7 806 lcd.printString("Hotter Weather",2,4); //column 2, row 4.
Volcano_498 3:70e14f1577f7 807 lcd.printString("On the Way!",2,5); //column 2 , row 5.
Volcano_498 7:104ac8e707e6 808 }
Volcano_498 8:29ac7d274ae0 809 else if (press < prevPress) {
Volcano_498 8:29ac7d274ae0 810 lcd.printString("Temp's predic-",0,4); //column 2, row 4.
Volcano_498 8:29ac7d274ae0 811 lcd.printString("ted to fall!",0,5); //column 2 , row 5.
Volcano_498 3:70e14f1577f7 812 }
Volcano_498 8:29ac7d274ae0 813 else{
Volcano_498 8:29ac7d274ae0 814
Volcano_498 8:29ac7d274ae0 815 }
Volcano_498 8:29ac7d274ae0 816 prevPress = press; //set the "previous pressure" to be the pressure read, so that it can be used the next time the function runs.
Volcano_498 3:70e14f1577f7 817 }
Volcano_498 3:70e14f1577f7 818
Volcano_498 3:70e14f1577f7 819 void clearCells()
Volcano_498 3:70e14f1577f7 820 {
Volcano_498 3:70e14f1577f7 821 //loop through cells, and clear them
Volcano_498 3:70e14f1577f7 822 for (int i = 0; i < 83 ; i++) {
Volcano_498 3:70e14f1577f7 823 for (int j = 0; j < 47; j++) {
Volcano_498 3:70e14f1577f7 824 lcd.clearPixel(i,j); //function to "kill" all cells after looping through.
Volcano_498 2:08f2469728d5 825 }
Volcano_498 3:70e14f1577f7 826 }
Volcano_498 3:70e14f1577f7 827 lcd.refresh(); //must refresh to write buffer to display
Volcano_498 5:6d85cafa1085 828 }