Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BMP180 N5110 PowerControl mbed
main.cpp@7:104ac8e707e6, 2015-04-19 (annotated)
- Committer:
- Volcano_498
- Date:
- Sun Apr 19 17:53:39 2015 +0000
- Revision:
- 7:104ac8e707e6
- Parent:
- 6:0aca5c17c988
- Child:
- 8:29ac7d274ae0
UI improved and blurs eliminated.
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:08f2469728d5 | 454 | //1 bar is 100000 Pa. An atm is 101325 Pa. Therefore an mb is 100 Pa. |
Volcano_498 | 2:08f2469728d5 | 455 | |
Volcano_498 | 3:70e14f1577f7 | 456 | int main() |
Volcano_498 | 3:70e14f1577f7 | 457 | { |
Volcano_498 | 3:70e14f1577f7 | 458 | |
Volcano_498 | 1:454dddb8adc2 | 459 | splashFlip.attach(&splashDelay,3.0); //attach timer and wait for ISR to be called |
Volcano_498 | 3:70e14f1577f7 | 460 | |
Volcano_498 | 6:0aca5c17c988 | 461 | displayTimer.attach(&timerExpiDisplay,dispInterval[dispSetting].time); //do the same for display dispInterval[dispSetting].time |
Volcano_498 | 6:0aca5c17c988 | 462 | |
Volcano_498 | 6:0aca5c17c988 | 463 | //logTimer.attach(&timerExpiLog,recInterval[recSetting].time); |
Volcano_498 | 6:0aca5c17c988 | 464 | |
Volcano_498 | 6:0aca5c17c988 | 465 | menuButton.rise(&menuButtonPressed); //event generated on rising edge (a positive spike in voltage), indicated by .rise |
Volcano_498 | 5:6d85cafa1085 | 466 | |
Volcano_498 | 6:0aca5c17c988 | 467 | buttonOne.rise(&buttonOnePressed); |
Volcano_498 | 6:0aca5c17c988 | 468 | |
Volcano_498 | 6:0aca5c17c988 | 469 | buttonTwo.rise(&buttonTwoPressed); |
Volcano_498 | 6:0aca5c17c988 | 470 | |
Volcano_498 | 6:0aca5c17c988 | 471 | buttonThree.rise(&buttonThreePressed); |
Volcano_498 | 5:6d85cafa1085 | 472 | |
Volcano_498 | 1:454dddb8adc2 | 473 | // first need to initialise display |
Volcano_498 | 1:454dddb8adc2 | 474 | lcd.init(); |
Volcano_498 | 3:70e14f1577f7 | 475 | |
Volcano_498 | 1:454dddb8adc2 | 476 | displayInitSplash(); |
Volcano_498 | 3:70e14f1577f7 | 477 | |
Volcano_498 | 0:b30f86a9a1c5 | 478 | //initialise barometer |
Volcano_498 | 0:b30f86a9a1c5 | 479 | bmp180.init(); |
Volcano_498 | 3:70e14f1577f7 | 480 | |
Volcano_498 | 1:454dddb8adc2 | 481 | PHY_PowerDown(); //powers down the Ethernet feature. |
Volcano_498 | 1:454dddb8adc2 | 482 | |
Volcano_498 | 1:454dddb8adc2 | 483 | int result = semihost_powerdown(); //as a result, power down the USB connection |
Volcano_498 | 1:454dddb8adc2 | 484 | //unless the cable is connected. |
Volcano_498 | 3:70e14f1577f7 | 485 | |
Volcano_498 | 1:454dddb8adc2 | 486 | Measurement measurement; // object created for pressure & temperature using the structure declared in BMP180 class |
Volcano_498 | 3:70e14f1577f7 | 487 | |
Volcano_498 | 3:70e14f1577f7 | 488 | while(1) { |
Volcano_498 | 3:70e14f1577f7 | 489 | |
Volcano_498 | 3:70e14f1577f7 | 490 | if(splashFlag == 0) { //if ISR has been called proceed with program flow |
Volcano_498 | 3:70e14f1577f7 | 491 | splashFlip.detach(); //detach Timeout object |
Volcano_498 | 5:6d85cafa1085 | 492 | lcd.clear(); |
Volcano_498 | 3:70e14f1577f7 | 493 | clearCells(); |
Volcano_498 | 6:0aca5c17c988 | 494 | lcd.refresh(); //need to refresh to write buffer on lcd |
Volcano_498 | 3:70e14f1577f7 | 495 | |
Volcano_498 | 5:6d85cafa1085 | 496 | if(dispTimerFlag) { |
Volcano_498 | 5:6d85cafa1085 | 497 | //read values (T in degrees Celsius and P in mb). |
Volcano_498 | 5:6d85cafa1085 | 498 | measurement = bmp180.readValues(); |
Volcano_498 | 5:6d85cafa1085 | 499 | temp = measurement.temperature; |
Volcano_498 | 5:6d85cafa1085 | 500 | press = measurement.pressure; |
Volcano_498 | 5:6d85cafa1085 | 501 | /*formula for calculating altitude from sea level by using atmospheric pressure. Unit in metres. |
Volcano_498 | 5:6d85cafa1085 | 502 | Use pow(double a,double b) for indices, not the ^ sign. Just a reminder! Also check out this site: |
Volcano_498 | 5:6d85cafa1085 | 503 | http://www.mide.com/products/slamstick/air-pressure-altitude-calculator.php |
Volcano_498 | 5:6d85cafa1085 | 504 | */ |
Volcano_498 | 5:6d85cafa1085 | 505 | altitude = 44330.0*(1.0-(pow((press/PNought),(1.0/5.255)))); |
Volcano_498 | 6:0aca5c17c988 | 506 | dispTimerFlag = 0; |
Volcano_498 | 6:0aca5c17c988 | 507 | |
Volcano_498 | 5:6d85cafa1085 | 508 | printReadings(); |
Volcano_498 | 5:6d85cafa1085 | 509 | lcd.refresh(); |
Volcano_498 | 6:0aca5c17c988 | 510 | } |
Volcano_498 | 6:0aca5c17c988 | 511 | |
Volcano_498 | 6:0aca5c17c988 | 512 | if(menuButtonFlag == 1){ //if menu button has been pressed once |
Volcano_498 | 6:0aca5c17c988 | 513 | |
Volcano_498 | 6:0aca5c17c988 | 514 | lcd.clear(); //clear the lcd display |
Volcano_498 | 6:0aca5c17c988 | 515 | displayMenuOne(); //display the menu strings function |
Volcano_498 | 7:104ac8e707e6 | 516 | subMenuId = 0; //initially set subMenuId to zero in case it is not |
Volcano_498 | 6:0aca5c17c988 | 517 | while(buttonOneAltFlag){ //if Button One is pressed AND the menu button is; |
Volcano_498 | 7:104ac8e707e6 | 518 | if(subMenuId == 0){ //if initially the UI wasn't in a sub-menu |
Volcano_498 | 7:104ac8e707e6 | 519 | lcd.clear(); //clear lcd |
Volcano_498 | 7:104ac8e707e6 | 520 | } |
Volcano_498 | 6:0aca5c17c988 | 521 | displayTempUnit(); //display unit change menu |
Volcano_498 | 7:104ac8e707e6 | 522 | lcd.refresh(); |
Volcano_498 | 7:104ac8e707e6 | 523 | //this helps avoid vertical swipe blurs and re-overwrites on the display while in sub-menu. |
Volcano_498 | 6:0aca5c17c988 | 524 | subMenuId = 1; //set sub-menu number to avoid confusions for the processor as it might change other settings! |
Volcano_498 | 6:0aca5c17c988 | 525 | |
Volcano_498 | 6:0aca5c17c988 | 526 | if(buttonTwoAltFlag && subMenuId == 1){ //if added to the above conditions button 2 is pressed |
Volcano_498 | 6:0aca5c17c988 | 527 | tempUnitSetting--; //decrease the unit setting |
Volcano_498 | 6:0aca5c17c988 | 528 | buttonTwoAltFlag = 0; //reset flag to avoid repeated unit changes on loop |
Volcano_498 | 6:0aca5c17c988 | 529 | if(tempUnitSetting < 0) //if it goes below 0 |
Volcano_498 | 6:0aca5c17c988 | 530 | tempUnitSetting = 3; //go back to the highest value |
Volcano_498 | 6:0aca5c17c988 | 531 | } |
Volcano_498 | 6:0aca5c17c988 | 532 | if(buttonThreeAltFlag && subMenuId == 1){ //if otherwise button 3 has been pressed |
Volcano_498 | 6:0aca5c17c988 | 533 | tempUnitSetting++; //increase temp setting |
Volcano_498 | 6:0aca5c17c988 | 534 | buttonThreeAltFlag = 0; |
Volcano_498 | 6:0aca5c17c988 | 535 | if(tempUnitSetting > 3) //if the upper limit has been exceeded (3) |
Volcano_498 | 6:0aca5c17c988 | 536 | tempUnitSetting = 0; //reset it to zero |
Volcano_498 | 6:0aca5c17c988 | 537 | } |
Volcano_498 | 6:0aca5c17c988 | 538 | } //close button one alt |
Volcano_498 | 6:0aca5c17c988 | 539 | buttonOneAltFlag = 0; |
Volcano_498 | 7:104ac8e707e6 | 540 | lcd.clear(); |
Volcano_498 | 7:104ac8e707e6 | 541 | displayMenuOne(); //display the menu strings function |
Volcano_498 | 7:104ac8e707e6 | 542 | subMenuId = 0; //reset sub-menu ID when loop has finished executing so it can be applied to other sub-menus. |
Volcano_498 | 6:0aca5c17c988 | 543 | |
Volcano_498 | 6:0aca5c17c988 | 544 | while(buttonTwoAltFlag){ //if Button Two flag is set AND the menu button is; |
Volcano_498 | 7:104ac8e707e6 | 545 | if(subMenuId == 0){ |
Volcano_498 | 7:104ac8e707e6 | 546 | lcd.clear(); //clear lcd |
Volcano_498 | 7:104ac8e707e6 | 547 | } |
Volcano_498 | 6:0aca5c17c988 | 548 | displayPressUnit(); //display unit change menu |
Volcano_498 | 7:104ac8e707e6 | 549 | lcd.refresh(); |
Volcano_498 | 6:0aca5c17c988 | 550 | subMenuId = 2; |
Volcano_498 | 6:0aca5c17c988 | 551 | |
Volcano_498 | 6:0aca5c17c988 | 552 | if(buttonOneAltFlag && subMenuId == 2){ //if added to the above conditions button 1 is pressed |
Volcano_498 | 6:0aca5c17c988 | 553 | pressUnitSetting--; //decrease the unit setting |
Volcano_498 | 6:0aca5c17c988 | 554 | buttonOneAltFlag = 0; //reset flag to avoid repeated unit changes on loop |
Volcano_498 | 6:0aca5c17c988 | 555 | if(pressUnitSetting < 0) //if it goes below 0 |
Volcano_498 | 6:0aca5c17c988 | 556 | pressUnitSetting = 2; //go back to the highest value |
Volcano_498 | 6:0aca5c17c988 | 557 | } |
Volcano_498 | 6:0aca5c17c988 | 558 | if(buttonThreeAltFlag && subMenuId == 2){ //if otherwise button 3 has been pressed |
Volcano_498 | 6:0aca5c17c988 | 559 | pressUnitSetting++; //increase pressure setting |
Volcano_498 | 6:0aca5c17c988 | 560 | buttonThreeAltFlag = 0; |
Volcano_498 | 6:0aca5c17c988 | 561 | if(pressUnitSetting > 2) //if the upper limit has been exceeded (2) |
Volcano_498 | 6:0aca5c17c988 | 562 | pressUnitSetting = 0; //reset it to zero |
Volcano_498 | 6:0aca5c17c988 | 563 | } |
Volcano_498 | 6:0aca5c17c988 | 564 | } //close button two alt |
Volcano_498 | 6:0aca5c17c988 | 565 | buttonTwoAltFlag = 0; |
Volcano_498 | 7:104ac8e707e6 | 566 | lcd.clear(); |
Volcano_498 | 7:104ac8e707e6 | 567 | displayMenuOne(); //display the menu strings function |
Volcano_498 | 7:104ac8e707e6 | 568 | subMenuId = 0; |
Volcano_498 | 6:0aca5c17c988 | 569 | } //close menu button flag |
Volcano_498 | 6:0aca5c17c988 | 570 | |
Volcano_498 | 7:104ac8e707e6 | 571 | subMenuId = 0; //reset sub-menu ID after going through first menu in case it is not already |
Volcano_498 | 7:104ac8e707e6 | 572 | if(menuButtonFlag == 2){ //if menu button has been pressed twice |
Volcano_498 | 6:0aca5c17c988 | 573 | |
Volcano_498 | 6:0aca5c17c988 | 574 | lcd.clear(); //clear the lcd display |
Volcano_498 | 6:0aca5c17c988 | 575 | displayMenuTwo(); //display the menu strings function |
Volcano_498 | 6:0aca5c17c988 | 576 | while(buttonOneAltFlag){ //if Button One is pressed AND the menu button is; |
Volcano_498 | 7:104ac8e707e6 | 577 | if(subMenuId == 0){ |
Volcano_498 | 7:104ac8e707e6 | 578 | lcd.clear(); //clear lcd |
Volcano_498 | 7:104ac8e707e6 | 579 | } |
Volcano_498 | 6:0aca5c17c988 | 580 | displayDispMenu(); //display unit change menu |
Volcano_498 | 7:104ac8e707e6 | 581 | lcd.refresh(); |
Volcano_498 | 6:0aca5c17c988 | 582 | subMenuId = 4; |
Volcano_498 | 7:104ac8e707e6 | 583 | lcd.refresh(); |
Volcano_498 | 6:0aca5c17c988 | 584 | if(buttonTwoAltFlag && subMenuId == 4){ //if added to the above conditions button 2 is pressed |
Volcano_498 | 6:0aca5c17c988 | 585 | dispSetting--; //decrease setting |
Volcano_498 | 6:0aca5c17c988 | 586 | buttonTwoAltFlag = 0; //reset flag to avoid repeated unit changes on loop |
Volcano_498 | 6:0aca5c17c988 | 587 | if(dispSetting < 0) //if it goes below 0 |
Volcano_498 | 6:0aca5c17c988 | 588 | dispSetting = 5; //go back to the highest value |
Volcano_498 | 6:0aca5c17c988 | 589 | } |
Volcano_498 | 6:0aca5c17c988 | 590 | if(buttonThreeAltFlag && subMenuId == 4){ //if otherwise button 3 has been pressed |
Volcano_498 | 6:0aca5c17c988 | 591 | dispSetting++; //increase setting |
Volcano_498 | 6:0aca5c17c988 | 592 | buttonThreeAltFlag = 0; |
Volcano_498 | 6:0aca5c17c988 | 593 | if(dispSetting > 5) //if the upper limit has been exceeded (3) |
Volcano_498 | 6:0aca5c17c988 | 594 | dispSetting = 0; //reset it to zero |
Volcano_498 | 6:0aca5c17c988 | 595 | } |
Volcano_498 | 6:0aca5c17c988 | 596 | } //close button one alt |
Volcano_498 | 6:0aca5c17c988 | 597 | buttonOneAltFlag = 0; |
Volcano_498 | 7:104ac8e707e6 | 598 | lcd.clear(); //clear the lcd display |
Volcano_498 | 7:104ac8e707e6 | 599 | displayMenuTwo(); //display the menu strings function |
Volcano_498 | 7:104ac8e707e6 | 600 | subMenuId = 0; |
Volcano_498 | 6:0aca5c17c988 | 601 | |
Volcano_498 | 6:0aca5c17c988 | 602 | while(buttonTwoAltFlag){ //if Button Two flag is set AND the menu button is; |
Volcano_498 | 7:104ac8e707e6 | 603 | if(subMenuId == 0){ |
Volcano_498 | 7:104ac8e707e6 | 604 | lcd.clear(); //clear lcd |
Volcano_498 | 7:104ac8e707e6 | 605 | } |
Volcano_498 | 6:0aca5c17c988 | 606 | displayThresholdTemp(); //display unit change menu |
Volcano_498 | 7:104ac8e707e6 | 607 | lcd.refresh(); |
Volcano_498 | 6:0aca5c17c988 | 608 | subMenuId = 5; |
Volcano_498 | 6:0aca5c17c988 | 609 | if(buttonOneAltFlag && subMenuId == 5){ //if added to the above conditions button 1 is pressed |
Volcano_498 | 6:0aca5c17c988 | 610 | tempSetting--; //decrease setting |
Volcano_498 | 6:0aca5c17c988 | 611 | buttonOneAltFlag = 0; //reset flag to avoid repeated unit changes on loop |
Volcano_498 | 6:0aca5c17c988 | 612 | if(tempSetting < 0) //if it goes below 0 |
Volcano_498 | 6:0aca5c17c988 | 613 | tempSetting = 4; //go back to the highest value |
Volcano_498 | 6:0aca5c17c988 | 614 | } |
Volcano_498 | 6:0aca5c17c988 | 615 | if(buttonThreeAltFlag && subMenuId == 5){ //if otherwise button 3 has been pressed |
Volcano_498 | 6:0aca5c17c988 | 616 | tempSetting++; //increase setting |
Volcano_498 | 6:0aca5c17c988 | 617 | buttonThreeAltFlag = 0; |
Volcano_498 | 6:0aca5c17c988 | 618 | if(tempSetting > 4) //if the upper limit has been exceeded (2) |
Volcano_498 | 6:0aca5c17c988 | 619 | tempSetting = 0; //reset it to zero |
Volcano_498 | 6:0aca5c17c988 | 620 | } |
Volcano_498 | 6:0aca5c17c988 | 621 | } //close button two alt |
Volcano_498 | 6:0aca5c17c988 | 622 | buttonTwoAltFlag = 0; |
Volcano_498 | 7:104ac8e707e6 | 623 | lcd.clear(); //clear the lcd display |
Volcano_498 | 7:104ac8e707e6 | 624 | displayMenuTwo(); //display the menu strings function |
Volcano_498 | 7:104ac8e707e6 | 625 | subMenuId = 0; |
Volcano_498 | 6:0aca5c17c988 | 626 | |
Volcano_498 | 6:0aca5c17c988 | 627 | while(buttonThreeAltFlag){ //if Button Three flag is set AND the menu button is; |
Volcano_498 | 7:104ac8e707e6 | 628 | if(subMenuId == 0){ |
Volcano_498 | 7:104ac8e707e6 | 629 | lcd.clear(); //clear lcd |
Volcano_498 | 7:104ac8e707e6 | 630 | } |
Volcano_498 | 6:0aca5c17c988 | 631 | displayAltitudeUnit(); //display unit change menu |
Volcano_498 | 7:104ac8e707e6 | 632 | lcd.refresh(); |
Volcano_498 | 6:0aca5c17c988 | 633 | subMenuId = 6; |
Volcano_498 | 6:0aca5c17c988 | 634 | if(buttonOneAltFlag && subMenuId == 6){ //if added to the above conditions button 1 is pressed |
Volcano_498 | 6:0aca5c17c988 | 635 | altUnitSetting--; //decrease the unit setting |
Volcano_498 | 6:0aca5c17c988 | 636 | buttonOneAltFlag = 0; //reset flag to avoid repeated unit changes on loop |
Volcano_498 | 6:0aca5c17c988 | 637 | if(altUnitSetting < 0) //if it goes below 0 |
Volcano_498 | 6:0aca5c17c988 | 638 | altUnitSetting = 2; //go back to the highest value |
Volcano_498 | 6:0aca5c17c988 | 639 | } |
Volcano_498 | 6:0aca5c17c988 | 640 | if(buttonTwoAltFlag && subMenuId == 6){ //if otherwise button 2 has been pressed |
Volcano_498 | 6:0aca5c17c988 | 641 | altUnitSetting++; //increase pressure setting |
Volcano_498 | 6:0aca5c17c988 | 642 | buttonTwoAltFlag = 0; |
Volcano_498 | 6:0aca5c17c988 | 643 | if(altUnitSetting > 2) //if the upper limit has been exceeded (2) |
Volcano_498 | 6:0aca5c17c988 | 644 | altUnitSetting = 0; //reset it to zero |
Volcano_498 | 6:0aca5c17c988 | 645 | } |
Volcano_498 | 6:0aca5c17c988 | 646 | } //close button three alt |
Volcano_498 | 6:0aca5c17c988 | 647 | buttonThreeAltFlag = 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 | } //close menu button flag |
Volcano_498 | 6:0aca5c17c988 | 652 | |
Volcano_498 | 5:6d85cafa1085 | 653 | |
Volcano_498 | 5:6d85cafa1085 | 654 | |
Volcano_498 | 5:6d85cafa1085 | 655 | Sleep(); //put the mbed to sleep once the program flow has been completed. |
Volcano_498 | 5:6d85cafa1085 | 656 | |
Volcano_498 | 3:70e14f1577f7 | 657 | |
Volcano_498 | 3:70e14f1577f7 | 658 | } //close if |
Volcano_498 | 3:70e14f1577f7 | 659 | } //close while |
Volcano_498 | 1:454dddb8adc2 | 660 | } //terminate main() |
Volcano_498 | 2:08f2469728d5 | 661 | |
Volcano_498 | 5:6d85cafa1085 | 662 | /** |
Volcano_498 | 5:6d85cafa1085 | 663 | Fetches readings from the sensor via the main() function and calculates altitude from pressure data. |
Volcano_498 | 5:6d85cafa1085 | 664 | Then prints them on the screen. |
Volcano_498 | 5:6d85cafa1085 | 665 | @param temp - temperature reading from the BMP180(˚C). |
Volcano_498 | 5:6d85cafa1085 | 666 | @param press - pressure reading from the BMP180(mb). |
Volcano_498 | 5:6d85cafa1085 | 667 | @param altitude - altitude calculated from the pressure reading (m). |
Volcano_498 | 5:6d85cafa1085 | 668 | */ |
Volcano_498 | 5:6d85cafa1085 | 669 | |
Volcano_498 | 3:70e14f1577f7 | 670 | void printReadings() |
Volcano_498 | 3:70e14f1577f7 | 671 | { |
Volcano_498 | 2:08f2469728d5 | 672 | char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
Volcano_498 | 3:70e14f1577f7 | 673 | // so can display a string of a maximum 14 characters in length |
Volcano_498 | 3:70e14f1577f7 | 674 | // or create formatted strings - ensure they aren't more than 14 characters long |
Volcano_498 | 7:104ac8e707e6 | 675 | |
Volcano_498 | 7:104ac8e707e6 | 676 | if(tempUnitSetting == 0){ //if Celsius has been selected as the temperature unit |
Volcano_498 | 7:104ac8e707e6 | 677 | int length = sprintf(buffer,"T = %.2f 'C",temp); // print formatted data to buffer |
Volcano_498 | 7:104ac8e707e6 | 678 | // it is important the format specifier ensures the length will fit in the buffer |
Volcano_498 | 7:104ac8e707e6 | 679 | if (length <= 14) { // if string will fit on display |
Volcano_498 | 7:104ac8e707e6 | 680 | lcd.printString(buffer,0,1); // display on screen. Column 0, row 1. |
Volcano_498 | 7:104ac8e707e6 | 681 | } |
Volcano_498 | 7:104ac8e707e6 | 682 | } //close unit setting 0 |
Volcano_498 | 7:104ac8e707e6 | 683 | else if(tempUnitSetting == 1){ //Fahrenheit |
Volcano_498 | 7:104ac8e707e6 | 684 | float tempTemp = (temp*1.8) + 32; |
Volcano_498 | 7:104ac8e707e6 | 685 | int length = sprintf(buffer,"T = %.2f F",tempTemp); // print formatted data to buffer |
Volcano_498 | 7:104ac8e707e6 | 686 | // it is important the format specifier ensures the length will fit in the buffer |
Volcano_498 | 7:104ac8e707e6 | 687 | if (length <= 14) { // if string will fit on display |
Volcano_498 | 7:104ac8e707e6 | 688 | lcd.printString(buffer,0,1); // display on screen. Column 0, row 1. |
Volcano_498 | 7:104ac8e707e6 | 689 | } |
Volcano_498 | 7:104ac8e707e6 | 690 | } //close unit setting 1 |
Volcano_498 | 7:104ac8e707e6 | 691 | else if(tempUnitSetting == 2){ //Kelvin |
Volcano_498 | 7:104ac8e707e6 | 692 | float tempTemp = temp + 273.15; |
Volcano_498 | 7:104ac8e707e6 | 693 | int length = sprintf(buffer,"T = %.1f K",tempTemp); // print formatted data to buffer |
Volcano_498 | 7:104ac8e707e6 | 694 | // it is important the format specifier ensures the length will fit in the buffer |
Volcano_498 | 7:104ac8e707e6 | 695 | if (length <= 14) { // if string will fit on display |
Volcano_498 | 7:104ac8e707e6 | 696 | lcd.printString(buffer,0,1); // display on screen. Column 0, row 1. |
Volcano_498 | 7:104ac8e707e6 | 697 | } |
Volcano_498 | 7:104ac8e707e6 | 698 | } //close unit setting 2 |
Volcano_498 | 7:104ac8e707e6 | 699 | |
Volcano_498 | 7:104ac8e707e6 | 700 | else if(tempUnitSetting == 3){ //Rankine |
Volcano_498 | 7:104ac8e707e6 | 701 | float tempTemp = (temp + 273.15)*1.8; |
Volcano_498 | 7:104ac8e707e6 | 702 | int length = sprintf(buffer,"T = %.1f 'R",tempTemp); // print formatted data to buffer |
Volcano_498 | 3:70e14f1577f7 | 703 | // it is important the format specifier ensures the length will fit in the buffer |
Volcano_498 | 7:104ac8e707e6 | 704 | if (length <= 14) { // if string will fit on display |
Volcano_498 | 7:104ac8e707e6 | 705 | lcd.printString(buffer,0,1); // display on screen. Column 0, row 1. |
Volcano_498 | 7:104ac8e707e6 | 706 | } |
Volcano_498 | 7:104ac8e707e6 | 707 | } //close unit setting 3 |
Volcano_498 | 7:104ac8e707e6 | 708 | |
Volcano_498 | 7:104ac8e707e6 | 709 | if(pressUnitSetting == 0){ //if pressure is to be displayed in mb |
Volcano_498 | 7:104ac8e707e6 | 710 | 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 | 711 | if (length <= 14) { |
Volcano_498 | 7:104ac8e707e6 | 712 | lcd.printString(buffer,0,2); // Column 0, row 2. |
Volcano_498 | 7:104ac8e707e6 | 713 | } |
Volcano_498 | 7:104ac8e707e6 | 714 | } //close unit setting 0 |
Volcano_498 | 7:104ac8e707e6 | 715 | |
Volcano_498 | 7:104ac8e707e6 | 716 | else if(pressUnitSetting == 1){ //Pa |
Volcano_498 | 7:104ac8e707e6 | 717 | float tempPress = press*100; //convert from mb to Pa |
Volcano_498 | 7:104ac8e707e6 | 718 | 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 | 719 | if (length <= 14) { |
Volcano_498 | 7:104ac8e707e6 | 720 | lcd.printString(buffer,0,2); // Column 0, row 2. |
Volcano_498 | 7:104ac8e707e6 | 721 | } |
Volcano_498 | 7:104ac8e707e6 | 722 | } //close unit setting 1 |
Volcano_498 | 7:104ac8e707e6 | 723 | else if(pressUnitSetting == 2){ //atm |
Volcano_498 | 7:104ac8e707e6 | 724 | float tempPress = press/1013.25; //an atm is 1013.25 mb; therefore dividing press by that gives pressure in atm. |
Volcano_498 | 7:104ac8e707e6 | 725 | 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 | 726 | if (length <= 14) { |
Volcano_498 | 7:104ac8e707e6 | 727 | lcd.printString(buffer,0,2); // Column 0, row 2. |
Volcano_498 | 7:104ac8e707e6 | 728 | } |
Volcano_498 | 7:104ac8e707e6 | 729 | } //close unit setting 2 |
Volcano_498 | 7:104ac8e707e6 | 730 | |
Volcano_498 | 7:104ac8e707e6 | 731 | //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 | 732 | if(altUnitSetting == 0){ //if metres have been selected |
Volcano_498 | 7:104ac8e707e6 | 733 | int length = sprintf(buffer,"A = %.1f m",altitude); |
Volcano_498 | 7:104ac8e707e6 | 734 | if (length <= 14) { |
Volcano_498 | 7:104ac8e707e6 | 735 | lcd.printString(buffer,0,3); //Column 0, row 3. |
Volcano_498 | 7:104ac8e707e6 | 736 | } |
Volcano_498 | 7:104ac8e707e6 | 737 | } // close unit setting 0 |
Volcano_498 | 7:104ac8e707e6 | 738 | else if(altUnitSetting == 1){ //feet |
Volcano_498 | 7:104ac8e707e6 | 739 | float tempAlt = altitude*3.2808399; //convert to feet |
Volcano_498 | 7:104ac8e707e6 | 740 | int length = sprintf(buffer,"A = %.1f ft",tempAlt); |
Volcano_498 | 7:104ac8e707e6 | 741 | if (length <= 14) { |
Volcano_498 | 7:104ac8e707e6 | 742 | lcd.printString(buffer,0,3); //Column 0, row 3. |
Volcano_498 | 7:104ac8e707e6 | 743 | } |
Volcano_498 | 7:104ac8e707e6 | 744 | } //close unit setting 1 |
Volcano_498 | 7:104ac8e707e6 | 745 | else if(altUnitSetting == 2){ //yards |
Volcano_498 | 7:104ac8e707e6 | 746 | float tempAlt = altitude*1.0936133; //convert to yards |
Volcano_498 | 7:104ac8e707e6 | 747 | int length = sprintf(buffer,"A = %.1f ft",tempAlt); |
Volcano_498 | 7:104ac8e707e6 | 748 | if (length <= 14) { |
Volcano_498 | 7:104ac8e707e6 | 749 | lcd.printString(buffer,0,3); //Column 0, row 3. |
Volcano_498 | 7:104ac8e707e6 | 750 | } |
Volcano_498 | 7:104ac8e707e6 | 751 | } //close unit setting 2 |
Volcano_498 | 7:104ac8e707e6 | 752 | |
Volcano_498 | 7:104ac8e707e6 | 753 | |
Volcano_498 | 3:70e14f1577f7 | 754 | if (press++) { |
Volcano_498 | 3:70e14f1577f7 | 755 | lcd.printString("Hotter Weather",2,4); //column 2, row 4. |
Volcano_498 | 3:70e14f1577f7 | 756 | lcd.printString("On the Way!",2,5); //column 2 , row 5. |
Volcano_498 | 7:104ac8e707e6 | 757 | } |
Volcano_498 | 7:104ac8e707e6 | 758 | else if (press--) { |
Volcano_498 | 3:70e14f1577f7 | 759 | lcd.printString("The temperature",2,4); //column 2, row 4. |
Volcano_498 | 3:70e14f1577f7 | 760 | lcd.printString("is predicted",2,5); //column 2 , row 5. |
Volcano_498 | 3:70e14f1577f7 | 761 | lcd.printString("to fall!",2,6); //column 2, row 4. |
Volcano_498 | 3:70e14f1577f7 | 762 | } |
Volcano_498 | 3:70e14f1577f7 | 763 | } |
Volcano_498 | 3:70e14f1577f7 | 764 | |
Volcano_498 | 3:70e14f1577f7 | 765 | void clearCells() |
Volcano_498 | 3:70e14f1577f7 | 766 | { |
Volcano_498 | 3:70e14f1577f7 | 767 | //loop through cells, and clear them |
Volcano_498 | 3:70e14f1577f7 | 768 | for (int i = 0; i < 83 ; i++) { |
Volcano_498 | 3:70e14f1577f7 | 769 | for (int j = 0; j < 47; j++) { |
Volcano_498 | 3:70e14f1577f7 | 770 | lcd.clearPixel(i,j); //function to "kill" all cells after looping through. |
Volcano_498 | 2:08f2469728d5 | 771 | } |
Volcano_498 | 3:70e14f1577f7 | 772 | } |
Volcano_498 | 3:70e14f1577f7 | 773 | lcd.refresh(); //must refresh to write buffer to display |
Volcano_498 | 5:6d85cafa1085 | 774 | } |