Orla Gilson / Mbed 2 deprecated WeatherLogger

Dependencies:   BMP180 N5110 mbed

Committer:
orlagilson
Date:
Thu Apr 30 17:14:08 2015 +0000
Revision:
4:0302731434a5
Parent:
3:c9162dc9ba24
Child:
5:3246c4b8362c
Fifth commit; Graph ticker increased so that graphs are updated every 2 seconds. Noise on graphs.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
orlagilson 0:2fd314d76f37 1 #include "mbed.h"
orlagilson 0:2fd314d76f37 2 #include "BMP180.h"
orlagilson 0:2fd314d76f37 3 #include "N5110.h"
orlagilson 0:2fd314d76f37 4
orlagilson 0:2fd314d76f37 5 // VCC,SCE,RST,D/C,MOSI,SCLK,LED
orlagilson 0:2fd314d76f37 6 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
orlagilson 0:2fd314d76f37 7 // Can also power (VCC) directly from VOUT (3.3 V) -
orlagilson 0:2fd314d76f37 8 // Can give better performance due to current limitation from GPIO pin
orlagilson 0:2fd314d76f37 9
orlagilson 0:2fd314d76f37 10 BMP180 bmp180(p28,p27); // SDA, SCL
orlagilson 0:2fd314d76f37 11
orlagilson 4:0302731434a5 12 InterruptIn buttonleft(p15);
orlagilson 0:2fd314d76f37 13 AnalogIn BUT2(p16);
orlagilson 0:2fd314d76f37 14 AnalogIn POT(p20);
orlagilson 0:2fd314d76f37 15
orlagilson 0:2fd314d76f37 16 int nx=84;
orlagilson 0:2fd314d76f37 17 int ny=48;
orlagilson 0:2fd314d76f37 18 int i,j;
orlagilson 2:6b564e388747 19 float temperature;
orlagilson 2:6b564e388747 20 float pressure;
orlagilson 0:2fd314d76f37 21
orlagilson 4:0302731434a5 22 int buttonFlag=0;
orlagilson 4:0302731434a5 23
orlagilson 0:2fd314d76f37 24 void clearCells();
orlagilson 4:0302731434a5 25 void menu();
orlagilson 0:2fd314d76f37 26
orlagilson 3:c9162dc9ba24 27 Timeout callT;
orlagilson 3:c9162dc9ba24 28 Timeout callP;
orlagilson 3:c9162dc9ba24 29
orlagilson 1:1accd82f4281 30 Ticker timerT;
orlagilson 1:1accd82f4281 31 Ticker timerP;
orlagilson 1:1accd82f4281 32
orlagilson 1:1accd82f4281 33 int timerTFlag = 0;
orlagilson 1:1accd82f4281 34 int timerPFlag = 0;
orlagilson 1:1accd82f4281 35
orlagilson 1:1accd82f4281 36 void timerTExpired(){
orlagilson 1:1accd82f4281 37 timerTFlag = 1;
orlagilson 1:1accd82f4281 38 }
orlagilson 1:1accd82f4281 39 void timerPExpired(){
orlagilson 1:1accd82f4281 40 timerPFlag = 1;
orlagilson 1:1accd82f4281 41 }
orlagilson 1:1accd82f4281 42
orlagilson 4:0302731434a5 43 void buttonPressed(){
orlagilson 4:0302731434a5 44 buttonFlag=1;
orlagilson 4:0302731434a5 45 }
orlagilson 4:0302731434a5 46
orlagilson 3:c9162dc9ba24 47 void callTemp(){
orlagilson 3:c9162dc9ba24 48 Measurement measurement;
orlagilson 3:c9162dc9ba24 49 clearCells();
orlagilson 3:c9162dc9ba24 50 lcd.printString("Menu",0,5);
orlagilson 3:c9162dc9ba24 51 char bufferT[14];
orlagilson 3:c9162dc9ba24 52 measurement = bmp180.readValues();
orlagilson 3:c9162dc9ba24 53 int length=sprintf(bufferT,"T = %.2f C",measurement.temperature);
orlagilson 3:c9162dc9ba24 54 if (length<=14){
orlagilson 3:c9162dc9ba24 55 lcd.printString(bufferT,10,2);
orlagilson 3:c9162dc9ba24 56 }
orlagilson 3:c9162dc9ba24 57 }
orlagilson 3:c9162dc9ba24 58
orlagilson 3:c9162dc9ba24 59 void callPress(){
orlagilson 3:c9162dc9ba24 60 Measurement measurement;
orlagilson 3:c9162dc9ba24 61 clearCells();
orlagilson 3:c9162dc9ba24 62 lcd.printString("Menu",0,5);
orlagilson 3:c9162dc9ba24 63 char bufferP[14];
orlagilson 3:c9162dc9ba24 64 measurement = bmp180.readValues();
orlagilson 3:c9162dc9ba24 65 int length=sprintf(bufferP,"P = %.2f mb",measurement.pressure);
orlagilson 3:c9162dc9ba24 66 if (length<=14){
orlagilson 3:c9162dc9ba24 67 lcd.printString(bufferP,0,2);
orlagilson 3:c9162dc9ba24 68 }
orlagilson 3:c9162dc9ba24 69 }
orlagilson 3:c9162dc9ba24 70
orlagilson 0:2fd314d76f37 71 void readTemp(){
orlagilson 3:c9162dc9ba24 72 callT.attach(&callTemp,0.1);
orlagilson 4:0302731434a5 73 timerT.attach(&timerTExpired,60);
orlagilson 0:2fd314d76f37 74 while(1){
orlagilson 1:1accd82f4281 75 if (timerTFlag){
orlagilson 1:1accd82f4281 76 timerTFlag=0;
orlagilson 2:6b564e388747 77 Measurement measurement;
orlagilson 1:1accd82f4281 78 clearCells();
orlagilson 3:c9162dc9ba24 79 lcd.printString("Menu",0,5);
orlagilson 1:1accd82f4281 80 char bufferT[14];
orlagilson 1:1accd82f4281 81 measurement = bmp180.readValues();
orlagilson 1:1accd82f4281 82 int length=sprintf(bufferT,"T = %.2f C",measurement.temperature);
orlagilson 1:1accd82f4281 83 if (length<=14){
orlagilson 1:1accd82f4281 84 lcd.printString(bufferT,10,2);
orlagilson 1:1accd82f4281 85 }
orlagilson 0:2fd314d76f37 86 }
orlagilson 4:0302731434a5 87 if (buttonFlag){
orlagilson 4:0302731434a5 88 buttonFlag=0;
orlagilson 4:0302731434a5 89 menu();
orlagilson 4:0302731434a5 90 }
orlagilson 0:2fd314d76f37 91 }
orlagilson 0:2fd314d76f37 92 }
orlagilson 0:2fd314d76f37 93
orlagilson 0:2fd314d76f37 94 void readPress(){
orlagilson 3:c9162dc9ba24 95 callP.attach(&callPress,0.1);
orlagilson 4:0302731434a5 96 timerP.attach(&timerPExpired,1800);
orlagilson 0:2fd314d76f37 97 while(1){
orlagilson 1:1accd82f4281 98 if (timerPFlag){
orlagilson 1:1accd82f4281 99 timerPFlag=0;
orlagilson 2:6b564e388747 100 Measurement measurement;
orlagilson 1:1accd82f4281 101 clearCells();
orlagilson 3:c9162dc9ba24 102 lcd.printString("Menu",0,5);
orlagilson 1:1accd82f4281 103 char bufferP[14];
orlagilson 1:1accd82f4281 104 measurement = bmp180.readValues();
orlagilson 1:1accd82f4281 105 int length=sprintf(bufferP,"P = %.2f mb",measurement.pressure);
orlagilson 1:1accd82f4281 106 if (length<=14){
orlagilson 1:1accd82f4281 107 lcd.printString(bufferP,0,2);
orlagilson 1:1accd82f4281 108 }
orlagilson 0:2fd314d76f37 109 }
orlagilson 4:0302731434a5 110 if (buttonFlag){
orlagilson 4:0302731434a5 111 buttonFlag=0;
orlagilson 4:0302731434a5 112 menu();
orlagilson 4:0302731434a5 113 }
orlagilson 0:2fd314d76f37 114 }
orlagilson 0:2fd314d76f37 115 }
orlagilson 0:2fd314d76f37 116
orlagilson 2:6b564e388747 117 void measurement(){
orlagilson 2:6b564e388747 118 Measurement measurement;
orlagilson 2:6b564e388747 119 measurement=bmp180.readValues();
orlagilson 2:6b564e388747 120 temperature=measurement.temperature;
orlagilson 2:6b564e388747 121 pressure=measurement.pressure;
orlagilson 2:6b564e388747 122 }
orlagilson 2:6b564e388747 123
orlagilson 2:6b564e388747 124 void tempGraph(){
orlagilson 4:0302731434a5 125 timerT.attach(&timerTExpired,2);
orlagilson 2:6b564e388747 126 int j=0; //start graph on left hand side of screen
orlagilson 2:6b564e388747 127 while(1){
orlagilson 3:c9162dc9ba24 128 lcd.printString("Menu",0,5);
orlagilson 2:6b564e388747 129 if (timerTFlag){
orlagilson 2:6b564e388747 130 timerTFlag=0;
orlagilson 2:6b564e388747 131 clearCells();
orlagilson 2:6b564e388747 132 float tempArray[84]; //create array of temperature values
orlagilson 2:6b564e388747 133 measurement(); //read in the measured values of temperature
orlagilson 2:6b564e388747 134 tempArray[j]=(temperature/47);
orlagilson 2:6b564e388747 135 j++; //add one to j so that the next point plotted moves across the screen by one pixel
orlagilson 2:6b564e388747 136 lcd.plotArray(tempArray); //plot the array
orlagilson 2:6b564e388747 137 wait(0.1); //wait one second before plotting the next point
orlagilson 2:6b564e388747 138 }
orlagilson 4:0302731434a5 139 if (buttonFlag){
orlagilson 4:0302731434a5 140 buttonFlag=0;
orlagilson 4:0302731434a5 141 menu();
orlagilson 4:0302731434a5 142 }
orlagilson 2:6b564e388747 143 }
orlagilson 2:6b564e388747 144 }
orlagilson 2:6b564e388747 145
orlagilson 2:6b564e388747 146 void pressGraph(){
orlagilson 4:0302731434a5 147 timerP.attach(&timerPExpired,2);
orlagilson 2:6b564e388747 148 int j=0;
orlagilson 2:6b564e388747 149 while(1){
orlagilson 3:c9162dc9ba24 150 lcd.printString("Menu",0,5);
orlagilson 2:6b564e388747 151 if (timerPFlag){
orlagilson 2:6b564e388747 152 timerPFlag=0;
orlagilson 2:6b564e388747 153 clearCells();
orlagilson 2:6b564e388747 154 float pressArray[84];
orlagilson 2:6b564e388747 155 measurement();
orlagilson 2:6b564e388747 156 pressArray[j]=(pressure/1100);
orlagilson 2:6b564e388747 157 j++;
orlagilson 2:6b564e388747 158 lcd.plotArray(pressArray);
orlagilson 4:0302731434a5 159 wait(0.1);
orlagilson 4:0302731434a5 160 }
orlagilson 4:0302731434a5 161 if (buttonFlag){
orlagilson 4:0302731434a5 162 buttonFlag=0;
orlagilson 4:0302731434a5 163 menu();
orlagilson 2:6b564e388747 164 }
orlagilson 2:6b564e388747 165 }
orlagilson 2:6b564e388747 166 }
orlagilson 2:6b564e388747 167
orlagilson 4:0302731434a5 168 void menu(){
orlagilson 4:0302731434a5 169 buttonleft.rise(&buttonPressed);
orlagilson 0:2fd314d76f37 170 while(1) {
orlagilson 2:6b564e388747 171 wait (0.1);
orlagilson 0:2fd314d76f37 172 lcd.normalMode(); //normal LCD colour mode
orlagilson 0:2fd314d76f37 173 lcd.setBrightness(0.5); //LCD backlight set to 50% brightness
orlagilson 0:2fd314d76f37 174 if (POT>(2.0/3.0)) {
orlagilson 0:2fd314d76f37 175 clearCells();
orlagilson 0:2fd314d76f37 176 lcd.printString("Temperature",10,1);
orlagilson 0:2fd314d76f37 177 lcd.printString(">",80,2);
orlagilson 0:2fd314d76f37 178 lcd.printString("Graph",0,5);
orlagilson 0:2fd314d76f37 179 lcd.printString("Current",43,5);
orlagilson 4:0302731434a5 180 if (buttonFlag){ //left button takes the user to the graph option
orlagilson 4:0302731434a5 181 buttonFlag=0;
orlagilson 0:2fd314d76f37 182 clearCells();
orlagilson 2:6b564e388747 183 tempGraph();
orlagilson 0:2fd314d76f37 184 }
orlagilson 0:2fd314d76f37 185 if (BUT2>0.9) { //right button takes the user to the current reading
orlagilson 0:2fd314d76f37 186 clearCells();
orlagilson 0:2fd314d76f37 187 readTemp();
orlagilson 0:2fd314d76f37 188 }
orlagilson 0:2fd314d76f37 189 }
orlagilson 0:2fd314d76f37 190 if ((POT>1.0/3.0)&&(POT<2.0/3.0)) {
orlagilson 0:2fd314d76f37 191 clearCells();
orlagilson 0:2fd314d76f37 192 lcd.printString("Pressure",20,1);
orlagilson 0:2fd314d76f37 193 lcd.printString("<",0,2);
orlagilson 0:2fd314d76f37 194 lcd.printString(">",80,2);
orlagilson 0:2fd314d76f37 195 lcd.printString("Graph",0,5);
orlagilson 0:2fd314d76f37 196 lcd.printString("Current",43,5);
orlagilson 4:0302731434a5 197 if (buttonFlag) {
orlagilson 4:0302731434a5 198 buttonFlag=0;
orlagilson 0:2fd314d76f37 199 clearCells();
orlagilson 2:6b564e388747 200 pressGraph();
orlagilson 0:2fd314d76f37 201 }
orlagilson 0:2fd314d76f37 202 if (BUT2>0.9) {
orlagilson 0:2fd314d76f37 203 clearCells();
orlagilson 0:2fd314d76f37 204 readPress();
orlagilson 0:2fd314d76f37 205 }
orlagilson 0:2fd314d76f37 206 }
orlagilson 0:2fd314d76f37 207 if (POT<(1.0/3.0)) {
orlagilson 0:2fd314d76f37 208 clearCells();
orlagilson 0:2fd314d76f37 209 lcd.printString("Light",30,1);
orlagilson 0:2fd314d76f37 210 lcd.printString("<",0,2);
orlagilson 0:2fd314d76f37 211 lcd.printString("Graph",0,5);
orlagilson 0:2fd314d76f37 212 lcd.printString("Current",43,5);
orlagilson 4:0302731434a5 213 if (buttonFlag) {
orlagilson 4:0302731434a5 214 buttonFlag=0;
orlagilson 0:2fd314d76f37 215 clearCells();
orlagilson 0:2fd314d76f37 216 //lightGraph();
orlagilson 0:2fd314d76f37 217 }
orlagilson 0:2fd314d76f37 218 if (BUT2>0.9) {
orlagilson 0:2fd314d76f37 219 clearCells();
orlagilson 0:2fd314d76f37 220 //readLight();
orlagilson 0:2fd314d76f37 221 }
orlagilson 0:2fd314d76f37 222 }
orlagilson 0:2fd314d76f37 223 }
orlagilson 0:2fd314d76f37 224 }
orlagilson 0:2fd314d76f37 225
orlagilson 0:2fd314d76f37 226 void clearCells ()
orlagilson 0:2fd314d76f37 227 {
orlagilson 0:2fd314d76f37 228 //loop through cells and clear
orlagilson 0:2fd314d76f37 229 for (int i=0; i<nx; i++) {
orlagilson 0:2fd314d76f37 230 for (int j=0; j<ny; j++) {
orlagilson 0:2fd314d76f37 231 lcd.clearPixel(i,j);
orlagilson 0:2fd314d76f37 232 }
orlagilson 0:2fd314d76f37 233 }
orlagilson 0:2fd314d76f37 234 lcd.refresh (); //must refresh to write buffer to display
orlagilson 0:2fd314d76f37 235 }
orlagilson 0:2fd314d76f37 236
orlagilson 1:1accd82f4281 237 int main(){
orlagilson 1:1accd82f4281 238 lcd.init();
orlagilson 1:1accd82f4281 239 bmp180.init();
orlagilson 1:1accd82f4281 240 menu();
orlagilson 0:2fd314d76f37 241 }