Keiarash Zoughi's Multi-Sensor for weather detection.

Dependencies:   BMP180 N5110 mbed

Committer:
el13kz
Date:
Wed May 06 15:39:53 2015 +0000
Revision:
3:2b49a80bd90b
Parent:
2:635c58eb70fb
switch loop way

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el13kz 0:771a5148e8e7 1 #include "mbed.h"
el13kz 0:771a5148e8e7 2 #include "N5110.h"
el13kz 0:771a5148e8e7 3 #include "BMP180.h"
el13kz 0:771a5148e8e7 4
el13kz 0:771a5148e8e7 5 #define PI 3.14159265359
el13kz 2:635c58eb70fb 6
el13kz 2:635c58eb70fb 7 //LCD inputs
el13kz 2:635c58eb70fb 8 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
el13kz 2:635c58eb70fb 9
el13kz 2:635c58eb70fb 10 //serial port for debugging
el13kz 2:635c58eb70fb 11 Serial serial(USBTX,USBRX);
el13kz 2:635c58eb70fb 12
el13kz 2:635c58eb70fb 13 //LED outputs
el13kz 0:771a5148e8e7 14 BusOut leds(LED4,LED3,LED2,LED1); // LEDs for display
el13kz 0:771a5148e8e7 15
el13kz 2:635c58eb70fb 16
el13kz 2:635c58eb70fb 17 //select button
el13kz 0:771a5148e8e7 18 DigitalIn pushbutton1(p19);
el13kz 2:635c58eb70fb 19
el13kz 2:635c58eb70fb 20
el13kz 2:635c58eb70fb 21 //barometer sensor input
el13kz 0:771a5148e8e7 22 BMP180 bmp180(p28,p27); // SDA, SCL
el13kz 2:635c58eb70fb 23
el13kz 2:635c58eb70fb 24 //warning LED
el13kz 2:635c58eb70fb 25 PwmOut Redled(p24);
el13kz 2:635c58eb70fb 26
el13kz 2:635c58eb70fb 27 //globals
el13kz 0:771a5148e8e7 28 int selectedOption = 0;
el13kz 0:771a5148e8e7 29 int buttonPressed = 0;
el13kz 0:771a5148e8e7 30
el13kz 2:635c58eb70fb 31 //boundary conditions - screen
el13kz 2:635c58eb70fb 32 int cells[84][48];
el13kz 2:635c58eb70fb 33
el13kz 2:635c58eb70fb 34 //shows pressure reading and temperature
el13kz 0:771a5148e8e7 35 void measurement()
el13kz 0:771a5148e8e7 36 {
el13kz 0:771a5148e8e7 37 Measurement measurement; // measurement structure declared in BMP180 class
el13kz 0:771a5148e8e7 38 while(1) {
el13kz 0:771a5148e8e7 39
el13kz 0:771a5148e8e7 40 // read values (T in Celsius and P in mb) and print over serial port
el13kz 0:771a5148e8e7 41 measurement = bmp180.readValues();
el13kz 0:771a5148e8e7 42 serial.printf("T = %.2f C P = %.2f mb\n",measurement.temperature,measurement.pressure);
el13kz 0:771a5148e8e7 43
el13kz 0:771a5148e8e7 44 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
el13kz 0:771a5148e8e7 45 // so can display a string of a maximum 14 characters in length
el13kz 0:771a5148e8e7 46 // or create formatted strings - ensure they aren't more than 14 characters long
el13kz 0:771a5148e8e7 47 int temperature = measurement.temperature;
el13kz 0:771a5148e8e7 48 int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
el13kz 0:771a5148e8e7 49 // it is important the format specifier ensures the length will fit in the buffer
el13kz 0:771a5148e8e7 50 if (length <= 14) // if string will fit on display
el13kz 0:771a5148e8e7 51 lcd.printString(buffer,0,1); // display on screen
el13kz 0:771a5148e8e7 52 //
el13kz 0:771a5148e8e7 53 float pressure = measurement.pressure; // same idea with floats
el13kz 0:771a5148e8e7 54 length = sprintf(buffer,"P = %.2f mb",pressure);
el13kz 0:771a5148e8e7 55 if (length <= 14)
el13kz 0:771a5148e8e7 56 lcd.printString(buffer,0,2);
el13kz 0:771a5148e8e7 57 }
el13kz 0:771a5148e8e7 58 }
el13kz 0:771a5148e8e7 59
el13kz 0:771a5148e8e7 60
el13kz 2:635c58eb70fb 61 //pushbutton - 0
el13kz 0:771a5148e8e7 62
el13kz 2:635c58eb70fb 63 //plots graph of temperature point by point
el13kz 0:771a5148e8e7 64 void graph()
el13kz 0:771a5148e8e7 65 {
el13kz 0:771a5148e8e7 66 Measurement measurement; // measurement structure declared in BMP180 class
el13kz 0:771a5148e8e7 67 int i = 0;
el13kz 0:771a5148e8e7 68 float graph[84]= {0};
el13kz 0:771a5148e8e7 69 while(1) {
el13kz 0:771a5148e8e7 70
el13kz 0:771a5148e8e7 71 // read values (T in Celsius and P in mb) and print over serial port
el13kz 0:771a5148e8e7 72 measurement = bmp180.readValues();
el13kz 0:771a5148e8e7 73 //serial.printf("T = %.2f C P = %.2f mb\n",measurement.temperature,measurement.pressure);
el13kz 0:771a5148e8e7 74
el13kz 0:771a5148e8e7 75 // so can display a string of a maximum 14 characters in length
el13kz 0:771a5148e8e7 76 // or create formatted strings - ensure they aren't more than 14 characters long
el13kz 0:771a5148e8e7 77 int temperature = measurement.temperature;
el13kz 0:771a5148e8e7 78 // int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
el13kz 0:771a5148e8e7 79 // it is important the format specifier ensures the length will fit in the buffer
el13kz 0:771a5148e8e7 80 // if (length <= 14) // if string will fit on display
el13kz 0:771a5148e8e7 81 // lcd.printString(buffer,0,1); // display on screen
el13kz 0:771a5148e8e7 82 //
el13kz 0:771a5148e8e7 83 // float pressure = measurement.pressure; // same idea with floats
el13kz 0:771a5148e8e7 84 // length = sprintf(buffer,"P = %.2f mb",pressure);
el13kz 0:771a5148e8e7 85 // if (length <= 14)
el13kz 0:771a5148e8e7 86 // lcd.printString(buffer,0,2);
el13kz 0:771a5148e8e7 87
el13kz 0:771a5148e8e7 88 graph[i]= temperature ;
el13kz 0:771a5148e8e7 89 lcd.plotArray(graph);
el13kz 0:771a5148e8e7 90 wait(0.1); // short delay until next reading
el13kz 0:771a5148e8e7 91 i++;
el13kz 0:771a5148e8e7 92 if (i>83) {
el13kz 0:771a5148e8e7 93 i=0;
el13kz 0:771a5148e8e7 94 lcd.clear();
el13kz 0:771a5148e8e7 95 }
el13kz 0:771a5148e8e7 96 }
el13kz 0:771a5148e8e7 97 }
el13kz 0:771a5148e8e7 98
el13kz 2:635c58eb70fb 99 void settingScreen()
el13kz 2:635c58eb70fb 100 {
el13kz 0:771a5148e8e7 101 selectedOption = 0;
el13kz 0:771a5148e8e7 102
el13kz 2:635c58eb70fb 103 while(pushbutton1) {
el13kz 0:771a5148e8e7 104
el13kz 2:635c58eb70fb 105 lcd.drawRect(70,6,10,10,1);
el13kz 2:635c58eb70fb 106 lcd.drawRect(70,21,10,10,0);
el13kz 0:771a5148e8e7 107
el13kz 2:635c58eb70fb 108 lcd.printString("clock",1,1);
el13kz 2:635c58eb70fb 109 lcd.printString(">",62,1);
el13kz 2:635c58eb70fb 110 selectedOption = 1;
el13kz 0:771a5148e8e7 111
el13kz 0:771a5148e8e7 112 wait(2);
el13kz 0:771a5148e8e7 113 lcd.clear();
el13kz 0:771a5148e8e7 114
el13kz 2:635c58eb70fb 115 lcd.drawRect(70,6,10,10,0);
el13kz 2:635c58eb70fb 116 lcd.drawRect(70,21,10,10,1);
el13kz 2:635c58eb70fb 117 lcd.printString("go back",1,3);
el13kz 2:635c58eb70fb 118 lcd.printString(">",62,3);
el13kz 2:635c58eb70fb 119 selectedOption = 2;
el13kz 0:771a5148e8e7 120
el13kz 0:771a5148e8e7 121 wait(2);
el13kz 0:771a5148e8e7 122 lcd.clear();
el13kz 2:635c58eb70fb 123
el13kz 2:635c58eb70fb 124 }
el13kz 2:635c58eb70fb 125
el13kz 2:635c58eb70fb 126 if(selectedOption == 1) {
el13kz 0:771a5148e8e7 127
el13kz 2:635c58eb70fb 128 measurement();
el13kz 2:635c58eb70fb 129 }
el13kz 2:635c58eb70fb 130
el13kz 2:635c58eb70fb 131 if(selectedOption == 2) {
el13kz 2:635c58eb70fb 132
el13kz 2:635c58eb70fb 133 graph();
el13kz 2:635c58eb70fb 134
el13kz 2:635c58eb70fb 135 }
el13kz 0:771a5148e8e7 136 }
el13kz 0:771a5148e8e7 137
el13kz 0:771a5148e8e7 138 void introScreen()
el13kz 0:771a5148e8e7 139 {
el13kz 0:771a5148e8e7 140
el13kz 0:771a5148e8e7 141 lcd.printString("Welcome to",15,1);
el13kz 0:771a5148e8e7 142 lcd.printString("Keiarash's",20,2);
el13kz 0:771a5148e8e7 143 lcd.printString("Multi-Sensor",6,3);
el13kz 2:635c58eb70fb 144 Redled= 1.0;
el13kz 0:771a5148e8e7 145
el13kz 0:771a5148e8e7 146
el13kz 0:771a5148e8e7 147 }
el13kz 0:771a5148e8e7 148
el13kz 0:771a5148e8e7 149 void mainScreen()
el13kz 0:771a5148e8e7 150 {
el13kz 0:771a5148e8e7 151 selectedOption = 0;
el13kz 0:771a5148e8e7 152
el13kz 2:635c58eb70fb 153 while(pushbutton1) {//initialse pushbutton as 0
el13kz 2:635c58eb70fb 154
el13kz 2:635c58eb70fb 155 //check boxes-options to be selected
el13kz 2:635c58eb70fb 156 lcd.drawRect(70,6,10,10,1);//check box 1
el13kz 2:635c58eb70fb 157 lcd.drawRect(70,21,10,10,0);//check box 2
el13kz 2:635c58eb70fb 158 lcd.drawRect(70,37,10,10,0);//check box 3
el13kz 0:771a5148e8e7 159
el13kz 2:635c58eb70fb 160 //option titles-appropriate to check boxes
el13kz 2:635c58eb70fb 161 lcd.printString("measure",1,1);//select measure menu
el13kz 2:635c58eb70fb 162 lcd.printString("settings",1,3);//select settings menu
el13kz 2:635c58eb70fb 163 lcd.printString("graph",1,5);//select grpah plot
el13kz 2:635c58eb70fb 164 lcd.printString(">",62,1);//pointer
el13kz 0:771a5148e8e7 165 selectedOption = 1;
el13kz 0:771a5148e8e7 166
el13kz 0:771a5148e8e7 167 wait(2);
el13kz 0:771a5148e8e7 168 lcd.clear();
el13kz 0:771a5148e8e7 169
el13kz 0:771a5148e8e7 170 lcd.drawRect(70,6,10,10,0);
el13kz 0:771a5148e8e7 171 lcd.drawRect(70,21,10,10,1);
el13kz 0:771a5148e8e7 172 lcd.drawRect(70,37,10,10,0);
el13kz 0:771a5148e8e7 173
el13kz 0:771a5148e8e7 174
el13kz 0:771a5148e8e7 175 lcd.printString("measure",1,1);
el13kz 0:771a5148e8e7 176 lcd.printString("settings",1,3);
el13kz 0:771a5148e8e7 177 lcd.printString("graph",1,5);
el13kz 0:771a5148e8e7 178 lcd.printString(">",62,3);
el13kz 0:771a5148e8e7 179 selectedOption = 2;
el13kz 0:771a5148e8e7 180
el13kz 0:771a5148e8e7 181 wait(2);
el13kz 0:771a5148e8e7 182 lcd.clear();
el13kz 0:771a5148e8e7 183
el13kz 0:771a5148e8e7 184 lcd.drawRect(70,6,10,10,0);
el13kz 0:771a5148e8e7 185 lcd.drawRect(70,21,10,10,0);
el13kz 0:771a5148e8e7 186 lcd.drawRect(70,37,10,10,1);
el13kz 0:771a5148e8e7 187
el13kz 0:771a5148e8e7 188
el13kz 0:771a5148e8e7 189 lcd.printString("measure",1,1);
el13kz 0:771a5148e8e7 190 lcd.printString("settings",1,3);
el13kz 0:771a5148e8e7 191 lcd.printString("graph",1,5);
el13kz 0:771a5148e8e7 192 lcd.printString(">",62,5);
el13kz 0:771a5148e8e7 193 selectedOption = 3;
el13kz 0:771a5148e8e7 194
el13kz 0:771a5148e8e7 195 wait(2);
el13kz 0:771a5148e8e7 196 lcd.clear();
el13kz 0:771a5148e8e7 197
el13kz 0:771a5148e8e7 198
el13kz 0:771a5148e8e7 199 }
el13kz 2:635c58eb70fb 200
el13kz 3:2b49a80bd90b 201 switch(buttonPressed){
el13kz 3:2b49a80bd90b 202
el13kz 3:2b49a80bd90b 203 case 1:
el13kz 3:2b49a80bd90b 204 measurement();
el13kz 3:2b49a80bd90b 205 break;
el13kz 3:2b49a80bd90b 206
el13kz 3:2b49a80bd90b 207 case 2:
el13kz 3:2b49a80bd90b 208
el13kz 3:2b49a80bd90b 209 settingScreen();
el13kz 3:2b49a80bd90b 210 break;
el13kz 3:2b49a80bd90b 211
el13kz 3:2b49a80bd90b 212 case 3:
el13kz 3:2b49a80bd90b 213
el13kz 3:2b49a80bd90b 214 graph();
el13kz 3:2b49a80bd90b 215 break;
el13kz 2:635c58eb70fb 216
el13kz 2:635c58eb70fb 217
el13kz 3:2b49a80bd90b 218 }
el13kz 3:2b49a80bd90b 219 }
el13kz 2:635c58eb70fb 220
el13kz 0:771a5148e8e7 221
el13kz 0:771a5148e8e7 222
el13kz 0:771a5148e8e7 223
el13kz 0:771a5148e8e7 224
el13kz 0:771a5148e8e7 225
el13kz 0:771a5148e8e7 226
el13kz 0:771a5148e8e7 227
el13kz 2:635c58eb70fb 228 void check(){
el13kz 2:635c58eb70fb 229
el13kz 2:635c58eb70fb 230 Redled.period(0.02); // set PWM period to 20ms
el13kz 0:771a5148e8e7 231
el13kz 2:635c58eb70fb 232 Measurement measurement; // measurement structure declared in BMP180 class
el13kz 2:635c58eb70fb 233
el13kz 0:771a5148e8e7 234
el13kz 2:635c58eb70fb 235 // read values (T in Celsius and P in mb) and print over serial port
el13kz 2:635c58eb70fb 236 measurement = bmp180.readValues();
el13kz 2:635c58eb70fb 237 serial.printf("T = %.2f C P = %.2f mb\n",measurement.temperature,measurement.pressure);
el13kz 0:771a5148e8e7 238
el13kz 2:635c58eb70fb 239 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
el13kz 2:635c58eb70fb 240 // so can display a string of a maximum 14 characters in length
el13kz 2:635c58eb70fb 241 // or create formatted strings - ensure they aren't more than 14 characters long
el13kz 2:635c58eb70fb 242 int temperature = measurement.temperature;
el13kz 2:635c58eb70fb 243 int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
el13kz 2:635c58eb70fb 244 // it is important the format specifier ensures the length will fit in the buffer
el13kz 2:635c58eb70fb 245 if (length <= 14) // if string will fit on display
el13kz 2:635c58eb70fb 246 lcd.printString(buffer,0,1); // display on screen
el13kz 2:635c58eb70fb 247 //
el13kz 2:635c58eb70fb 248 float pressure = measurement.pressure; // same idea with floats
el13kz 2:635c58eb70fb 249 length = sprintf(buffer,"P = %.2f mb",pressure);
el13kz 2:635c58eb70fb 250 if (length <= 14)
el13kz 2:635c58eb70fb 251 lcd.printString(buffer,0,2);
el13kz 2:635c58eb70fb 252
el13kz 0:771a5148e8e7 253
el13kz 2:635c58eb70fb 254 if(measurement.temperature > 30 ){
el13kz 0:771a5148e8e7 255
el13kz 2:635c58eb70fb 256 lcd.printString("temperature high!",1,1);
el13kz 0:771a5148e8e7 257
el13kz 2:635c58eb70fb 258 Redled=1.0;
el13kz 2:635c58eb70fb 259
el13kz 2:635c58eb70fb 260 wait(3); }
el13kz 0:771a5148e8e7 261
el13kz 2:635c58eb70fb 262 if(measurement.temperature < 3){
el13kz 2:635c58eb70fb 263
el13kz 2:635c58eb70fb 264 lcd.printString("temperature low!",1,1);
el13kz 2:635c58eb70fb 265
el13kz 2:635c58eb70fb 266 Redled= 1.0;
el13kz 0:771a5148e8e7 267
el13kz 2:635c58eb70fb 268 wait(3); }
el13kz 0:771a5148e8e7 269
el13kz 2:635c58eb70fb 270 else { lcd.printString("temperature ok!",1,1);
el13kz 2:635c58eb70fb 271 wait(3);
el13kz 2:635c58eb70fb 272 }
el13kz 2:635c58eb70fb 273 }
el13kz 2:635c58eb70fb 274
el13kz 2:635c58eb70fb 275
el13kz 2:635c58eb70fb 276 void altitude(){ // creating a new reading from the current ones, this appoximates the altitude in metres.
el13kz 0:771a5148e8e7 277
el13kz 0:771a5148e8e7 278
el13kz 0:771a5148e8e7 279
el13kz 2:635c58eb70fb 280 Measurement measurement; // measurement structure declared in BMP180 class
el13kz 2:635c58eb70fb 281
el13kz 2:635c58eb70fb 282 while(1){
el13kz 2:635c58eb70fb 283 // read values (T in Celsius and P in mb) and print over serial port
el13kz 2:635c58eb70fb 284 measurement = bmp180.readValues();
el13kz 2:635c58eb70fb 285
el13kz 2:635c58eb70fb 286 int temperature = measurement.temperature;
el13kz 2:635c58eb70fb 287 int pressure = measurement.pressure; // same idea with floats
el13kz 1:dea8267d47d6 288
el13kz 2:635c58eb70fb 289 float altitude = -(log(pressure/1013.25)*1.38e-23*temperature)/(9.81*28.95);
el13kz 2:635c58eb70fb 290 serial.printf("A = %.2f metres",altitude);
el13kz 2:635c58eb70fb 291 }
el13kz 2:635c58eb70fb 292
el13kz 2:635c58eb70fb 293 }
el13kz 2:635c58eb70fb 294
el13kz 2:635c58eb70fb 295 //main loop
el13kz 0:771a5148e8e7 296 int main()
el13kz 0:771a5148e8e7 297 {
el13kz 2:635c58eb70fb 298 pushbutton1.mode(PullUp);//initial value 0
el13kz 0:771a5148e8e7 299 // initiliase barometer
el13kz 0:771a5148e8e7 300 bmp180.init();
el13kz 2:635c58eb70fb 301
el13kz 0:771a5148e8e7 302 lcd.init();
el13kz 0:771a5148e8e7 303 introScreen();
el13kz 0:771a5148e8e7 304 wait(3);
el13kz 2:635c58eb70fb 305 Redled =0;
el13kz 0:771a5148e8e7 306 lcd.clear();
el13kz 0:771a5148e8e7 307 mainScreen();
el13kz 0:771a5148e8e7 308
el13kz 2:635c58eb70fb 309 while(1) {
el13kz 2:635c58eb70fb 310 }
el13kz 2:635c58eb70fb 311 }