A Weather station designed to read the values of temperature and pressure from the sensor (BMP180), data displayed via a nokia N5110 display.
Dependencies: BMP180 N5110 PowerControl mbed
Barometer.cpp@23:a0ce9fa869c2, 2015-05-09 (annotated)
- Committer:
- amiraseman
- Date:
- Sat May 09 01:08:16 2015 +0000
- Revision:
- 23:a0ce9fa869c2
- Child:
- 24:51cbed3fbf5c
Final version. All the major parts are done ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
amiraseman | 23:a0ce9fa869c2 | 1 | /** |
amiraseman | 23:a0ce9fa869c2 | 2 | @file Barometer.cpp |
amiraseman | 23:a0ce9fa869c2 | 3 | |
amiraseman | 23:a0ce9fa869c2 | 4 | @brief Member functions implementations |
amiraseman | 23:a0ce9fa869c2 | 5 | |
amiraseman | 23:a0ce9fa869c2 | 6 | */ |
amiraseman | 23:a0ce9fa869c2 | 7 | #include "mbed.h" |
amiraseman | 23:a0ce9fa869c2 | 8 | #include "Barometer.h" |
amiraseman | 23:a0ce9fa869c2 | 9 | |
amiraseman | 23:a0ce9fa869c2 | 10 | |
amiraseman | 23:a0ce9fa869c2 | 11 | /// |
amiraseman | 23:a0ce9fa869c2 | 12 | void button1Pressed() //ISR to subtract 1 from the value of choice when the first button is pressed |
amiraseman | 23:a0ce9fa869c2 | 13 | { |
amiraseman | 23:a0ce9fa869c2 | 14 | if (debounce.read_ms()>200) // only allow if debounce timer |
amiraseman | 23:a0ce9fa869c2 | 15 | button1Flag=1; // has passed 200 ms |
amiraseman | 23:a0ce9fa869c2 | 16 | debounce.reset(); // restart timer when the toggle is performed |
amiraseman | 23:a0ce9fa869c2 | 17 | } |
amiraseman | 23:a0ce9fa869c2 | 18 | |
amiraseman | 23:a0ce9fa869c2 | 19 | |
amiraseman | 23:a0ce9fa869c2 | 20 | /// |
amiraseman | 23:a0ce9fa869c2 | 21 | void button2Pressed() //ISR to add 1 to the value of the choice when the second button is pressed |
amiraseman | 23:a0ce9fa869c2 | 22 | { |
amiraseman | 23:a0ce9fa869c2 | 23 | if (debounce.read_ms()>200) // only allow if debounce timer |
amiraseman | 23:a0ce9fa869c2 | 24 | button2Flag=1; // has passed 200 ms |
amiraseman | 23:a0ce9fa869c2 | 25 | debounce.reset(); // restart timer when the toggle is performed |
amiraseman | 23:a0ce9fa869c2 | 26 | } |
amiraseman | 23:a0ce9fa869c2 | 27 | |
amiraseman | 23:a0ce9fa869c2 | 28 | |
amiraseman | 23:a0ce9fa869c2 | 29 | /// |
amiraseman | 23:a0ce9fa869c2 | 30 | void button3Pressed() |
amiraseman | 23:a0ce9fa869c2 | 31 | { |
amiraseman | 23:a0ce9fa869c2 | 32 | if (debounce.read_ms()>200) // only allow if debounce timer |
amiraseman | 23:a0ce9fa869c2 | 33 | button3Flag=1; // has passed 200 ms |
amiraseman | 23:a0ce9fa869c2 | 34 | debounce.reset(); // restart timer when the toggle is performed |
amiraseman | 23:a0ce9fa869c2 | 35 | } |
amiraseman | 23:a0ce9fa869c2 | 36 | |
amiraseman | 23:a0ce9fa869c2 | 37 | |
amiraseman | 23:a0ce9fa869c2 | 38 | /// |
amiraseman | 23:a0ce9fa869c2 | 39 | void button4Pressed() |
amiraseman | 23:a0ce9fa869c2 | 40 | { |
amiraseman | 23:a0ce9fa869c2 | 41 | if (debounce.read_ms()>200) // only allow if debounce timer |
amiraseman | 23:a0ce9fa869c2 | 42 | button4Flag=1; // has passed 200 ms |
amiraseman | 23:a0ce9fa869c2 | 43 | debounce.reset(); // restart timer when the toggle is performed |
amiraseman | 23:a0ce9fa869c2 | 44 | } |
amiraseman | 23:a0ce9fa869c2 | 45 | |
amiraseman | 23:a0ce9fa869c2 | 46 | |
amiraseman | 23:a0ce9fa869c2 | 47 | |
amiraseman | 23:a0ce9fa869c2 | 48 | /// ISR called by the timer to set the timer flag to 1 (flag is used to update the values of pressure and temperature every second for the live data function) |
amiraseman | 23:a0ce9fa869c2 | 49 | void timerExpired() |
amiraseman | 23:a0ce9fa869c2 | 50 | { |
amiraseman | 23:a0ce9fa869c2 | 51 | yellowLED = 0; |
amiraseman | 23:a0ce9fa869c2 | 52 | timerFlag=1; |
amiraseman | 23:a0ce9fa869c2 | 53 | |
amiraseman | 23:a0ce9fa869c2 | 54 | } |
amiraseman | 23:a0ce9fa869c2 | 55 | |
amiraseman | 23:a0ce9fa869c2 | 56 | |
amiraseman | 23:a0ce9fa869c2 | 57 | ///An ISR called by data logger timer to set the dataLoggerFlag to 1 |
amiraseman | 23:a0ce9fa869c2 | 58 | void dataLoggerTimerExpired () |
amiraseman | 23:a0ce9fa869c2 | 59 | { |
amiraseman | 23:a0ce9fa869c2 | 60 | yellowLED = 0; |
amiraseman | 23:a0ce9fa869c2 | 61 | dataLoggerFlag = 1 ; |
amiraseman | 23:a0ce9fa869c2 | 62 | |
amiraseman | 23:a0ce9fa869c2 | 63 | } |
amiraseman | 23:a0ce9fa869c2 | 64 | |
amiraseman | 23:a0ce9fa869c2 | 65 | |
amiraseman | 23:a0ce9fa869c2 | 66 | /// open a local file and print the recieved char arrays in the stream |
amiraseman | 23:a0ce9fa869c2 | 67 | void saveToFile(char *data,char *data1,char *data2,char *data3) |
amiraseman | 23:a0ce9fa869c2 | 68 | { |
amiraseman | 23:a0ce9fa869c2 | 69 | FILE* pFile = fopen("/local/textfile.csv","w"); // open file access |
amiraseman | 23:a0ce9fa869c2 | 70 | fprintf (pFile, "%s\n%s\n%s\n%s", data,data1,data2,data3); |
amiraseman | 23:a0ce9fa869c2 | 71 | fclose (pFile); |
amiraseman | 23:a0ce9fa869c2 | 72 | } |
amiraseman | 23:a0ce9fa869c2 | 73 | |
amiraseman | 23:a0ce9fa869c2 | 74 | |
amiraseman | 23:a0ce9fa869c2 | 75 | /// Gets the current time and stores them in bufferTime and bufferDate |
amiraseman | 23:a0ce9fa869c2 | 76 | void updateTime() |
amiraseman | 23:a0ce9fa869c2 | 77 | { |
amiraseman | 23:a0ce9fa869c2 | 78 | time_t seconds = time(NULL); // get current time |
amiraseman | 23:a0ce9fa869c2 | 79 | // format time into a string (time and date) |
amiraseman | 23:a0ce9fa869c2 | 80 | currentTime = seconds ; // sets the current time to an integer for alarm clock check |
amiraseman | 23:a0ce9fa869c2 | 81 | //serial.printf("current time :%i \n",currentTime); |
amiraseman | 23:a0ce9fa869c2 | 82 | //serial.printf("Unix date :%i \n",UNIXdate); |
amiraseman | 23:a0ce9fa869c2 | 83 | strftime(bufferTime, 14 , "%H:%M", localtime(&seconds)); |
amiraseman | 23:a0ce9fa869c2 | 84 | strftime(bufferDate, 14 , "%d/%m/%y", localtime(&seconds)); |
amiraseman | 23:a0ce9fa869c2 | 85 | if (alarmClockFlag == 1 ) { // if the the alarm clock flag is set by the user |
amiraseman | 23:a0ce9fa869c2 | 86 | alarmClockCheck(); |
amiraseman | 23:a0ce9fa869c2 | 87 | } |
amiraseman | 23:a0ce9fa869c2 | 88 | } |
amiraseman | 23:a0ce9fa869c2 | 89 | |
amiraseman | 23:a0ce9fa869c2 | 90 | |
amiraseman | 23:a0ce9fa869c2 | 91 | /// Displays the screen buffers on the lcd |
amiraseman | 23:a0ce9fa869c2 | 92 | void display() |
amiraseman | 23:a0ce9fa869c2 | 93 | { |
amiraseman | 23:a0ce9fa869c2 | 94 | lcd.clear(); |
amiraseman | 23:a0ce9fa869c2 | 95 | lcd.printString(buffer0,0,0); |
amiraseman | 23:a0ce9fa869c2 | 96 | lcd.printString(buffer1,0,1); |
amiraseman | 23:a0ce9fa869c2 | 97 | lcd.printString(buffer2,0,2); |
amiraseman | 23:a0ce9fa869c2 | 98 | lcd.printString(buffer3,0,3); |
amiraseman | 23:a0ce9fa869c2 | 99 | lcd.printString(buffer4,0,4); |
amiraseman | 23:a0ce9fa869c2 | 100 | lcd.printString(buffer5,0,5); |
amiraseman | 23:a0ce9fa869c2 | 101 | wait(0.1); |
amiraseman | 23:a0ce9fa869c2 | 102 | |
amiraseman | 23:a0ce9fa869c2 | 103 | } |
amiraseman | 23:a0ce9fa869c2 | 104 | |
amiraseman | 23:a0ce9fa869c2 | 105 | |
amiraseman | 23:a0ce9fa869c2 | 106 | |
amiraseman | 23:a0ce9fa869c2 | 107 | ///Imports the data and saves them to the bufferss |
amiraseman | 23:a0ce9fa869c2 | 108 | void readData() |
amiraseman | 23:a0ce9fa869c2 | 109 | { |
amiraseman | 23:a0ce9fa869c2 | 110 | |
amiraseman | 23:a0ce9fa869c2 | 111 | Measurement measurement; // measurement structure declared in BMP180 class |
amiraseman | 23:a0ce9fa869c2 | 112 | |
amiraseman | 23:a0ce9fa869c2 | 113 | measurement = bmp180.readValues(); |
amiraseman | 23:a0ce9fa869c2 | 114 | // serial.printf("T = %.2f C P = %.2f mb\n",measurement.temperature,measurement.pressure); |
amiraseman | 23:a0ce9fa869c2 | 115 | |
amiraseman | 23:a0ce9fa869c2 | 116 | |
amiraseman | 23:a0ce9fa869c2 | 117 | if (unitFlag==1 || unitFlag==2 ) { //c |
amiraseman | 23:a0ce9fa869c2 | 118 | |
amiraseman | 23:a0ce9fa869c2 | 119 | temperature = measurement.temperature; |
amiraseman | 23:a0ce9fa869c2 | 120 | int length = sprintf(bufferT,"%0.1f",temperature); // print formatted data to buffer |
amiraseman | 23:a0ce9fa869c2 | 121 | // it is important the format specifier ensures the length will fit in the buffer |
amiraseman | 23:a0ce9fa869c2 | 122 | } |
amiraseman | 23:a0ce9fa869c2 | 123 | if (unitFlag==3 || unitFlag==4 ) {//f |
amiraseman | 23:a0ce9fa869c2 | 124 | temperature =( measurement.temperature*(9/5))+32; |
amiraseman | 23:a0ce9fa869c2 | 125 | int length = sprintf(bufferT,"%0.1f",temperature); // print formatted data to buffer |
amiraseman | 23:a0ce9fa869c2 | 126 | // it is important the format specifier ensures the length will fit in the buffer |
amiraseman | 23:a0ce9fa869c2 | 127 | } |
amiraseman | 23:a0ce9fa869c2 | 128 | |
amiraseman | 23:a0ce9fa869c2 | 129 | |
amiraseman | 23:a0ce9fa869c2 | 130 | |
amiraseman | 23:a0ce9fa869c2 | 131 | |
amiraseman | 23:a0ce9fa869c2 | 132 | if (unitFlag==1 || unitFlag==3 ) {//mb |
amiraseman | 23:a0ce9fa869c2 | 133 | pressure = measurement.pressure; // same idea with float |
amiraseman | 23:a0ce9fa869c2 | 134 | int length = sprintf(bufferP,"%.2f",pressure); // print formatted data to buffer |
amiraseman | 23:a0ce9fa869c2 | 135 | // it is important the format specifier ensures the length will fit in the buffer |
amiraseman | 23:a0ce9fa869c2 | 136 | } |
amiraseman | 23:a0ce9fa869c2 | 137 | if (unitFlag==2 || unitFlag==4 ) {//atm |
amiraseman | 23:a0ce9fa869c2 | 138 | pressure = measurement.pressure*0.0009869; // same idea with floats |
amiraseman | 23:a0ce9fa869c2 | 139 | int length = sprintf(bufferP,"%.2f",pressure); // print formatted data to buffer |
amiraseman | 23:a0ce9fa869c2 | 140 | // it is important the format specifier ensures the length will fit in the buffer |
amiraseman | 23:a0ce9fa869c2 | 141 | } |
amiraseman | 23:a0ce9fa869c2 | 142 | |
amiraseman | 23:a0ce9fa869c2 | 143 | if (thresholdAlarmFlag == 1) { // if the flag is set by the user to notify the thresholds |
amiraseman | 23:a0ce9fa869c2 | 144 | serial.printf("thresholds flag checked = 1\n"); |
amiraseman | 23:a0ce9fa869c2 | 145 | serial.printf("real values: t %i p %f \n",temperature,pressure); |
amiraseman | 23:a0ce9fa869c2 | 146 | serial.printf("thresholds: mint%i maxt%i minP%i maxP %i \n",fsmH[0].output,fsmH[1].output,fsmH[2].output,fsmH[3].output); |
amiraseman | 23:a0ce9fa869c2 | 147 | thresholdCheck(); |
amiraseman | 23:a0ce9fa869c2 | 148 | } |
amiraseman | 23:a0ce9fa869c2 | 149 | |
amiraseman | 23:a0ce9fa869c2 | 150 | } |
amiraseman | 23:a0ce9fa869c2 | 151 | |
amiraseman | 23:a0ce9fa869c2 | 152 | |
amiraseman | 23:a0ce9fa869c2 | 153 | |
amiraseman | 23:a0ce9fa869c2 | 154 | |
amiraseman | 23:a0ce9fa869c2 | 155 | /// Called by the powerSaverTimeout to turn off the lcd and put the mbed in sleep mode |
amiraseman | 23:a0ce9fa869c2 | 156 | void powerSaverExpired() |
amiraseman | 23:a0ce9fa869c2 | 157 | { |
amiraseman | 23:a0ce9fa869c2 | 158 | powerSaverFlag = 1; |
amiraseman | 23:a0ce9fa869c2 | 159 | } |
amiraseman | 23:a0ce9fa869c2 | 160 | |
amiraseman | 23:a0ce9fa869c2 | 161 | |
amiraseman | 23:a0ce9fa869c2 | 162 | |
amiraseman | 23:a0ce9fa869c2 | 163 | |
amiraseman | 23:a0ce9fa869c2 | 164 | |
amiraseman | 23:a0ce9fa869c2 | 165 | |
amiraseman | 23:a0ce9fa869c2 | 166 | /// Checks if the power saver option is set by the user, if yes attach a timeout to call save power; if its not set detach the timeout |
amiraseman | 23:a0ce9fa869c2 | 167 | void powerSaverCheck() // checks if the powersaverFlag is set |
amiraseman | 23:a0ce9fa869c2 | 168 | { |
amiraseman | 23:a0ce9fa869c2 | 169 | if (powerSaverFlag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 170 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 171 | thresholdCheck(); |
amiraseman | 23:a0ce9fa869c2 | 172 | alarmClockCheck(); |
amiraseman | 23:a0ce9fa869c2 | 173 | updateTime(); |
amiraseman | 23:a0ce9fa869c2 | 174 | loggerData(); |
amiraseman | 23:a0ce9fa869c2 | 175 | |
amiraseman | 23:a0ce9fa869c2 | 176 | if (button1Flag || button2Flag || button3Flag || button4Flag) { // if any of the buttons are pressed |
amiraseman | 23:a0ce9fa869c2 | 177 | powerSaverTimeout.attach(&powerSaverExpired, powerSaverTime); // setup a timeiut to call the savePower function |
amiraseman | 23:a0ce9fa869c2 | 178 | powerSaverFlag = 0; |
amiraseman | 23:a0ce9fa869c2 | 179 | lcd.init(); |
amiraseman | 23:a0ce9fa869c2 | 180 | button1Flag = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 181 | button2Flag = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 182 | button3Flag = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 183 | button4Flag = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 184 | break; |
amiraseman | 23:a0ce9fa869c2 | 185 | } |
amiraseman | 23:a0ce9fa869c2 | 186 | lcd.turnOff(); |
amiraseman | 23:a0ce9fa869c2 | 187 | Sleep(); |
amiraseman | 23:a0ce9fa869c2 | 188 | } |
amiraseman | 23:a0ce9fa869c2 | 189 | } |
amiraseman | 23:a0ce9fa869c2 | 190 | } |
amiraseman | 23:a0ce9fa869c2 | 191 | |
amiraseman | 23:a0ce9fa869c2 | 192 | |
amiraseman | 23:a0ce9fa869c2 | 193 | |
amiraseman | 23:a0ce9fa869c2 | 194 | /// Displays the live values of temperature and pressure, refreshes every 1 second |
amiraseman | 23:a0ce9fa869c2 | 195 | void liveData() |
amiraseman | 23:a0ce9fa869c2 | 196 | { |
amiraseman | 23:a0ce9fa869c2 | 197 | timerFlag=1; |
amiraseman | 23:a0ce9fa869c2 | 198 | timer.attach(&timerExpired,1.0); |
amiraseman | 23:a0ce9fa869c2 | 199 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 200 | thresholdCheck(); |
amiraseman | 23:a0ce9fa869c2 | 201 | alarmClockCheck(); |
amiraseman | 23:a0ce9fa869c2 | 202 | powerSaverCheck(); |
amiraseman | 23:a0ce9fa869c2 | 203 | updateTime(); |
amiraseman | 23:a0ce9fa869c2 | 204 | loggerData(); |
amiraseman | 23:a0ce9fa869c2 | 205 | if (timerFlag) { |
amiraseman | 23:a0ce9fa869c2 | 206 | |
amiraseman | 23:a0ce9fa869c2 | 207 | yellowLED = 1; |
amiraseman | 23:a0ce9fa869c2 | 208 | timerFlag=0; |
amiraseman | 23:a0ce9fa869c2 | 209 | readData(); |
amiraseman | 23:a0ce9fa869c2 | 210 | strncpy(buffer1, bufferTime, 14); |
amiraseman | 23:a0ce9fa869c2 | 211 | strncpy(buffer2, bufferDate, 14); |
amiraseman | 23:a0ce9fa869c2 | 212 | strncpy(buffer3, bufferT, 14); |
amiraseman | 23:a0ce9fa869c2 | 213 | strncpy(buffer4, bufferP, 14); |
amiraseman | 23:a0ce9fa869c2 | 214 | int dispaly = sprintf (buffer5, "");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 215 | int dispaly1 = sprintf (buffer0, "");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 216 | display(); |
amiraseman | 23:a0ce9fa869c2 | 217 | } |
amiraseman | 23:a0ce9fa869c2 | 218 | yellowLED = 0; |
amiraseman | 23:a0ce9fa869c2 | 219 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 220 | button3Flag = 0; |
amiraseman | 23:a0ce9fa869c2 | 221 | timer.detach(); |
amiraseman | 23:a0ce9fa869c2 | 222 | break; |
amiraseman | 23:a0ce9fa869c2 | 223 | } |
amiraseman | 23:a0ce9fa869c2 | 224 | } |
amiraseman | 23:a0ce9fa869c2 | 225 | } |
amiraseman | 23:a0ce9fa869c2 | 226 | |
amiraseman | 23:a0ce9fa869c2 | 227 | |
amiraseman | 23:a0ce9fa869c2 | 228 | |
amiraseman | 23:a0ce9fa869c2 | 229 | /// gets the temperature and pressure data and stores to suitable arrays, saves the arrays to the local file |
amiraseman | 23:a0ce9fa869c2 | 230 | void loggerData() |
amiraseman | 23:a0ce9fa869c2 | 231 | { |
amiraseman | 23:a0ce9fa869c2 | 232 | |
amiraseman | 23:a0ce9fa869c2 | 233 | |
amiraseman | 23:a0ce9fa869c2 | 234 | if(dataLoggerFlag ==1) { |
amiraseman | 23:a0ce9fa869c2 | 235 | int k ; |
amiraseman | 23:a0ce9fa869c2 | 236 | int sumTemperature = 0; |
amiraseman | 23:a0ce9fa869c2 | 237 | int sumPressure = 0; |
amiraseman | 23:a0ce9fa869c2 | 238 | greenLED = 1; |
amiraseman | 23:a0ce9fa869c2 | 239 | readData(); |
amiraseman | 23:a0ce9fa869c2 | 240 | |
amiraseman | 23:a0ce9fa869c2 | 241 | dataLoggerFlag=0; |
amiraseman | 23:a0ce9fa869c2 | 242 | //write the data to the arrays (arrays used to plot graphs) |
amiraseman | 23:a0ce9fa869c2 | 243 | //read the data from sensor |
amiraseman | 23:a0ce9fa869c2 | 244 | |
amiraseman | 23:a0ce9fa869c2 | 245 | |
amiraseman | 23:a0ce9fa869c2 | 246 | //reads the saved data back from the local file |
amiraseman | 23:a0ce9fa869c2 | 247 | |
amiraseman | 23:a0ce9fa869c2 | 248 | FILE* pFilea = fopen("/local/textfile.csv","r"); // open file access |
amiraseman | 23:a0ce9fa869c2 | 249 | |
amiraseman | 23:a0ce9fa869c2 | 250 | if (pFilea) {//checks if any data is available from the previous readings |
amiraseman | 23:a0ce9fa869c2 | 251 | fscanf (pFilea, "%s\n %s\n %s\n %s\n", temperatureRecieved,pressureRecieved,timeRecieved,dateRecieved); |
amiraseman | 23:a0ce9fa869c2 | 252 | fclose (pFilea); |
amiraseman | 23:a0ce9fa869c2 | 253 | |
amiraseman | 23:a0ce9fa869c2 | 254 | |
amiraseman | 23:a0ce9fa869c2 | 255 | //initialises the arrays with the saved data from the past |
amiraseman | 23:a0ce9fa869c2 | 256 | strcpy (temperatureSent,temperatureRecieved); |
amiraseman | 23:a0ce9fa869c2 | 257 | strcpy (pressureSent,pressureRecieved); |
amiraseman | 23:a0ce9fa869c2 | 258 | strcpy (timeSent,timeRecieved); |
amiraseman | 23:a0ce9fa869c2 | 259 | strcpy (dateSent,dateRecieved); |
amiraseman | 23:a0ce9fa869c2 | 260 | } |
amiraseman | 23:a0ce9fa869c2 | 261 | |
amiraseman | 23:a0ce9fa869c2 | 262 | |
amiraseman | 23:a0ce9fa869c2 | 263 | //inserts the int values to char arrays to save them to the local file |
amiraseman | 23:a0ce9fa869c2 | 264 | strcat(temperatureSent,bufferT); |
amiraseman | 23:a0ce9fa869c2 | 265 | strcat(temperatureSent,","); |
amiraseman | 23:a0ce9fa869c2 | 266 | strcat(pressureSent,bufferP); |
amiraseman | 23:a0ce9fa869c2 | 267 | strcat(pressureSent,","); |
amiraseman | 23:a0ce9fa869c2 | 268 | |
amiraseman | 23:a0ce9fa869c2 | 269 | |
amiraseman | 23:a0ce9fa869c2 | 270 | |
amiraseman | 23:a0ce9fa869c2 | 271 | //apends the new time and current stamp to the array with , between them |
amiraseman | 23:a0ce9fa869c2 | 272 | strcat(timeSent,bufferTime); |
amiraseman | 23:a0ce9fa869c2 | 273 | strcat(timeSent,","); |
amiraseman | 23:a0ce9fa869c2 | 274 | strcat (dateSent,bufferDate); |
amiraseman | 23:a0ce9fa869c2 | 275 | strcat (dateSent,","); |
amiraseman | 23:a0ce9fa869c2 | 276 | |
amiraseman | 23:a0ce9fa869c2 | 277 | //send the arrays above to the local file |
amiraseman | 23:a0ce9fa869c2 | 278 | saveToFile(temperatureSent,pressureSent,timeSent,dateSent); |
amiraseman | 23:a0ce9fa869c2 | 279 | //serial.printf ("sent temp %s\nsent pressure %s\nsent time %s\nsent date %s\n",temperatureSent,pressureSent,timeSent,dateSent); |
amiraseman | 23:a0ce9fa869c2 | 280 | |
amiraseman | 23:a0ce9fa869c2 | 281 | //reads the saved data back from the local file |
amiraseman | 23:a0ce9fa869c2 | 282 | |
amiraseman | 23:a0ce9fa869c2 | 283 | FILE* pFileb = fopen("/local/textfile.csv","r"); // open file access |
amiraseman | 23:a0ce9fa869c2 | 284 | fscanf (pFileb, "%s\n %s\n %s\n %s\n", temperatureRecieved,pressureRecieved,timeRecieved,dateRecieved); |
amiraseman | 23:a0ce9fa869c2 | 285 | fclose (pFileb); |
amiraseman | 23:a0ce9fa869c2 | 286 | |
amiraseman | 23:a0ce9fa869c2 | 287 | //serial.printf ("rec temp %s\nrec pressure %s\nrec time %s\nrec date %s\n",temperatureRecieved,pressureRecieved,timeRecieved,dateRecieved); |
amiraseman | 23:a0ce9fa869c2 | 288 | |
amiraseman | 23:a0ce9fa869c2 | 289 | |
amiraseman | 23:a0ce9fa869c2 | 290 | //seperates the array into tokens(seperated after the delimiter , ) |
amiraseman | 23:a0ce9fa869c2 | 291 | pch = strtok (timeRecieved,",");//pch is the arguement of the for str |
amiraseman | 23:a0ce9fa869c2 | 292 | while (pch != NULL) { |
amiraseman | 23:a0ce9fa869c2 | 293 | //serial.printf (" splitted time = %s\n",pch); |
amiraseman | 23:a0ce9fa869c2 | 294 | pch = strtok (NULL, ","); |
amiraseman | 23:a0ce9fa869c2 | 295 | } |
amiraseman | 23:a0ce9fa869c2 | 296 | pch1 = strtok (dateRecieved,","); |
amiraseman | 23:a0ce9fa869c2 | 297 | while (pch1 != NULL) { |
amiraseman | 23:a0ce9fa869c2 | 298 | //serial.printf (" splitted date= %s\n",pch1); |
amiraseman | 23:a0ce9fa869c2 | 299 | pch1 = strtok (NULL, ","); |
amiraseman | 23:a0ce9fa869c2 | 300 | } |
amiraseman | 23:a0ce9fa869c2 | 301 | pch2 = strtok (temperatureRecieved,","); |
amiraseman | 23:a0ce9fa869c2 | 302 | int p=0; |
amiraseman | 23:a0ce9fa869c2 | 303 | while (pch2 != NULL) { |
amiraseman | 23:a0ce9fa869c2 | 304 | //serial.printf (" splitted temperature = %s\n",pch2); |
amiraseman | 23:a0ce9fa869c2 | 305 | arrayT[p]=atof(pch2); |
amiraseman | 23:a0ce9fa869c2 | 306 | pch2 = strtok (NULL, ","); |
amiraseman | 23:a0ce9fa869c2 | 307 | p++; |
amiraseman | 23:a0ce9fa869c2 | 308 | } |
amiraseman | 23:a0ce9fa869c2 | 309 | |
amiraseman | 23:a0ce9fa869c2 | 310 | p=0; |
amiraseman | 23:a0ce9fa869c2 | 311 | pch3 = strtok (pressureRecieved,","); |
amiraseman | 23:a0ce9fa869c2 | 312 | while (pch3 != NULL) { |
amiraseman | 23:a0ce9fa869c2 | 313 | // serial.printf (" splitted pressure= %s\n",pch3); |
amiraseman | 23:a0ce9fa869c2 | 314 | arrayP[p]=atof(pch3); |
amiraseman | 23:a0ce9fa869c2 | 315 | pch3 = strtok (NULL, ","); |
amiraseman | 23:a0ce9fa869c2 | 316 | p++; |
amiraseman | 23:a0ce9fa869c2 | 317 | } |
amiraseman | 23:a0ce9fa869c2 | 318 | |
amiraseman | 23:a0ce9fa869c2 | 319 | // calculate the average value of the arrays and save them to an integer |
amiraseman | 23:a0ce9fa869c2 | 320 | |
amiraseman | 23:a0ce9fa869c2 | 321 | |
amiraseman | 23:a0ce9fa869c2 | 322 | |
amiraseman | 23:a0ce9fa869c2 | 323 | minTemperature = arrayT[0]; // sets the min to the first value in the array |
amiraseman | 23:a0ce9fa869c2 | 324 | minPressure = arrayP[0]; |
amiraseman | 23:a0ce9fa869c2 | 325 | maxTemperature = arrayT[0]; |
amiraseman | 23:a0ce9fa869c2 | 326 | maxPressure = arrayP[0]; |
amiraseman | 23:a0ce9fa869c2 | 327 | |
amiraseman | 23:a0ce9fa869c2 | 328 | |
amiraseman | 23:a0ce9fa869c2 | 329 | for (k = 0 ; k<p ; k++) { // loops through the arrays |
amiraseman | 23:a0ce9fa869c2 | 330 | // printf("arrayT = %lf\n",arrayT[k]); |
amiraseman | 23:a0ce9fa869c2 | 331 | // printf("arrayP = %lf\n",arrayP[k]); |
amiraseman | 23:a0ce9fa869c2 | 332 | |
amiraseman | 23:a0ce9fa869c2 | 333 | |
amiraseman | 23:a0ce9fa869c2 | 334 | if (unitFlag==1 || unitFlag==2 ) { //c |
amiraseman | 23:a0ce9fa869c2 | 335 | arrayTG[k]=(arrayT[k]/60); |
amiraseman | 23:a0ce9fa869c2 | 336 | } |
amiraseman | 23:a0ce9fa869c2 | 337 | if (unitFlag==3 || unitFlag==4 ) {//f |
amiraseman | 23:a0ce9fa869c2 | 338 | arrayTG[k]=(arrayT[k]/140); |
amiraseman | 23:a0ce9fa869c2 | 339 | } |
amiraseman | 23:a0ce9fa869c2 | 340 | |
amiraseman | 23:a0ce9fa869c2 | 341 | |
amiraseman | 23:a0ce9fa869c2 | 342 | |
amiraseman | 23:a0ce9fa869c2 | 343 | |
amiraseman | 23:a0ce9fa869c2 | 344 | if (unitFlag==1 || unitFlag==3 ) {//mb |
amiraseman | 23:a0ce9fa869c2 | 345 | arrayPG[k]=(arrayP[k]/1500); |
amiraseman | 23:a0ce9fa869c2 | 346 | } |
amiraseman | 23:a0ce9fa869c2 | 347 | if (unitFlag==2 || unitFlag==4 ) {//atm |
amiraseman | 23:a0ce9fa869c2 | 348 | arrayPG[k]=(arrayP[k]/2); |
amiraseman | 23:a0ce9fa869c2 | 349 | } |
amiraseman | 23:a0ce9fa869c2 | 350 | |
amiraseman | 23:a0ce9fa869c2 | 351 | |
amiraseman | 23:a0ce9fa869c2 | 352 | |
amiraseman | 23:a0ce9fa869c2 | 353 | sumTemperature += arrayT[k]; // calculates the sum of the stored values |
amiraseman | 23:a0ce9fa869c2 | 354 | averageTemperature = sumTemperature/(k+1) ; //calculates the average value |
amiraseman | 23:a0ce9fa869c2 | 355 | |
amiraseman | 23:a0ce9fa869c2 | 356 | sumPressure += arrayP[k]; // calculates the sum of the stored values |
amiraseman | 23:a0ce9fa869c2 | 357 | averagePressure =(sumPressure/(k+1)) ; //calculates the average value |
amiraseman | 23:a0ce9fa869c2 | 358 | |
amiraseman | 23:a0ce9fa869c2 | 359 | if(arrayT[k]>maxTemperature) { // checks if any of the values in the array is bigger than the fist value |
amiraseman | 23:a0ce9fa869c2 | 360 | maxTemperature=arrayT[k]; // if any greater values, sets the max value to that |
amiraseman | 23:a0ce9fa869c2 | 361 | } |
amiraseman | 23:a0ce9fa869c2 | 362 | if(arrayT[k]<minTemperature) { // finds the smallest value of the array |
amiraseman | 23:a0ce9fa869c2 | 363 | minTemperature=arrayT[k]; |
amiraseman | 23:a0ce9fa869c2 | 364 | } |
amiraseman | 23:a0ce9fa869c2 | 365 | if(arrayP[k]>maxPressure) { // checks if any of the values in the array is bigger than the fist value |
amiraseman | 23:a0ce9fa869c2 | 366 | maxPressure=(arrayP[k]); // if any greater values, sets the max value to that |
amiraseman | 23:a0ce9fa869c2 | 367 | } |
amiraseman | 23:a0ce9fa869c2 | 368 | if(arrayP[k]<maxPressure) { |
amiraseman | 23:a0ce9fa869c2 | 369 | minPressure=(arrayP[k]); |
amiraseman | 23:a0ce9fa869c2 | 370 | } |
amiraseman | 23:a0ce9fa869c2 | 371 | |
amiraseman | 23:a0ce9fa869c2 | 372 | } |
amiraseman | 23:a0ce9fa869c2 | 373 | |
amiraseman | 23:a0ce9fa869c2 | 374 | |
amiraseman | 23:a0ce9fa869c2 | 375 | |
amiraseman | 23:a0ce9fa869c2 | 376 | if (p>83) { // if the number of reading taken is more than 84 clear the arrays and the local file |
amiraseman | 23:a0ce9fa869c2 | 377 | strcpy (temperatureSent,""); |
amiraseman | 23:a0ce9fa869c2 | 378 | strcpy (pressureSent,""); |
amiraseman | 23:a0ce9fa869c2 | 379 | strcpy (timeSent,""); |
amiraseman | 23:a0ce9fa869c2 | 380 | strcpy (dateSent,""); |
amiraseman | 23:a0ce9fa869c2 | 381 | } |
amiraseman | 23:a0ce9fa869c2 | 382 | } |
amiraseman | 23:a0ce9fa869c2 | 383 | greenLED = 0; |
amiraseman | 23:a0ce9fa869c2 | 384 | } |
amiraseman | 23:a0ce9fa869c2 | 385 | |
amiraseman | 23:a0ce9fa869c2 | 386 | |
amiraseman | 23:a0ce9fa869c2 | 387 | |
amiraseman | 23:a0ce9fa869c2 | 388 | |
amiraseman | 23:a0ce9fa869c2 | 389 | /// Show a summery of minimum maximum and average values for temperature and pressure as well as the temperature-time and pressure_time graphs |
amiraseman | 23:a0ce9fa869c2 | 390 | void dataLogger() |
amiraseman | 23:a0ce9fa869c2 | 391 | { |
amiraseman | 23:a0ce9fa869c2 | 392 | int swipe = 1 ; |
amiraseman | 23:a0ce9fa869c2 | 393 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 394 | thresholdCheck(); |
amiraseman | 23:a0ce9fa869c2 | 395 | alarmClockCheck(); |
amiraseman | 23:a0ce9fa869c2 | 396 | powerSaverCheck(); |
amiraseman | 23:a0ce9fa869c2 | 397 | updateTime(); |
amiraseman | 23:a0ce9fa869c2 | 398 | if (runLoggerFlag) { |
amiraseman | 23:a0ce9fa869c2 | 399 | |
amiraseman | 23:a0ce9fa869c2 | 400 | |
amiraseman | 23:a0ce9fa869c2 | 401 | loggerData(); |
amiraseman | 23:a0ce9fa869c2 | 402 | |
amiraseman | 23:a0ce9fa869c2 | 403 | |
amiraseman | 23:a0ce9fa869c2 | 404 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 405 | button4Flag = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 406 | swipe ++; |
amiraseman | 23:a0ce9fa869c2 | 407 | } |
amiraseman | 23:a0ce9fa869c2 | 408 | if (swipe > 4) { |
amiraseman | 23:a0ce9fa869c2 | 409 | swipe = 1; |
amiraseman | 23:a0ce9fa869c2 | 410 | } |
amiraseman | 23:a0ce9fa869c2 | 411 | |
amiraseman | 23:a0ce9fa869c2 | 412 | strncpy(buffer0, bufferTime, 14); |
amiraseman | 23:a0ce9fa869c2 | 413 | int display5 = sprintf (buffer5, " Back Next");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 414 | |
amiraseman | 23:a0ce9fa869c2 | 415 | switch (swipe) { |
amiraseman | 23:a0ce9fa869c2 | 416 | case 1: { |
amiraseman | 23:a0ce9fa869c2 | 417 | int display1 = sprintf (buffer1, "Temperature"); |
amiraseman | 23:a0ce9fa869c2 | 418 | int display2 = sprintf (buffer2, "Min = %0.2f", minTemperature); |
amiraseman | 23:a0ce9fa869c2 | 419 | int display3 = sprintf (buffer3, "Max = %0.2f", maxTemperature); |
amiraseman | 23:a0ce9fa869c2 | 420 | int display4 = sprintf (buffer4, "Avg = %0.2f", averageTemperature); |
amiraseman | 23:a0ce9fa869c2 | 421 | display(); |
amiraseman | 23:a0ce9fa869c2 | 422 | break; |
amiraseman | 23:a0ce9fa869c2 | 423 | } |
amiraseman | 23:a0ce9fa869c2 | 424 | case 2 : { |
amiraseman | 23:a0ce9fa869c2 | 425 | lcd.clear(); |
amiraseman | 23:a0ce9fa869c2 | 426 | lcd.printString(" Temperature",0,0); |
amiraseman | 23:a0ce9fa869c2 | 427 | lcd.plotArray(arrayTG); |
amiraseman | 23:a0ce9fa869c2 | 428 | break; |
amiraseman | 23:a0ce9fa869c2 | 429 | } |
amiraseman | 23:a0ce9fa869c2 | 430 | case 3 : { |
amiraseman | 23:a0ce9fa869c2 | 431 | int display11 = sprintf (buffer1, "Pressure"); |
amiraseman | 23:a0ce9fa869c2 | 432 | int display22 = sprintf (buffer2, "Min = %0.2f", minPressure); |
amiraseman | 23:a0ce9fa869c2 | 433 | int display33 = sprintf (buffer3, "Max = %0.2f", maxPressure); |
amiraseman | 23:a0ce9fa869c2 | 434 | int display44 = sprintf (buffer4, "Avg = %0.2f", averagePressure); |
amiraseman | 23:a0ce9fa869c2 | 435 | display(); |
amiraseman | 23:a0ce9fa869c2 | 436 | break; |
amiraseman | 23:a0ce9fa869c2 | 437 | } |
amiraseman | 23:a0ce9fa869c2 | 438 | case 4 : { |
amiraseman | 23:a0ce9fa869c2 | 439 | lcd.clear(); |
amiraseman | 23:a0ce9fa869c2 | 440 | lcd.printString(" Pressure",0,0); |
amiraseman | 23:a0ce9fa869c2 | 441 | lcd.plotArray(arrayPG); |
amiraseman | 23:a0ce9fa869c2 | 442 | break; |
amiraseman | 23:a0ce9fa869c2 | 443 | } |
amiraseman | 23:a0ce9fa869c2 | 444 | default : |
amiraseman | 23:a0ce9fa869c2 | 445 | break; |
amiraseman | 23:a0ce9fa869c2 | 446 | } |
amiraseman | 23:a0ce9fa869c2 | 447 | } else { |
amiraseman | 23:a0ce9fa869c2 | 448 | lcd.clear(); |
amiraseman | 23:a0ce9fa869c2 | 449 | lcd.printString(" To Turn On",0,1); |
amiraseman | 23:a0ce9fa869c2 | 450 | lcd.printString(" Go To",0,2); |
amiraseman | 23:a0ce9fa869c2 | 451 | lcd.printString(" Settings",0,3); |
amiraseman | 23:a0ce9fa869c2 | 452 | lcd.printString ("Back",0,5); |
amiraseman | 23:a0ce9fa869c2 | 453 | |
amiraseman | 23:a0ce9fa869c2 | 454 | } |
amiraseman | 23:a0ce9fa869c2 | 455 | |
amiraseman | 23:a0ce9fa869c2 | 456 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 457 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 458 | break; |
amiraseman | 23:a0ce9fa869c2 | 459 | } |
amiraseman | 23:a0ce9fa869c2 | 460 | } |
amiraseman | 23:a0ce9fa869c2 | 461 | } |
amiraseman | 23:a0ce9fa869c2 | 462 | |
amiraseman | 23:a0ce9fa869c2 | 463 | |
amiraseman | 23:a0ce9fa869c2 | 464 | |
amiraseman | 23:a0ce9fa869c2 | 465 | /// Checks if the current temperture and pressure is equal to one of the thresholds set by the user, if yes it triggers the alarm and sets the buzzer |
amiraseman | 23:a0ce9fa869c2 | 466 | void thresholdCheck() |
amiraseman | 23:a0ce9fa869c2 | 467 | { |
amiraseman | 23:a0ce9fa869c2 | 468 | if (thresholdAlarmFlag) { |
amiraseman | 23:a0ce9fa869c2 | 469 | thresholdAlarmFlag = 1; |
amiraseman | 23:a0ce9fa869c2 | 470 | if (int(temperature) == fsmH[0].output) { //if the current temp is less than the set threshold |
amiraseman | 23:a0ce9fa869c2 | 471 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 472 | redLED = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 473 | strncpy(buffer0, bufferTime, 14); |
amiraseman | 23:a0ce9fa869c2 | 474 | int display2 = sprintf (buffer1, ""); |
amiraseman | 23:a0ce9fa869c2 | 475 | int display0 = sprintf (buffer2, "Threshold"); |
amiraseman | 23:a0ce9fa869c2 | 476 | int display1 = sprintf (buffer3, "Reached!"); |
amiraseman | 23:a0ce9fa869c2 | 477 | int display4 = sprintf (buffer4, "%0.1f!",temperature); |
amiraseman | 23:a0ce9fa869c2 | 478 | int display5 = sprintf (buffer5, "Stop"); |
amiraseman | 23:a0ce9fa869c2 | 479 | display(); |
amiraseman | 23:a0ce9fa869c2 | 480 | for (int i=0; i<=11; i++) { |
amiraseman | 23:a0ce9fa869c2 | 481 | buzzer.period(1/(frequency[i])); // set PWM period |
amiraseman | 23:a0ce9fa869c2 | 482 | buzzer=0.5; // set duty cycle |
amiraseman | 23:a0ce9fa869c2 | 483 | //wait(0.5); // hold for beat period |
amiraseman | 23:a0ce9fa869c2 | 484 | redLED = 1 ; |
amiraseman | 23:a0ce9fa869c2 | 485 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 486 | buzzer = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 487 | lcd.setBrightness(0.5); |
amiraseman | 23:a0ce9fa869c2 | 488 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 489 | goto exit ; |
amiraseman | 23:a0ce9fa869c2 | 490 | } |
amiraseman | 23:a0ce9fa869c2 | 491 | } |
amiraseman | 23:a0ce9fa869c2 | 492 | } |
amiraseman | 23:a0ce9fa869c2 | 493 | } |
amiraseman | 23:a0ce9fa869c2 | 494 | if (int(temperature) == fsmH[1].output) { //if the current temp is greater than the set threshold |
amiraseman | 23:a0ce9fa869c2 | 495 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 496 | redLED = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 497 | strncpy(buffer0, bufferTime, 14); |
amiraseman | 23:a0ce9fa869c2 | 498 | int display2 = sprintf (buffer1, ""); |
amiraseman | 23:a0ce9fa869c2 | 499 | int display0 = sprintf (buffer2, "Threshold"); |
amiraseman | 23:a0ce9fa869c2 | 500 | int display1 = sprintf (buffer3, "Reached!"); |
amiraseman | 23:a0ce9fa869c2 | 501 | int display4 = sprintf (buffer4, "%0.1f!",temperature ); |
amiraseman | 23:a0ce9fa869c2 | 502 | int display5 = sprintf (buffer5, "Stop"); |
amiraseman | 23:a0ce9fa869c2 | 503 | display(); |
amiraseman | 23:a0ce9fa869c2 | 504 | for (int i=0; i<=11; i++) { |
amiraseman | 23:a0ce9fa869c2 | 505 | buzzer.period(1/(frequency[i])); // set PWM period |
amiraseman | 23:a0ce9fa869c2 | 506 | buzzer=0.5; // set duty cycle |
amiraseman | 23:a0ce9fa869c2 | 507 | //wait(0.5); // hold for beat period |
amiraseman | 23:a0ce9fa869c2 | 508 | redLED = 1 ; |
amiraseman | 23:a0ce9fa869c2 | 509 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 510 | buzzer = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 511 | lcd.setBrightness(0.5); |
amiraseman | 23:a0ce9fa869c2 | 512 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 513 | goto exit ; |
amiraseman | 23:a0ce9fa869c2 | 514 | } |
amiraseman | 23:a0ce9fa869c2 | 515 | } |
amiraseman | 23:a0ce9fa869c2 | 516 | } |
amiraseman | 23:a0ce9fa869c2 | 517 | } |
amiraseman | 23:a0ce9fa869c2 | 518 | if (int(pressure) == fsmH[2].output) { //if the current pressure is less than the set threshold |
amiraseman | 23:a0ce9fa869c2 | 519 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 520 | redLED = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 521 | strncpy(buffer0, bufferTime, 14); |
amiraseman | 23:a0ce9fa869c2 | 522 | int display2 = sprintf (buffer1, ""); |
amiraseman | 23:a0ce9fa869c2 | 523 | int display0 = sprintf (buffer2, "Threshold"); |
amiraseman | 23:a0ce9fa869c2 | 524 | int display1 = sprintf (buffer3, "Reached!"); |
amiraseman | 23:a0ce9fa869c2 | 525 | int display4 = sprintf (buffer4, "%0.1f!",pressure); |
amiraseman | 23:a0ce9fa869c2 | 526 | int display5 = sprintf (buffer5, "Stop"); |
amiraseman | 23:a0ce9fa869c2 | 527 | display(); |
amiraseman | 23:a0ce9fa869c2 | 528 | for (int i=0; i<=11; i++) { |
amiraseman | 23:a0ce9fa869c2 | 529 | buzzer.period(1/(frequency[i])); // set PWM period |
amiraseman | 23:a0ce9fa869c2 | 530 | buzzer=0.5; // set duty cycle |
amiraseman | 23:a0ce9fa869c2 | 531 | redLED = 1 ; |
amiraseman | 23:a0ce9fa869c2 | 532 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 533 | buzzer = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 534 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 535 | goto exit ; |
amiraseman | 23:a0ce9fa869c2 | 536 | } |
amiraseman | 23:a0ce9fa869c2 | 537 | } |
amiraseman | 23:a0ce9fa869c2 | 538 | } |
amiraseman | 23:a0ce9fa869c2 | 539 | } |
amiraseman | 23:a0ce9fa869c2 | 540 | if (int(pressure) == fsmH[3].output) { //if the current pressure is greater than the set threshold |
amiraseman | 23:a0ce9fa869c2 | 541 | |
amiraseman | 23:a0ce9fa869c2 | 542 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 543 | redLED = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 544 | strncpy(buffer0, bufferTime, 14); |
amiraseman | 23:a0ce9fa869c2 | 545 | int display2 = sprintf (buffer1, ""); |
amiraseman | 23:a0ce9fa869c2 | 546 | int display0 = sprintf (buffer2, "Threshold"); |
amiraseman | 23:a0ce9fa869c2 | 547 | int display1 = sprintf (buffer3, "Reached!"); |
amiraseman | 23:a0ce9fa869c2 | 548 | int display4 = sprintf (buffer4, "%0.1f!",pressure); |
amiraseman | 23:a0ce9fa869c2 | 549 | int display5 = sprintf (buffer5, "Stop"); |
amiraseman | 23:a0ce9fa869c2 | 550 | display(); |
amiraseman | 23:a0ce9fa869c2 | 551 | for (int i=0; i<=11; i++) { |
amiraseman | 23:a0ce9fa869c2 | 552 | buzzer.period(1/(frequency[i])); // set PWM period |
amiraseman | 23:a0ce9fa869c2 | 553 | buzzer=1; // set duty cycle |
amiraseman | 23:a0ce9fa869c2 | 554 | redLED = 1 ; |
amiraseman | 23:a0ce9fa869c2 | 555 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 556 | buzzer = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 557 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 558 | goto exit ; |
amiraseman | 23:a0ce9fa869c2 | 559 | } |
amiraseman | 23:a0ce9fa869c2 | 560 | } |
amiraseman | 23:a0ce9fa869c2 | 561 | } |
amiraseman | 23:a0ce9fa869c2 | 562 | } |
amiraseman | 23:a0ce9fa869c2 | 563 | exit: |
amiraseman | 23:a0ce9fa869c2 | 564 | redLED = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 565 | } |
amiraseman | 23:a0ce9fa869c2 | 566 | |
amiraseman | 23:a0ce9fa869c2 | 567 | } |
amiraseman | 23:a0ce9fa869c2 | 568 | |
amiraseman | 23:a0ce9fa869c2 | 569 | ///Allows the user to set the minimum and maximum values of temperature and pressure for the thresholds |
amiraseman | 23:a0ce9fa869c2 | 570 | void threshold() |
amiraseman | 23:a0ce9fa869c2 | 571 | { |
amiraseman | 23:a0ce9fa869c2 | 572 | state = 0; |
amiraseman | 23:a0ce9fa869c2 | 573 | //sets the initial threshhold values to the values recieved from the sensor |
amiraseman | 23:a0ce9fa869c2 | 574 | readData(); |
amiraseman | 23:a0ce9fa869c2 | 575 | fsmH[0].output = temperature; |
amiraseman | 23:a0ce9fa869c2 | 576 | fsmH[1].output = temperature; |
amiraseman | 23:a0ce9fa869c2 | 577 | fsmH[2].output = pressure; |
amiraseman | 23:a0ce9fa869c2 | 578 | fsmH[3].output = pressure; |
amiraseman | 23:a0ce9fa869c2 | 579 | |
amiraseman | 23:a0ce9fa869c2 | 580 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 581 | alarmClockCheck(); |
amiraseman | 23:a0ce9fa869c2 | 582 | powerSaverCheck(); |
amiraseman | 23:a0ce9fa869c2 | 583 | updateTime(); |
amiraseman | 23:a0ce9fa869c2 | 584 | loggerData(); |
amiraseman | 23:a0ce9fa869c2 | 585 | |
amiraseman | 23:a0ce9fa869c2 | 586 | |
amiraseman | 23:a0ce9fa869c2 | 587 | |
amiraseman | 23:a0ce9fa869c2 | 588 | int display0 = sprintf (buffer0, " Back Save");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 589 | int display1 = sprintf (buffer1, "");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 590 | int display2 = sprintf (buffer2, "");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 591 | int display3 = sprintf (buffer3, ">>%s %d", fsmH[state].title,fsmH[state].output);//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 592 | int display4 = sprintf (buffer4, "");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 593 | int display5 = sprintf (buffer5, "");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 594 | |
amiraseman | 23:a0ce9fa869c2 | 595 | |
amiraseman | 23:a0ce9fa869c2 | 596 | display(); |
amiraseman | 23:a0ce9fa869c2 | 597 | |
amiraseman | 23:a0ce9fa869c2 | 598 | |
amiraseman | 23:a0ce9fa869c2 | 599 | |
amiraseman | 23:a0ce9fa869c2 | 600 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 601 | button4Flag= 0; |
amiraseman | 23:a0ce9fa869c2 | 602 | fsmH[state].output ++; |
amiraseman | 23:a0ce9fa869c2 | 603 | } |
amiraseman | 23:a0ce9fa869c2 | 604 | |
amiraseman | 23:a0ce9fa869c2 | 605 | |
amiraseman | 23:a0ce9fa869c2 | 606 | |
amiraseman | 23:a0ce9fa869c2 | 607 | |
amiraseman | 23:a0ce9fa869c2 | 608 | //Navigates to the next5 state of the fsm when one of the buttons is pressed |
amiraseman | 23:a0ce9fa869c2 | 609 | if (button1Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 610 | button1Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 611 | state=fsmH[state].nextState[1]; |
amiraseman | 23:a0ce9fa869c2 | 612 | } |
amiraseman | 23:a0ce9fa869c2 | 613 | if (button2Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 614 | button2Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 615 | state=fsmH[state].nextState[0]; |
amiraseman | 23:a0ce9fa869c2 | 616 | } |
amiraseman | 23:a0ce9fa869c2 | 617 | |
amiraseman | 23:a0ce9fa869c2 | 618 | |
amiraseman | 23:a0ce9fa869c2 | 619 | //checks the limits of the values |
amiraseman | 23:a0ce9fa869c2 | 620 | if (fsmH[0].output >(temperature*2)) { //min temperature |
amiraseman | 23:a0ce9fa869c2 | 621 | fsmH[0].output = -(temperature*2) ; |
amiraseman | 23:a0ce9fa869c2 | 622 | } |
amiraseman | 23:a0ce9fa869c2 | 623 | if (fsmH[1].output>(temperature*2)) { // max temperature |
amiraseman | 23:a0ce9fa869c2 | 624 | fsmH[1].output = -(temperature*2) ; |
amiraseman | 23:a0ce9fa869c2 | 625 | } |
amiraseman | 23:a0ce9fa869c2 | 626 | if (fsmH[2].output>(pressure*2)) { //min pressure |
amiraseman | 23:a0ce9fa869c2 | 627 | fsmH[2].output = -(pressure*2) ; |
amiraseman | 23:a0ce9fa869c2 | 628 | } |
amiraseman | 23:a0ce9fa869c2 | 629 | if (fsmH[3].output>(pressure*2)) { //max pressure |
amiraseman | 23:a0ce9fa869c2 | 630 | fsmH[3].output = -(pressure*2) ; |
amiraseman | 23:a0ce9fa869c2 | 631 | } |
amiraseman | 23:a0ce9fa869c2 | 632 | |
amiraseman | 23:a0ce9fa869c2 | 633 | |
amiraseman | 23:a0ce9fa869c2 | 634 | if (button3Flag) {//if wants to go back |
amiraseman | 23:a0ce9fa869c2 | 635 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 636 | state = 0; |
amiraseman | 23:a0ce9fa869c2 | 637 | while (1) { //runs a while loop |
amiraseman | 23:a0ce9fa869c2 | 638 | int display0 = sprintf (buffer0, ""); |
amiraseman | 23:a0ce9fa869c2 | 639 | int display1 = sprintf (buffer1, ""); |
amiraseman | 23:a0ce9fa869c2 | 640 | int display2 = sprintf (buffer2, "Set Thresholds?"); //asks to set the threshold |
amiraseman | 23:a0ce9fa869c2 | 641 | int display3 = sprintf (buffer3, ">>%s", fsmG[state].title); // yes or no (1 or 0 output) |
amiraseman | 23:a0ce9fa869c2 | 642 | int display4 = sprintf (buffer4, ""); |
amiraseman | 23:a0ce9fa869c2 | 643 | int display5 = sprintf (buffer5, ""); |
amiraseman | 23:a0ce9fa869c2 | 644 | display(); |
amiraseman | 23:a0ce9fa869c2 | 645 | thresholdAlarmFlag = fsmG[state].output ; //yes or no (1 or 0) |
amiraseman | 23:a0ce9fa869c2 | 646 | |
amiraseman | 23:a0ce9fa869c2 | 647 | |
amiraseman | 23:a0ce9fa869c2 | 648 | //Navigates to the next state of the fsm when one of the buttons is pressed |
amiraseman | 23:a0ce9fa869c2 | 649 | if (button1Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 650 | button1Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 651 | state=fsmG[state].nextState[1]; |
amiraseman | 23:a0ce9fa869c2 | 652 | } |
amiraseman | 23:a0ce9fa869c2 | 653 | if (button2Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 654 | button2Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 655 | state=fsmG[state].nextState[0]; |
amiraseman | 23:a0ce9fa869c2 | 656 | } |
amiraseman | 23:a0ce9fa869c2 | 657 | |
amiraseman | 23:a0ce9fa869c2 | 658 | |
amiraseman | 23:a0ce9fa869c2 | 659 | |
amiraseman | 23:a0ce9fa869c2 | 660 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 661 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 662 | //serial.printf("thresholds flag :%i \n",fsmG[state].output); |
amiraseman | 23:a0ce9fa869c2 | 663 | //serial.printf("thresholds: mint%i maxt%i minP%i maxP %i \n",fsmH[0].output,fsmH[1].output,fsmH[2].output,fsmH[3].output); |
amiraseman | 23:a0ce9fa869c2 | 664 | goto exit; |
amiraseman | 23:a0ce9fa869c2 | 665 | } |
amiraseman | 23:a0ce9fa869c2 | 666 | } |
amiraseman | 23:a0ce9fa869c2 | 667 | } |
amiraseman | 23:a0ce9fa869c2 | 668 | } |
amiraseman | 23:a0ce9fa869c2 | 669 | exit: |
amiraseman | 23:a0ce9fa869c2 | 670 | return; |
amiraseman | 23:a0ce9fa869c2 | 671 | } |
amiraseman | 23:a0ce9fa869c2 | 672 | |
amiraseman | 23:a0ce9fa869c2 | 673 | |
amiraseman | 23:a0ce9fa869c2 | 674 | |
amiraseman | 23:a0ce9fa869c2 | 675 | |
amiraseman | 23:a0ce9fa869c2 | 676 | |
amiraseman | 23:a0ce9fa869c2 | 677 | /// Compares the time set by the user in the alarm clock section with the current time, turn on the buzzer if they are equal |
amiraseman | 23:a0ce9fa869c2 | 678 | void alarmClockCheck() |
amiraseman | 23:a0ce9fa869c2 | 679 | { |
amiraseman | 23:a0ce9fa869c2 | 680 | if (alarmClockFlag) { |
amiraseman | 23:a0ce9fa869c2 | 681 | if (currentTime == UNIXdate) { //if the current time is equal to the time set by user |
amiraseman | 23:a0ce9fa869c2 | 682 | //serial.printf("Alarm clock flag is set\n"); |
amiraseman | 23:a0ce9fa869c2 | 683 | alarmClockFlag =0 ; |
amiraseman | 23:a0ce9fa869c2 | 684 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 685 | int display0 = sprintf (buffer0, ""); |
amiraseman | 23:a0ce9fa869c2 | 686 | int display1 = sprintf (buffer1, "Alarm !"); |
amiraseman | 23:a0ce9fa869c2 | 687 | strncpy(buffer2, bufferTime, 14); |
amiraseman | 23:a0ce9fa869c2 | 688 | int display2 = sprintf (buffer3, ""); |
amiraseman | 23:a0ce9fa869c2 | 689 | int display4 = sprintf (buffer4, ""); |
amiraseman | 23:a0ce9fa869c2 | 690 | int display5 = sprintf (buffer5, "Stop"); |
amiraseman | 23:a0ce9fa869c2 | 691 | display(); |
amiraseman | 23:a0ce9fa869c2 | 692 | for (int i=0; i<=11; i++) { |
amiraseman | 23:a0ce9fa869c2 | 693 | buzzer.period(1/(frequency[i])); // set PWM period |
amiraseman | 23:a0ce9fa869c2 | 694 | buzzer=0.5; // set duty cycle |
amiraseman | 23:a0ce9fa869c2 | 695 | wait(0.5); // hold for beat period |
amiraseman | 23:a0ce9fa869c2 | 696 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 697 | buzzer = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 698 | lcd.setBrightness(0.5); |
amiraseman | 23:a0ce9fa869c2 | 699 | break; |
amiraseman | 23:a0ce9fa869c2 | 700 | } |
amiraseman | 23:a0ce9fa869c2 | 701 | } |
amiraseman | 23:a0ce9fa869c2 | 702 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 703 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 704 | break; |
amiraseman | 23:a0ce9fa869c2 | 705 | } |
amiraseman | 23:a0ce9fa869c2 | 706 | |
amiraseman | 23:a0ce9fa869c2 | 707 | } |
amiraseman | 23:a0ce9fa869c2 | 708 | } |
amiraseman | 23:a0ce9fa869c2 | 709 | } |
amiraseman | 23:a0ce9fa869c2 | 710 | } |
amiraseman | 23:a0ce9fa869c2 | 711 | |
amiraseman | 23:a0ce9fa869c2 | 712 | |
amiraseman | 23:a0ce9fa869c2 | 713 | |
amiraseman | 23:a0ce9fa869c2 | 714 | |
amiraseman | 23:a0ce9fa869c2 | 715 | |
amiraseman | 23:a0ce9fa869c2 | 716 | |
amiraseman | 23:a0ce9fa869c2 | 717 | /// Allows the user to set the time for the alarm clock |
amiraseman | 23:a0ce9fa869c2 | 718 | void alarmClock () |
amiraseman | 23:a0ce9fa869c2 | 719 | { |
amiraseman | 23:a0ce9fa869c2 | 720 | state = 0; |
amiraseman | 23:a0ce9fa869c2 | 721 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 722 | thresholdCheck(); |
amiraseman | 23:a0ce9fa869c2 | 723 | alarmClockCheck(); |
amiraseman | 23:a0ce9fa869c2 | 724 | powerSaverCheck(); |
amiraseman | 23:a0ce9fa869c2 | 725 | updateTime(); |
amiraseman | 23:a0ce9fa869c2 | 726 | loggerData(); |
amiraseman | 23:a0ce9fa869c2 | 727 | |
amiraseman | 23:a0ce9fa869c2 | 728 | |
amiraseman | 23:a0ce9fa869c2 | 729 | int display0 = sprintf (buffer0, " Back Save");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 730 | int display1 = sprintf (buffer1, "");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 731 | int display2 = sprintf (buffer2, "" );//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 732 | int display3 = sprintf (buffer3, ">>%s %d", fsmC[state].title,fsmC[state].output);//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 733 | int display4 = sprintf (buffer4, "" );//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 734 | int display5 = sprintf (buffer5, "" );//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 735 | |
amiraseman | 23:a0ce9fa869c2 | 736 | |
amiraseman | 23:a0ce9fa869c2 | 737 | |
amiraseman | 23:a0ce9fa869c2 | 738 | display(); |
amiraseman | 23:a0ce9fa869c2 | 739 | |
amiraseman | 23:a0ce9fa869c2 | 740 | |
amiraseman | 23:a0ce9fa869c2 | 741 | |
amiraseman | 23:a0ce9fa869c2 | 742 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 743 | button4Flag= 0; |
amiraseman | 23:a0ce9fa869c2 | 744 | fsmC[state].output ++; |
amiraseman | 23:a0ce9fa869c2 | 745 | calculateUNIXTime(); |
amiraseman | 23:a0ce9fa869c2 | 746 | } |
amiraseman | 23:a0ce9fa869c2 | 747 | |
amiraseman | 23:a0ce9fa869c2 | 748 | |
amiraseman | 23:a0ce9fa869c2 | 749 | |
amiraseman | 23:a0ce9fa869c2 | 750 | //Navigates to the next5 state of the fsm when one of the buttons is pressed |
amiraseman | 23:a0ce9fa869c2 | 751 | if (button1Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 752 | button1Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 753 | state=fsmC[state].nextState[1]; |
amiraseman | 23:a0ce9fa869c2 | 754 | } |
amiraseman | 23:a0ce9fa869c2 | 755 | if (button2Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 756 | button2Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 757 | state=fsmC[state].nextState[0]; |
amiraseman | 23:a0ce9fa869c2 | 758 | } |
amiraseman | 23:a0ce9fa869c2 | 759 | |
amiraseman | 23:a0ce9fa869c2 | 760 | |
amiraseman | 23:a0ce9fa869c2 | 761 | |
amiraseman | 23:a0ce9fa869c2 | 762 | //checks the limits of the values |
amiraseman | 23:a0ce9fa869c2 | 763 | if (fsmC[0].output >23) { //hour |
amiraseman | 23:a0ce9fa869c2 | 764 | fsmC[0].output = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 765 | } |
amiraseman | 23:a0ce9fa869c2 | 766 | if (fsmC[1].output>59) { // minute |
amiraseman | 23:a0ce9fa869c2 | 767 | fsmC[1].output = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 768 | } |
amiraseman | 23:a0ce9fa869c2 | 769 | if (fsmC[3].output>11) { //month |
amiraseman | 23:a0ce9fa869c2 | 770 | fsmC[3].output = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 771 | } |
amiraseman | 23:a0ce9fa869c2 | 772 | if (fsmC[2].output>31) { //day |
amiraseman | 23:a0ce9fa869c2 | 773 | fsmC[2].output = 1 ; |
amiraseman | 23:a0ce9fa869c2 | 774 | } |
amiraseman | 23:a0ce9fa869c2 | 775 | if (fsmC[4].output>2030) { //year |
amiraseman | 23:a0ce9fa869c2 | 776 | fsmC[4].output = 2015 ; |
amiraseman | 23:a0ce9fa869c2 | 777 | } |
amiraseman | 23:a0ce9fa869c2 | 778 | |
amiraseman | 23:a0ce9fa869c2 | 779 | |
amiraseman | 23:a0ce9fa869c2 | 780 | |
amiraseman | 23:a0ce9fa869c2 | 781 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 782 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 783 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 784 | int display0 = sprintf (buffer0, ""); |
amiraseman | 23:a0ce9fa869c2 | 785 | int display1 = sprintf (buffer1, ""); |
amiraseman | 23:a0ce9fa869c2 | 786 | int display2 = sprintf (buffer2, "Set Alarm?"); |
amiraseman | 23:a0ce9fa869c2 | 787 | int display3 = sprintf (buffer3, ">>%s", fsmG[state].title); |
amiraseman | 23:a0ce9fa869c2 | 788 | int display4 = sprintf (buffer4, ""); |
amiraseman | 23:a0ce9fa869c2 | 789 | int display5 = sprintf (buffer5, ""); |
amiraseman | 23:a0ce9fa869c2 | 790 | display(); |
amiraseman | 23:a0ce9fa869c2 | 791 | alarmClockFlag = fsmG[state].output ; |
amiraseman | 23:a0ce9fa869c2 | 792 | if (state>1) { |
amiraseman | 23:a0ce9fa869c2 | 793 | state = 0; |
amiraseman | 23:a0ce9fa869c2 | 794 | } |
amiraseman | 23:a0ce9fa869c2 | 795 | if (state<0) { |
amiraseman | 23:a0ce9fa869c2 | 796 | state = 1; |
amiraseman | 23:a0ce9fa869c2 | 797 | } |
amiraseman | 23:a0ce9fa869c2 | 798 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 799 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 800 | goto exit; |
amiraseman | 23:a0ce9fa869c2 | 801 | } |
amiraseman | 23:a0ce9fa869c2 | 802 | |
amiraseman | 23:a0ce9fa869c2 | 803 | |
amiraseman | 23:a0ce9fa869c2 | 804 | //Navigates to the next5 state of the fsm when one of the buttons is pressed |
amiraseman | 23:a0ce9fa869c2 | 805 | if (button1Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 806 | button1Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 807 | state=fsmG[state].nextState[1]; |
amiraseman | 23:a0ce9fa869c2 | 808 | } |
amiraseman | 23:a0ce9fa869c2 | 809 | if (button2Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 810 | button2Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 811 | state=fsmG[state].nextState[0]; |
amiraseman | 23:a0ce9fa869c2 | 812 | } |
amiraseman | 23:a0ce9fa869c2 | 813 | } |
amiraseman | 23:a0ce9fa869c2 | 814 | } |
amiraseman | 23:a0ce9fa869c2 | 815 | } |
amiraseman | 23:a0ce9fa869c2 | 816 | exit: |
amiraseman | 23:a0ce9fa869c2 | 817 | return; |
amiraseman | 23:a0ce9fa869c2 | 818 | } |
amiraseman | 23:a0ce9fa869c2 | 819 | |
amiraseman | 23:a0ce9fa869c2 | 820 | |
amiraseman | 23:a0ce9fa869c2 | 821 | |
amiraseman | 23:a0ce9fa869c2 | 822 | |
amiraseman | 23:a0ce9fa869c2 | 823 | ///A menu to indicate the alarm options (Alarm clock and thresholds alarm), also allows the user to navigate through the given options |
amiraseman | 23:a0ce9fa869c2 | 824 | void alarmsMenu () |
amiraseman | 23:a0ce9fa869c2 | 825 | { |
amiraseman | 23:a0ce9fa869c2 | 826 | state = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 827 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 828 | thresholdCheck(); |
amiraseman | 23:a0ce9fa869c2 | 829 | alarmClockCheck(); |
amiraseman | 23:a0ce9fa869c2 | 830 | powerSaverCheck(); |
amiraseman | 23:a0ce9fa869c2 | 831 | updateTime(); |
amiraseman | 23:a0ce9fa869c2 | 832 | loggerData(); |
amiraseman | 23:a0ce9fa869c2 | 833 | int display0 = sprintf (buffer0, " Back Next"); |
amiraseman | 23:a0ce9fa869c2 | 834 | int display1 = sprintf (buffer1, ""); |
amiraseman | 23:a0ce9fa869c2 | 835 | int display2 = sprintf (buffer2, ""); |
amiraseman | 23:a0ce9fa869c2 | 836 | int display3 = sprintf (buffer3, ">>%s", fsmF[state].title); |
amiraseman | 23:a0ce9fa869c2 | 837 | int display4 = sprintf (buffer4, ""); |
amiraseman | 23:a0ce9fa869c2 | 838 | int display5 = sprintf (buffer5, ""); |
amiraseman | 23:a0ce9fa869c2 | 839 | |
amiraseman | 23:a0ce9fa869c2 | 840 | display(); |
amiraseman | 23:a0ce9fa869c2 | 841 | |
amiraseman | 23:a0ce9fa869c2 | 842 | |
amiraseman | 23:a0ce9fa869c2 | 843 | |
amiraseman | 23:a0ce9fa869c2 | 844 | if (state > 1) { |
amiraseman | 23:a0ce9fa869c2 | 845 | state = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 846 | } |
amiraseman | 23:a0ce9fa869c2 | 847 | if (state < 0) { |
amiraseman | 23:a0ce9fa869c2 | 848 | state = 1 ; |
amiraseman | 23:a0ce9fa869c2 | 849 | } |
amiraseman | 23:a0ce9fa869c2 | 850 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 851 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 852 | state = 0; |
amiraseman | 23:a0ce9fa869c2 | 853 | break; |
amiraseman | 23:a0ce9fa869c2 | 854 | } |
amiraseman | 23:a0ce9fa869c2 | 855 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 856 | if (state == 0) { // temp and pressure thresholds |
amiraseman | 23:a0ce9fa869c2 | 857 | threshold(); |
amiraseman | 23:a0ce9fa869c2 | 858 | } |
amiraseman | 23:a0ce9fa869c2 | 859 | if (state == 1) {// alarm clock |
amiraseman | 23:a0ce9fa869c2 | 860 | |
amiraseman | 23:a0ce9fa869c2 | 861 | alarmClock(); |
amiraseman | 23:a0ce9fa869c2 | 862 | } |
amiraseman | 23:a0ce9fa869c2 | 863 | } |
amiraseman | 23:a0ce9fa869c2 | 864 | //Navigates to the next5 state of the fsm when one of the buttons is pressed |
amiraseman | 23:a0ce9fa869c2 | 865 | if (button1Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 866 | button1Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 867 | state=fsmF[state].nextState[1]; |
amiraseman | 23:a0ce9fa869c2 | 868 | } |
amiraseman | 23:a0ce9fa869c2 | 869 | if (button2Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 870 | button2Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 871 | state=fsmF[state].nextState[0]; |
amiraseman | 23:a0ce9fa869c2 | 872 | } |
amiraseman | 23:a0ce9fa869c2 | 873 | } |
amiraseman | 23:a0ce9fa869c2 | 874 | } |
amiraseman | 23:a0ce9fa869c2 | 875 | |
amiraseman | 23:a0ce9fa869c2 | 876 | |
amiraseman | 23:a0ce9fa869c2 | 877 | |
amiraseman | 23:a0ce9fa869c2 | 878 | /// Allows the user to turn on the data logger and set the time interval between the readings |
amiraseman | 23:a0ce9fa869c2 | 879 | void dataLoggerSetting() |
amiraseman | 23:a0ce9fa869c2 | 880 | { |
amiraseman | 23:a0ce9fa869c2 | 881 | state = runLoggerFlag ; //inits the state to the saved flag by the user in the past |
amiraseman | 23:a0ce9fa869c2 | 882 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 883 | thresholdCheck(); |
amiraseman | 23:a0ce9fa869c2 | 884 | alarmClockCheck(); |
amiraseman | 23:a0ce9fa869c2 | 885 | powerSaverCheck(); |
amiraseman | 23:a0ce9fa869c2 | 886 | updateTime(); |
amiraseman | 23:a0ce9fa869c2 | 887 | loggerData(); |
amiraseman | 23:a0ce9fa869c2 | 888 | sprintf (buffer0, " Back Save"); |
amiraseman | 23:a0ce9fa869c2 | 889 | sprintf (buffer1, ""); |
amiraseman | 23:a0ce9fa869c2 | 890 | sprintf (buffer2, "Data Logger"); |
amiraseman | 23:a0ce9fa869c2 | 891 | sprintf (buffer3, ">>%s", fsmE[state].title); |
amiraseman | 23:a0ce9fa869c2 | 892 | if (state == 1) { |
amiraseman | 23:a0ce9fa869c2 | 893 | sprintf (buffer4, "%i mins", fsmE[state].output/60); |
amiraseman | 23:a0ce9fa869c2 | 894 | } else { // if the power saver is off it doesnt show the time |
amiraseman | 23:a0ce9fa869c2 | 895 | sprintf (buffer4, ""); |
amiraseman | 23:a0ce9fa869c2 | 896 | } |
amiraseman | 23:a0ce9fa869c2 | 897 | sprintf (buffer5, ""); |
amiraseman | 23:a0ce9fa869c2 | 898 | |
amiraseman | 23:a0ce9fa869c2 | 899 | |
amiraseman | 23:a0ce9fa869c2 | 900 | display(); |
amiraseman | 23:a0ce9fa869c2 | 901 | |
amiraseman | 23:a0ce9fa869c2 | 902 | |
amiraseman | 23:a0ce9fa869c2 | 903 | |
amiraseman | 23:a0ce9fa869c2 | 904 | runLoggerFlag = state ; //sets the flag to the value of state (0 is off, 1 is on) |
amiraseman | 23:a0ce9fa869c2 | 905 | dataLoggerTime = fsmE[state].output; //sets the timer to to value of output from the fsm |
amiraseman | 23:a0ce9fa869c2 | 906 | // serial.printf("data logger Time = %i \n dataLoggerFlag = %i \n",dataLoggerTime , runLoggerFlag); |
amiraseman | 23:a0ce9fa869c2 | 907 | |
amiraseman | 23:a0ce9fa869c2 | 908 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 909 | button4Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 910 | fsmE[state].output += 60 ; //adds a minute to the timer when button 4 is pressed |
amiraseman | 23:a0ce9fa869c2 | 911 | } |
amiraseman | 23:a0ce9fa869c2 | 912 | if (fsmE[state].output > 1800) { |
amiraseman | 23:a0ce9fa869c2 | 913 | fsmE[state].output = 60 ; |
amiraseman | 23:a0ce9fa869c2 | 914 | } |
amiraseman | 23:a0ce9fa869c2 | 915 | |
amiraseman | 23:a0ce9fa869c2 | 916 | if (state > 1) { |
amiraseman | 23:a0ce9fa869c2 | 917 | state = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 918 | } |
amiraseman | 23:a0ce9fa869c2 | 919 | |
amiraseman | 23:a0ce9fa869c2 | 920 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 921 | if (state == 1) { // if the data logger is swiched on |
amiraseman | 23:a0ce9fa869c2 | 922 | dataLoggerTimer.attach(&dataLoggerTimerExpired, dataLoggerTime); // set the timer |
amiraseman | 23:a0ce9fa869c2 | 923 | } |
amiraseman | 23:a0ce9fa869c2 | 924 | if (state == 0) { // if the data logger is swiched off |
amiraseman | 23:a0ce9fa869c2 | 925 | dataLoggerTimer.detach(); // detach the timer |
amiraseman | 23:a0ce9fa869c2 | 926 | } |
amiraseman | 23:a0ce9fa869c2 | 927 | dataLoggerFlag = 1; |
amiraseman | 23:a0ce9fa869c2 | 928 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 929 | state = 0; |
amiraseman | 23:a0ce9fa869c2 | 930 | break; |
amiraseman | 23:a0ce9fa869c2 | 931 | } |
amiraseman | 23:a0ce9fa869c2 | 932 | //Navigates to the next5 state of the fsm when one of the buttons is pressed |
amiraseman | 23:a0ce9fa869c2 | 933 | if (button1Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 934 | button1Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 935 | state=fsmE[state].nextState[1]; |
amiraseman | 23:a0ce9fa869c2 | 936 | } |
amiraseman | 23:a0ce9fa869c2 | 937 | if (button2Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 938 | button2Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 939 | state=fsmE[state].nextState[0]; |
amiraseman | 23:a0ce9fa869c2 | 940 | } |
amiraseman | 23:a0ce9fa869c2 | 941 | } |
amiraseman | 23:a0ce9fa869c2 | 942 | |
amiraseman | 23:a0ce9fa869c2 | 943 | } |
amiraseman | 23:a0ce9fa869c2 | 944 | |
amiraseman | 23:a0ce9fa869c2 | 945 | |
amiraseman | 23:a0ce9fa869c2 | 946 | |
amiraseman | 23:a0ce9fa869c2 | 947 | |
amiraseman | 23:a0ce9fa869c2 | 948 | |
amiraseman | 23:a0ce9fa869c2 | 949 | |
amiraseman | 23:a0ce9fa869c2 | 950 | |
amiraseman | 23:a0ce9fa869c2 | 951 | |
amiraseman | 23:a0ce9fa869c2 | 952 | |
amiraseman | 23:a0ce9fa869c2 | 953 | |
amiraseman | 23:a0ce9fa869c2 | 954 | |
amiraseman | 23:a0ce9fa869c2 | 955 | /// Allows the user to set the power saving option and set the timeout |
amiraseman | 23:a0ce9fa869c2 | 956 | void powerSaverSetting() |
amiraseman | 23:a0ce9fa869c2 | 957 | { |
amiraseman | 23:a0ce9fa869c2 | 958 | |
amiraseman | 23:a0ce9fa869c2 | 959 | state = powerSaverFlag ; //inits the state to the saved flag by the user in the past |
amiraseman | 23:a0ce9fa869c2 | 960 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 961 | thresholdCheck(); |
amiraseman | 23:a0ce9fa869c2 | 962 | alarmClockCheck(); |
amiraseman | 23:a0ce9fa869c2 | 963 | powerSaverCheck(); |
amiraseman | 23:a0ce9fa869c2 | 964 | updateTime(); |
amiraseman | 23:a0ce9fa869c2 | 965 | loggerData(); |
amiraseman | 23:a0ce9fa869c2 | 966 | sprintf (buffer0, " Back Save"); |
amiraseman | 23:a0ce9fa869c2 | 967 | sprintf (buffer1, ""); |
amiraseman | 23:a0ce9fa869c2 | 968 | sprintf (buffer2, "Power Saver"); |
amiraseman | 23:a0ce9fa869c2 | 969 | sprintf (buffer3, ">>%s", fsmE[state].title); |
amiraseman | 23:a0ce9fa869c2 | 970 | if (state == 1) { |
amiraseman | 23:a0ce9fa869c2 | 971 | sprintf (buffer4, "%i mins", fsmE[state].output/60); |
amiraseman | 23:a0ce9fa869c2 | 972 | } else { // if the power saver is off it dowsnt show the time |
amiraseman | 23:a0ce9fa869c2 | 973 | sprintf (buffer4, ""); |
amiraseman | 23:a0ce9fa869c2 | 974 | } |
amiraseman | 23:a0ce9fa869c2 | 975 | sprintf (buffer5, ""); |
amiraseman | 23:a0ce9fa869c2 | 976 | |
amiraseman | 23:a0ce9fa869c2 | 977 | |
amiraseman | 23:a0ce9fa869c2 | 978 | display(); |
amiraseman | 23:a0ce9fa869c2 | 979 | |
amiraseman | 23:a0ce9fa869c2 | 980 | powerSaverTime = fsmE[state].output; //sets the timer to to value of output from the fsm |
amiraseman | 23:a0ce9fa869c2 | 981 | //serial.printf("Power Saver Time = %i \n powerSaverFlag = %i \n",powerSaverTime , powerSaverFlag); |
amiraseman | 23:a0ce9fa869c2 | 982 | |
amiraseman | 23:a0ce9fa869c2 | 983 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 984 | button4Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 985 | fsmE[state].output += 60 ; //adds a minute to the timer when button 4 is pressed |
amiraseman | 23:a0ce9fa869c2 | 986 | } |
amiraseman | 23:a0ce9fa869c2 | 987 | if (fsmE[state].output > 600) { // maximum time is 10 minutes |
amiraseman | 23:a0ce9fa869c2 | 988 | fsmE[state].output = 60 ; |
amiraseman | 23:a0ce9fa869c2 | 989 | } |
amiraseman | 23:a0ce9fa869c2 | 990 | |
amiraseman | 23:a0ce9fa869c2 | 991 | if (state > 1) { |
amiraseman | 23:a0ce9fa869c2 | 992 | state = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 993 | } |
amiraseman | 23:a0ce9fa869c2 | 994 | |
amiraseman | 23:a0ce9fa869c2 | 995 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 996 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 997 | |
amiraseman | 23:a0ce9fa869c2 | 998 | if (state== 1) { |
amiraseman | 23:a0ce9fa869c2 | 999 | powerSaverTimeout.attach(&powerSaverExpired, powerSaverTime); // setup a timeiut to call the savePower function |
amiraseman | 23:a0ce9fa869c2 | 1000 | } |
amiraseman | 23:a0ce9fa869c2 | 1001 | break; |
amiraseman | 23:a0ce9fa869c2 | 1002 | } |
amiraseman | 23:a0ce9fa869c2 | 1003 | |
amiraseman | 23:a0ce9fa869c2 | 1004 | |
amiraseman | 23:a0ce9fa869c2 | 1005 | //Navigates to the next5 state of the fsm when one of the buttons is pressed |
amiraseman | 23:a0ce9fa869c2 | 1006 | if (button1Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 1007 | button1Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1008 | state=fsmF[state].nextState[1]; |
amiraseman | 23:a0ce9fa869c2 | 1009 | } |
amiraseman | 23:a0ce9fa869c2 | 1010 | if (button2Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 1011 | button2Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1012 | state=fsmF[state].nextState[0]; |
amiraseman | 23:a0ce9fa869c2 | 1013 | } |
amiraseman | 23:a0ce9fa869c2 | 1014 | } |
amiraseman | 23:a0ce9fa869c2 | 1015 | } |
amiraseman | 23:a0ce9fa869c2 | 1016 | |
amiraseman | 23:a0ce9fa869c2 | 1017 | |
amiraseman | 23:a0ce9fa869c2 | 1018 | |
amiraseman | 23:a0ce9fa869c2 | 1019 | |
amiraseman | 23:a0ce9fa869c2 | 1020 | |
amiraseman | 23:a0ce9fa869c2 | 1021 | |
amiraseman | 23:a0ce9fa869c2 | 1022 | |
amiraseman | 23:a0ce9fa869c2 | 1023 | /// Converts the entered raw date and time values by the user to create the unix time stamp |
amiraseman | 23:a0ce9fa869c2 | 1024 | void calculateUNIXTime() |
amiraseman | 23:a0ce9fa869c2 | 1025 | { |
amiraseman | 23:a0ce9fa869c2 | 1026 | |
amiraseman | 23:a0ce9fa869c2 | 1027 | time_t rawtime; |
amiraseman | 23:a0ce9fa869c2 | 1028 | struct tm * timeinfo; |
amiraseman | 23:a0ce9fa869c2 | 1029 | |
amiraseman | 23:a0ce9fa869c2 | 1030 | // get current timeinfo |
amiraseman | 23:a0ce9fa869c2 | 1031 | time ( &rawtime ); |
amiraseman | 23:a0ce9fa869c2 | 1032 | // convert to struct |
amiraseman | 23:a0ce9fa869c2 | 1033 | timeinfo = localtime ( &rawtime ); |
amiraseman | 23:a0ce9fa869c2 | 1034 | // now modify the timeinfo to the given date |
amiraseman | 23:a0ce9fa869c2 | 1035 | timeinfo->tm_year = (fsmC[4].output) - 1900; |
amiraseman | 23:a0ce9fa869c2 | 1036 | timeinfo->tm_mon = (fsmC[3].output) - 1; //months since January - [0,11] |
amiraseman | 23:a0ce9fa869c2 | 1037 | timeinfo->tm_mday = (fsmC[2].output) ; //day of the month - [1,31] |
amiraseman | 23:a0ce9fa869c2 | 1038 | timeinfo->tm_hour = (fsmC[0].output) ; //hours since midnight - [0,23] |
amiraseman | 23:a0ce9fa869c2 | 1039 | timeinfo->tm_min = (fsmC[1].output) ; //minutes after the hour - [0,59] |
amiraseman | 23:a0ce9fa869c2 | 1040 | timeinfo->tm_sec = 0 ; //seconds after the minute - [0,59] |
amiraseman | 23:a0ce9fa869c2 | 1041 | |
amiraseman | 23:a0ce9fa869c2 | 1042 | //call mktime to create unix time stamp from timeinfo struct |
amiraseman | 23:a0ce9fa869c2 | 1043 | UNIXdate = mktime ( timeinfo ); |
amiraseman | 23:a0ce9fa869c2 | 1044 | |
amiraseman | 23:a0ce9fa869c2 | 1045 | } |
amiraseman | 23:a0ce9fa869c2 | 1046 | |
amiraseman | 23:a0ce9fa869c2 | 1047 | |
amiraseman | 23:a0ce9fa869c2 | 1048 | |
amiraseman | 23:a0ce9fa869c2 | 1049 | |
amiraseman | 23:a0ce9fa869c2 | 1050 | /// Allows the user to change the units for temperature and pressure |
amiraseman | 23:a0ce9fa869c2 | 1051 | void unitsSetting() |
amiraseman | 23:a0ce9fa869c2 | 1052 | { |
amiraseman | 23:a0ce9fa869c2 | 1053 | state = 0; |
amiraseman | 23:a0ce9fa869c2 | 1054 | powerSaverCheck(); |
amiraseman | 23:a0ce9fa869c2 | 1055 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 1056 | thresholdCheck(); |
amiraseman | 23:a0ce9fa869c2 | 1057 | alarmClockCheck(); |
amiraseman | 23:a0ce9fa869c2 | 1058 | updateTime(); |
amiraseman | 23:a0ce9fa869c2 | 1059 | loggerData(); |
amiraseman | 23:a0ce9fa869c2 | 1060 | |
amiraseman | 23:a0ce9fa869c2 | 1061 | sprintf (buffer0, " Back Save");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 1062 | sprintf (buffer1, "");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 1063 | sprintf (buffer2, "");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 1064 | sprintf (buffer3, ">>%s", fsmD[state].title);//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 1065 | sprintf (buffer4, "");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 1066 | sprintf (buffer5, "");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 1067 | |
amiraseman | 23:a0ce9fa869c2 | 1068 | |
amiraseman | 23:a0ce9fa869c2 | 1069 | unitFlag = fsmD[state].output; |
amiraseman | 23:a0ce9fa869c2 | 1070 | |
amiraseman | 23:a0ce9fa869c2 | 1071 | display(); |
amiraseman | 23:a0ce9fa869c2 | 1072 | |
amiraseman | 23:a0ce9fa869c2 | 1073 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 1074 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1075 | state = 0; |
amiraseman | 23:a0ce9fa869c2 | 1076 | break; |
amiraseman | 23:a0ce9fa869c2 | 1077 | } |
amiraseman | 23:a0ce9fa869c2 | 1078 | |
amiraseman | 23:a0ce9fa869c2 | 1079 | //Navigates to the next5 state of the fsm when one of the buttons is pressed |
amiraseman | 23:a0ce9fa869c2 | 1080 | if (button1Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 1081 | button1Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1082 | state=fsmD[state].nextState[1]; |
amiraseman | 23:a0ce9fa869c2 | 1083 | } |
amiraseman | 23:a0ce9fa869c2 | 1084 | if (button2Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 1085 | button2Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1086 | state=fsmD[state].nextState[0]; |
amiraseman | 23:a0ce9fa869c2 | 1087 | } |
amiraseman | 23:a0ce9fa869c2 | 1088 | } |
amiraseman | 23:a0ce9fa869c2 | 1089 | } |
amiraseman | 23:a0ce9fa869c2 | 1090 | |
amiraseman | 23:a0ce9fa869c2 | 1091 | |
amiraseman | 23:a0ce9fa869c2 | 1092 | |
amiraseman | 23:a0ce9fa869c2 | 1093 | |
amiraseman | 23:a0ce9fa869c2 | 1094 | |
amiraseman | 23:a0ce9fa869c2 | 1095 | |
amiraseman | 23:a0ce9fa869c2 | 1096 | /// Used by the user to set the current date and time |
amiraseman | 23:a0ce9fa869c2 | 1097 | void timeDateSetting() |
amiraseman | 23:a0ce9fa869c2 | 1098 | { |
amiraseman | 23:a0ce9fa869c2 | 1099 | |
amiraseman | 23:a0ce9fa869c2 | 1100 | state = 0; |
amiraseman | 23:a0ce9fa869c2 | 1101 | while (1) { |
amiraseman | 23:a0ce9fa869c2 | 1102 | thresholdCheck(); |
amiraseman | 23:a0ce9fa869c2 | 1103 | alarmClockCheck(); |
amiraseman | 23:a0ce9fa869c2 | 1104 | powerSaverCheck(); |
amiraseman | 23:a0ce9fa869c2 | 1105 | updateTime(); |
amiraseman | 23:a0ce9fa869c2 | 1106 | loggerData(); |
amiraseman | 23:a0ce9fa869c2 | 1107 | |
amiraseman | 23:a0ce9fa869c2 | 1108 | sprintf (buffer0, " Back Save");//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 1109 | sprintf (buffer1, ""); |
amiraseman | 23:a0ce9fa869c2 | 1110 | sprintf (buffer2, ""); |
amiraseman | 23:a0ce9fa869c2 | 1111 | sprintf (buffer3, ">>%s %d", fsmC[state].title,fsmC[state].output);//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 1112 | sprintf (buffer4, ""); |
amiraseman | 23:a0ce9fa869c2 | 1113 | sprintf (buffer5, ""); |
amiraseman | 23:a0ce9fa869c2 | 1114 | |
amiraseman | 23:a0ce9fa869c2 | 1115 | |
amiraseman | 23:a0ce9fa869c2 | 1116 | display(); |
amiraseman | 23:a0ce9fa869c2 | 1117 | |
amiraseman | 23:a0ce9fa869c2 | 1118 | |
amiraseman | 23:a0ce9fa869c2 | 1119 | |
amiraseman | 23:a0ce9fa869c2 | 1120 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 1121 | button4Flag= 0; |
amiraseman | 23:a0ce9fa869c2 | 1122 | fsmC[state].output ++; |
amiraseman | 23:a0ce9fa869c2 | 1123 | calculateUNIXTime(); |
amiraseman | 23:a0ce9fa869c2 | 1124 | set_time(UNIXdate); // initialise time from the calculated UNIX time entered by the user |
amiraseman | 23:a0ce9fa869c2 | 1125 | } |
amiraseman | 23:a0ce9fa869c2 | 1126 | |
amiraseman | 23:a0ce9fa869c2 | 1127 | |
amiraseman | 23:a0ce9fa869c2 | 1128 | //checks the limits of the values |
amiraseman | 23:a0ce9fa869c2 | 1129 | if (fsmC[0].output >23) { //hour |
amiraseman | 23:a0ce9fa869c2 | 1130 | fsmC[0].output = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 1131 | } |
amiraseman | 23:a0ce9fa869c2 | 1132 | if (fsmC[1].output>59) { // minute |
amiraseman | 23:a0ce9fa869c2 | 1133 | fsmC[1].output = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 1134 | } |
amiraseman | 23:a0ce9fa869c2 | 1135 | if (fsmC[3].output>11) { //month |
amiraseman | 23:a0ce9fa869c2 | 1136 | fsmC[3].output = 0 ; |
amiraseman | 23:a0ce9fa869c2 | 1137 | } |
amiraseman | 23:a0ce9fa869c2 | 1138 | if (fsmC[2].output>31) { //day |
amiraseman | 23:a0ce9fa869c2 | 1139 | fsmC[2].output = 1 ; |
amiraseman | 23:a0ce9fa869c2 | 1140 | } |
amiraseman | 23:a0ce9fa869c2 | 1141 | if (fsmC[4].output>2030) { //year |
amiraseman | 23:a0ce9fa869c2 | 1142 | fsmC[4].output = 2015 ; |
amiraseman | 23:a0ce9fa869c2 | 1143 | } |
amiraseman | 23:a0ce9fa869c2 | 1144 | |
amiraseman | 23:a0ce9fa869c2 | 1145 | |
amiraseman | 23:a0ce9fa869c2 | 1146 | |
amiraseman | 23:a0ce9fa869c2 | 1147 | if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 1148 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1149 | state = 0; |
amiraseman | 23:a0ce9fa869c2 | 1150 | break; |
amiraseman | 23:a0ce9fa869c2 | 1151 | } |
amiraseman | 23:a0ce9fa869c2 | 1152 | //Navigates to the next5 state of the fsm when one of the buttons is pressed |
amiraseman | 23:a0ce9fa869c2 | 1153 | if (button1Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 1154 | button1Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1155 | state=fsmC[state].nextState[1]; |
amiraseman | 23:a0ce9fa869c2 | 1156 | } |
amiraseman | 23:a0ce9fa869c2 | 1157 | if (button2Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 1158 | button2Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1159 | state=fsmC[state].nextState[0]; |
amiraseman | 23:a0ce9fa869c2 | 1160 | } |
amiraseman | 23:a0ce9fa869c2 | 1161 | } |
amiraseman | 23:a0ce9fa869c2 | 1162 | } |
amiraseman | 23:a0ce9fa869c2 | 1163 | |
amiraseman | 23:a0ce9fa869c2 | 1164 | |
amiraseman | 23:a0ce9fa869c2 | 1165 | /// The settings menu allows user to change the settings |
amiraseman | 23:a0ce9fa869c2 | 1166 | void settingsMenu() |
amiraseman | 23:a0ce9fa869c2 | 1167 | { |
amiraseman | 23:a0ce9fa869c2 | 1168 | state = 0; |
amiraseman | 23:a0ce9fa869c2 | 1169 | while(button3Flag==0) { |
amiraseman | 23:a0ce9fa869c2 | 1170 | |
amiraseman | 23:a0ce9fa869c2 | 1171 | powerSaverCheck(); |
amiraseman | 23:a0ce9fa869c2 | 1172 | updateTime(); |
amiraseman | 23:a0ce9fa869c2 | 1173 | loggerData(); |
amiraseman | 23:a0ce9fa869c2 | 1174 | strncpy(buffer0, bufferTime, 14); |
amiraseman | 23:a0ce9fa869c2 | 1175 | int dispaly = sprintf (buffer1, "Settings"); |
amiraseman | 23:a0ce9fa869c2 | 1176 | int dispaly1 = sprintf (buffer2, ""); |
amiraseman | 23:a0ce9fa869c2 | 1177 | int dispaly2 = sprintf (buffer3, ">>%s", fsmB[state].title);//convert integer to buffer str |
amiraseman | 23:a0ce9fa869c2 | 1178 | int dispaly3 = sprintf (buffer4, ""); |
amiraseman | 23:a0ce9fa869c2 | 1179 | int dispaly4 = sprintf (buffer5, ""); |
amiraseman | 23:a0ce9fa869c2 | 1180 | |
amiraseman | 23:a0ce9fa869c2 | 1181 | display(); |
amiraseman | 23:a0ce9fa869c2 | 1182 | |
amiraseman | 23:a0ce9fa869c2 | 1183 | |
amiraseman | 23:a0ce9fa869c2 | 1184 | switch (state) { |
amiraseman | 23:a0ce9fa869c2 | 1185 | case 0: |
amiraseman | 23:a0ce9fa869c2 | 1186 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 1187 | button4Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1188 | timeDateSetting(); |
amiraseman | 23:a0ce9fa869c2 | 1189 | } |
amiraseman | 23:a0ce9fa869c2 | 1190 | |
amiraseman | 23:a0ce9fa869c2 | 1191 | |
amiraseman | 23:a0ce9fa869c2 | 1192 | break; |
amiraseman | 23:a0ce9fa869c2 | 1193 | case 1: |
amiraseman | 23:a0ce9fa869c2 | 1194 | |
amiraseman | 23:a0ce9fa869c2 | 1195 | |
amiraseman | 23:a0ce9fa869c2 | 1196 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 1197 | button4Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1198 | unitsSetting(); |
amiraseman | 23:a0ce9fa869c2 | 1199 | } |
amiraseman | 23:a0ce9fa869c2 | 1200 | |
amiraseman | 23:a0ce9fa869c2 | 1201 | break; |
amiraseman | 23:a0ce9fa869c2 | 1202 | case 2: |
amiraseman | 23:a0ce9fa869c2 | 1203 | |
amiraseman | 23:a0ce9fa869c2 | 1204 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 1205 | button4Flag = 0; |
amiraseman | 23:a0ce9fa869c2 | 1206 | powerSaverSetting(); |
amiraseman | 23:a0ce9fa869c2 | 1207 | } |
amiraseman | 23:a0ce9fa869c2 | 1208 | break; |
amiraseman | 23:a0ce9fa869c2 | 1209 | case 3: |
amiraseman | 23:a0ce9fa869c2 | 1210 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 1211 | button4Flag = 0; |
amiraseman | 23:a0ce9fa869c2 | 1212 | dataLoggerSetting(); |
amiraseman | 23:a0ce9fa869c2 | 1213 | } |
amiraseman | 23:a0ce9fa869c2 | 1214 | break; |
amiraseman | 23:a0ce9fa869c2 | 1215 | default: |
amiraseman | 23:a0ce9fa869c2 | 1216 | break; |
amiraseman | 23:a0ce9fa869c2 | 1217 | } |
amiraseman | 23:a0ce9fa869c2 | 1218 | // if (button3Flag) { |
amiraseman | 23:a0ce9fa869c2 | 1219 | // button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1220 | // state = 0; |
amiraseman | 23:a0ce9fa869c2 | 1221 | // break; |
amiraseman | 23:a0ce9fa869c2 | 1222 | // } |
amiraseman | 23:a0ce9fa869c2 | 1223 | //Navigates to the next5 state of the fsm when one of the buttons is pressed |
amiraseman | 23:a0ce9fa869c2 | 1224 | if (button1Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 1225 | button1Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1226 | state=fsmB[state].nextState[1]; |
amiraseman | 23:a0ce9fa869c2 | 1227 | } |
amiraseman | 23:a0ce9fa869c2 | 1228 | if (button2Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 1229 | button2Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1230 | state=fsmB[state].nextState[0]; |
amiraseman | 23:a0ce9fa869c2 | 1231 | } |
amiraseman | 23:a0ce9fa869c2 | 1232 | } |
amiraseman | 23:a0ce9fa869c2 | 1233 | } |
amiraseman | 23:a0ce9fa869c2 | 1234 | |
amiraseman | 23:a0ce9fa869c2 | 1235 | /// The first main menu |
amiraseman | 23:a0ce9fa869c2 | 1236 | void startMenu() |
amiraseman | 23:a0ce9fa869c2 | 1237 | { |
amiraseman | 23:a0ce9fa869c2 | 1238 | while(1) { |
amiraseman | 23:a0ce9fa869c2 | 1239 | updateTime(); |
amiraseman | 23:a0ce9fa869c2 | 1240 | thresholdCheck(); |
amiraseman | 23:a0ce9fa869c2 | 1241 | alarmClockCheck(); |
amiraseman | 23:a0ce9fa869c2 | 1242 | powerSaverCheck(); |
amiraseman | 23:a0ce9fa869c2 | 1243 | loggerData(); |
amiraseman | 23:a0ce9fa869c2 | 1244 | button3Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1245 | strncpy(buffer0, bufferTime, 14); |
amiraseman | 23:a0ce9fa869c2 | 1246 | sprintf (buffer1, "Main Menu"); |
amiraseman | 23:a0ce9fa869c2 | 1247 | sprintf (buffer2, ""); |
amiraseman | 23:a0ce9fa869c2 | 1248 | sprintf (buffer3, ">>%s", fsmA[state].title); |
amiraseman | 23:a0ce9fa869c2 | 1249 | sprintf (buffer4, ""); |
amiraseman | 23:a0ce9fa869c2 | 1250 | sprintf (buffer5, ""); |
amiraseman | 23:a0ce9fa869c2 | 1251 | display(); |
amiraseman | 23:a0ce9fa869c2 | 1252 | |
amiraseman | 23:a0ce9fa869c2 | 1253 | |
amiraseman | 23:a0ce9fa869c2 | 1254 | switch (state) { |
amiraseman | 23:a0ce9fa869c2 | 1255 | case 0: |
amiraseman | 23:a0ce9fa869c2 | 1256 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 1257 | button4Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1258 | liveData(); |
amiraseman | 23:a0ce9fa869c2 | 1259 | } |
amiraseman | 23:a0ce9fa869c2 | 1260 | break; |
amiraseman | 23:a0ce9fa869c2 | 1261 | case 1: |
amiraseman | 23:a0ce9fa869c2 | 1262 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 1263 | button4Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1264 | dataLogger(); |
amiraseman | 23:a0ce9fa869c2 | 1265 | } |
amiraseman | 23:a0ce9fa869c2 | 1266 | break; |
amiraseman | 23:a0ce9fa869c2 | 1267 | case 2: |
amiraseman | 23:a0ce9fa869c2 | 1268 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 1269 | button4Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1270 | alarmsMenu(); |
amiraseman | 23:a0ce9fa869c2 | 1271 | } |
amiraseman | 23:a0ce9fa869c2 | 1272 | break; |
amiraseman | 23:a0ce9fa869c2 | 1273 | case 3: |
amiraseman | 23:a0ce9fa869c2 | 1274 | if (button4Flag) { |
amiraseman | 23:a0ce9fa869c2 | 1275 | button4Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1276 | settingsMenu(); |
amiraseman | 23:a0ce9fa869c2 | 1277 | } |
amiraseman | 23:a0ce9fa869c2 | 1278 | break; |
amiraseman | 23:a0ce9fa869c2 | 1279 | default: |
amiraseman | 23:a0ce9fa869c2 | 1280 | break; |
amiraseman | 23:a0ce9fa869c2 | 1281 | } |
amiraseman | 23:a0ce9fa869c2 | 1282 | |
amiraseman | 23:a0ce9fa869c2 | 1283 | |
amiraseman | 23:a0ce9fa869c2 | 1284 | //Navigates to the next5 state of the fsm when one of the buttons is pressed |
amiraseman | 23:a0ce9fa869c2 | 1285 | if (button1Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 1286 | button1Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1287 | state=fsmA[state].nextState[1]; |
amiraseman | 23:a0ce9fa869c2 | 1288 | } |
amiraseman | 23:a0ce9fa869c2 | 1289 | if (button2Flag == 1) { |
amiraseman | 23:a0ce9fa869c2 | 1290 | button2Flag=0; |
amiraseman | 23:a0ce9fa869c2 | 1291 | state=fsmA[state].nextState[0]; |
amiraseman | 23:a0ce9fa869c2 | 1292 | } |
amiraseman | 23:a0ce9fa869c2 | 1293 | |
amiraseman | 23:a0ce9fa869c2 | 1294 | } |
amiraseman | 23:a0ce9fa869c2 | 1295 | } |
amiraseman | 23:a0ce9fa869c2 | 1296 | |
amiraseman | 23:a0ce9fa869c2 | 1297 | |
amiraseman | 23:a0ce9fa869c2 | 1298 | |
amiraseman | 23:a0ce9fa869c2 | 1299 | |
amiraseman | 23:a0ce9fa869c2 | 1300 | int main() |
amiraseman | 23:a0ce9fa869c2 | 1301 | { |
amiraseman | 23:a0ce9fa869c2 | 1302 | |
amiraseman | 23:a0ce9fa869c2 | 1303 | /// Initialise barometer |
amiraseman | 23:a0ce9fa869c2 | 1304 | bmp180.init(); |
amiraseman | 23:a0ce9fa869c2 | 1305 | /// Initialise the lcd |
amiraseman | 23:a0ce9fa869c2 | 1306 | lcd.init(); |
amiraseman | 23:a0ce9fa869c2 | 1307 | /// call the appropriate ISR on rising edge (when any of the buttons are pressed) |
amiraseman | 23:a0ce9fa869c2 | 1308 | button1.rise(&button1Pressed); |
amiraseman | 23:a0ce9fa869c2 | 1309 | button2.rise(&button2Pressed); |
amiraseman | 23:a0ce9fa869c2 | 1310 | button3.rise(&button3Pressed); |
amiraseman | 23:a0ce9fa869c2 | 1311 | button4.rise(&button4Pressed); |
amiraseman | 23:a0ce9fa869c2 | 1312 | debounce.start(); |
amiraseman | 23:a0ce9fa869c2 | 1313 | /// Set frequency at 40kHz for the leds |
amiraseman | 23:a0ce9fa869c2 | 1314 | redLED.period_us(25); |
amiraseman | 23:a0ce9fa869c2 | 1315 | yellowLED.period_us(25); |
amiraseman | 23:a0ce9fa869c2 | 1316 | greenLED.period_us(25); |
amiraseman | 23:a0ce9fa869c2 | 1317 | state = 0; |
amiraseman | 23:a0ce9fa869c2 | 1318 | startMenu(); |
amiraseman | 23:a0ce9fa869c2 | 1319 | } |
amiraseman | 23:a0ce9fa869c2 | 1320 | |
amiraseman | 23:a0ce9fa869c2 | 1321 | |
amiraseman | 23:a0ce9fa869c2 | 1322 |