Orla Gilson / Mbed 2 deprecated WeatherLogger

Dependencies:   BMP180 N5110 mbed

Committer:
orlagilson
Date:
Thu Apr 30 16:25:48 2015 +0000
Revision:
3:c9162dc9ba24
Parent:
2:6b564e388747
Child:
4:0302731434a5
Fourth commit; Timeout function working, calls temperature or pressure 0.1 seconds after menu selection is made. Prevents blank screen being shown until first ticker function is called.; No light measurements yet.

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