Orla Gilson / Mbed 2 deprecated WeatherLogger

Dependencies:   BMP180 N5110 mbed

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