VarDuino v.0.2 - ESP8266 und BMP280 Drucksensor als Gleitschirm Variometer

1:  /*  
2:   Scrolling text example code  
3:   Modified from: http://www.arduino.cc/playground/Code/PCD8544  
4:   Arduvario test BMP280  
5:  */  
6:  // Your sketch must #include this library, and the Wire library.  
7:  // (Wire is a standard library included with Arduino.):  
8:  #include <BMP280.h>  
9:  #include <Wire.h>  
10:  #include <elapsedMillis.h>  
11:  #define P0 1013.2  
12:  elapsedMillis displayTimeElapsed; //declare global if you don't want it reset every time loop runs  
13:  elapsedMillis buzzerTimeElapsed; //declare global if you don't want it reset every time loop runs  
14:  String temp;  
15:  double lastHeight = 0;  
16:  double newHeight = 0;  
17:  int heightReadCounter = 0;  
18:  double addedHeight = 0;  
19:  double heightDiff = 0;  
20:  // You will need to create an SFE_BMP180 object, here called "pressure":  
21:  BMP280 pressure;  
22:  #define ALTITUDE 440.0 // Altitude of SparkFun's HQ in Boulder, CO. in meters  
23:  #define BASEFREQ 900  
24:  char status;  
25:  double T, P, a;  
26:  double altitude;  
27:  double p0; //sea level pressure  
28:  // delay in milliseconds  
29:  unsigned int displayUpdateInterval = 500;  
30:  unsigned int buzzerToggleInterval = 500;  
31:  // state of the LED = LOW is off, HIGH is on  
32:  boolean buzzerState = LOW;  
33:  // The pins to use on the arduino  
34:  #define PIN_SCE D8  
35:  #define PIN_RESET D1  
36:  #define PIN_DC D2  
37:  #define PIN_SDIN D7  
38:  #define PIN_SCLK D5  
39:  // COnfiguration for the LCD  
40:  #define LCD_C LOW  
41:  #define LCD_D HIGH  
42:  #define LCD_CMD 0  
43:  // Size of the LCD  
44:  #define LCD_X 84  
45:  #define LCD_Y 48  
46:  int scrollPosition = -10;  
47:  static const byte ASCII[][5] =  
48:  {  
49:   {0x00, 0x00, 0x00, 0x00, 0x00} // 20  
50:   , {0x00, 0x00, 0x5f, 0x00, 0x00} // 21 !  
51:   , {0x00, 0x07, 0x00, 0x07, 0x00} // 22 "  
52:   , {0x14, 0x7f, 0x14, 0x7f, 0x14} // 23 #  
53:   , {0x24, 0x2a, 0x7f, 0x2a, 0x12} // 24 $  
54:   , {0x23, 0x13, 0x08, 0x64, 0x62} // 25 %  
55:   , {0x36, 0x49, 0x55, 0x22, 0x50} // 26 &  
56:   , {0x00, 0x05, 0x03, 0x00, 0x00} // 27 ‘  
57:   , {0x00, 0x1c, 0x22, 0x41, 0x00} // 28 (  
58:   , {0x00, 0x41, 0x22, 0x1c, 0x00} // 29 )  
59:   , {0x14, 0x08, 0x3e, 0x08, 0x14} // 2a *  
60:   , {0x08, 0x08, 0x3e, 0x08, 0x08} // 2b +  
61:   , {0x00, 0x50, 0x30, 0x00, 0x00} // 2c ,  
62:   , {0x08, 0x08, 0x08, 0x08, 0x08} // 2d –  
63:   , {0x00, 0x60, 0x60, 0x00, 0x00} // 2e .  
64:   , {0x20, 0x10, 0x08, 0x04, 0x02} // 2f /  
65:   , {0x3e, 0x51, 0x49, 0x45, 0x3e} // 30 0  
66:   , {0x00, 0x42, 0x7f, 0x40, 0x00} // 31 1  
67:   , {0x42, 0x61, 0x51, 0x49, 0x46} // 32 2  
68:   , {0x21, 0x41, 0x45, 0x4b, 0x31} // 33 3  
69:   , {0x18, 0x14, 0x12, 0x7f, 0x10} // 34 4  
70:   , {0x27, 0x45, 0x45, 0x45, 0x39} // 35 5  
71:   , {0x3c, 0x4a, 0x49, 0x49, 0x30} // 36 6  
72:   , {0x01, 0x71, 0x09, 0x05, 0x03} // 37 7  
73:   , {0x36, 0x49, 0x49, 0x49, 0x36} // 38 8  
74:   , {0x06, 0x49, 0x49, 0x29, 0x1e} // 39 9  
75:   , {0x00, 0x36, 0x36, 0x00, 0x00} // 3a :  
76:   , {0x00, 0x56, 0x36, 0x00, 0x00} // 3b ;  
77:   , {0x08, 0x14, 0x22, 0x41, 0x00} // 3c <  
78:   , {0x14, 0x14, 0x14, 0x14, 0x14} // 3d =  
79:   , {0x00, 0x41, 0x22, 0x14, 0x08} // 3e >  
80:   , {0x02, 0x01, 0x51, 0x09, 0x06} // 3f ?  
81:   , {0x32, 0x49, 0x79, 0x41, 0x3e} // 40 @  
82:   , {0x7e, 0x11, 0x11, 0x11, 0x7e} // 41 A  
83:   , {0x7f, 0x49, 0x49, 0x49, 0x36} // 42 B  
84:   , {0x3e, 0x41, 0x41, 0x41, 0x22} // 43 C  
85:   , {0x7f, 0x41, 0x41, 0x22, 0x1c} // 44 D  
86:   , {0x7f, 0x49, 0x49, 0x49, 0x41} // 45 E  
87:   , {0x7f, 0x09, 0x09, 0x09, 0x01} // 46 F  
88:   , {0x3e, 0x41, 0x49, 0x49, 0x7a} // 47 G  
89:   , {0x7f, 0x08, 0x08, 0x08, 0x7f} // 48 H  
90:   , {0x00, 0x41, 0x7f, 0x41, 0x00} // 49 I  
91:   , {0x20, 0x40, 0x41, 0x3f, 0x01} // 4a J  
92:   , {0x7f, 0x08, 0x14, 0x22, 0x41} // 4b K  
93:   , {0x7f, 0x40, 0x40, 0x40, 0x40} // 4c L  
94:   , {0x7f, 0x02, 0x0c, 0x02, 0x7f} // 4d M  
95:   , {0x7f, 0x04, 0x08, 0x10, 0x7f} // 4e N  
96:   , {0x3e, 0x41, 0x41, 0x41, 0x3e} // 4f O  
97:   , {0x7f, 0x09, 0x09, 0x09, 0x06} // 50 P  
98:   , {0x3e, 0x41, 0x51, 0x21, 0x5e} // 51 Q  
99:   , {0x7f, 0x09, 0x19, 0x29, 0x46} // 52 R  
100:   , {0x46, 0x49, 0x49, 0x49, 0x31} // 53 S  
101:   , {0x01, 0x01, 0x7f, 0x01, 0x01} // 54 T  
102:   , {0x3f, 0x40, 0x40, 0x40, 0x3f} // 55 U  
103:   , {0x1f, 0x20, 0x40, 0x20, 0x1f} // 56 V  
104:   , {0x3f, 0x40, 0x38, 0x40, 0x3f} // 57 W  
105:   , {0x63, 0x14, 0x08, 0x14, 0x63} // 58 X  
106:   , {0x07, 0x08, 0x70, 0x08, 0x07} // 59 Y  
107:   , {0x61, 0x51, 0x49, 0x45, 0x43} // 5a Z  
108:   , {0x00, 0x7f, 0x41, 0x41, 0x00} // 5b [  
109:   , {0x02, 0x04, 0x08, 0x10, 0x20} // 5c ¥  
110:   , {0x00, 0x41, 0x41, 0x7f, 0x00} // 5d ]  
111:   , {0x04, 0x02, 0x01, 0x02, 0x04} // 5e ^  
112:   , {0x40, 0x40, 0x40, 0x40, 0x40} // 5f _  
113:   , {0x00, 0x01, 0x02, 0x04, 0x00} // 60 `  
114:   , {0x20, 0x54, 0x54, 0x54, 0x78} // 61 a  
115:   , {0x7f, 0x48, 0x44, 0x44, 0x38} // 62 b  
116:   , {0x38, 0x44, 0x44, 0x44, 0x20} // 63 c  
117:   , {0x38, 0x44, 0x44, 0x48, 0x7f} // 64 d  
118:   , {0x38, 0x54, 0x54, 0x54, 0x18} // 65 e  
119:   , {0x08, 0x7e, 0x09, 0x01, 0x02} // 66 f  
120:   , {0x0c, 0x52, 0x52, 0x52, 0x3e} // 67 g  
121:   , {0x7f, 0x08, 0x04, 0x04, 0x78} // 68 h  
122:   , {0x00, 0x44, 0x7d, 0x40, 0x00} // 69 i  
123:   , {0x20, 0x40, 0x44, 0x3d, 0x00} // 6a j  
124:   , {0x7f, 0x10, 0x28, 0x44, 0x00} // 6b k  
125:   , {0x00, 0x41, 0x7f, 0x40, 0x00} // 6c l  
126:   , {0x7c, 0x04, 0x18, 0x04, 0x78} // 6d m  
127:   , {0x7c, 0x08, 0x04, 0x04, 0x78} // 6e n  
128:   , {0x38, 0x44, 0x44, 0x44, 0x38} // 6f o  
129:   , {0x7c, 0x14, 0x14, 0x14, 0x08} // 70 p  
130:   , {0x08, 0x14, 0x14, 0x18, 0x7c} // 71 q  
131:   , {0x7c, 0x08, 0x04, 0x04, 0x08} // 72 r  
132:   , {0x48, 0x54, 0x54, 0x54, 0x20} // 73 s  
133:   , {0x04, 0x3f, 0x44, 0x40, 0x20} // 74 t  
134:   , {0x3c, 0x40, 0x40, 0x20, 0x7c} // 75 u  
135:   , {0x1c, 0x20, 0x40, 0x20, 0x1c} // 76 v  
136:   , {0x3c, 0x40, 0x30, 0x40, 0x3c} // 77 w  
137:   , {0x44, 0x28, 0x10, 0x28, 0x44} // 78 x  
138:   , {0x0c, 0x50, 0x50, 0x50, 0x3c} // 79 y  
139:   , {0x44, 0x64, 0x54, 0x4c, 0x44} // 7a z  
140:   , {0x00, 0x08, 0x36, 0x41, 0x00} // 7b {  
141:   , {0x00, 0x00, 0x7f, 0x00, 0x00} // 7c |  
142:   , {0x00, 0x41, 0x36, 0x08, 0x00} // 7d }  
143:   , {0x10, 0x08, 0x08, 0x10, 0x08} // 7e ←  
144:   , {0x00, 0x06, 0x09, 0x09, 0x06} // 7f →  
145:  };  
146:  void LcdCharacter(char character)  
147:  {  
148:   LcdWrite(LCD_D, 0x00);  
149:   for (int index = 0; index < 5; index++)  
150:   {  
151:    LcdWrite(LCD_D, ASCII[character - 0x20][index]);  
152:   }  
153:   LcdWrite(LCD_D, 0x00);  
154:  }  
155:  void LcdClear(void)  
156:  {  
157:   for (int index = 0; index < LCD_X * LCD_Y / 8; index++)  
158:   {  
159:    LcdWrite(LCD_D, 0x00);  
160:   }  
161:  }  
162:  void LcdInitialise(void)  
163:  {  
164:   pinMode(PIN_SCE, OUTPUT);  
165:   pinMode(PIN_RESET, OUTPUT);  
166:   pinMode(PIN_DC, OUTPUT);  
167:   pinMode(PIN_SDIN, OUTPUT);  
168:   pinMode(PIN_SCLK, OUTPUT);  
169:   digitalWrite(PIN_RESET, LOW);  
170:   digitalWrite(PIN_RESET, HIGH);  
171:   LcdWrite(LCD_CMD, 0x21); // LCD Extended Commands.  
172:   LcdWrite(LCD_CMD, 0xBf); // Set LCD Vop (Contrast). //B1  
173:   LcdWrite(LCD_CMD, 0x04); // Set Temp coefficent. //0x04  
174:   LcdWrite(LCD_CMD, 0x14); // LCD bias mode 1:48. //0x13  
175:   LcdWrite(LCD_CMD, 0x0C); // LCD in normal mode. 0x0d for inverse  
176:   LcdWrite(LCD_C, 0x20);  
177:   LcdWrite(LCD_C, 0x0C);  
178:  }  
179:  void LcdString(char *characters)  
180:  {  
181:   while (*characters)  
182:   {  
183:    LcdCharacter(*characters++);  
184:   }  
185:  }  
186:  void LcdWrite(byte dc, byte data)  
187:  {  
188:   digitalWrite(PIN_DC, dc);  
189:   digitalWrite(PIN_SCE, LOW);  
190:   shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);  
191:   digitalWrite(PIN_SCE, HIGH);  
192:  }  
193:  /**  
194:   gotoXY routine to position cursor  
195:   x – range: 0 to 84  
196:   y – range: 0 to 5  
197:  */  
198:  void gotoXY(int x, int y)  
199:  {  
200:   LcdWrite( 0, 0x80 | x); // Column.  
201:   LcdWrite( 0, 0x40 | y); // Row.  
202:  }  
203:  void drawBox(void)  
204:  {  
205:   int j;  
206:   for (j = 0; j < 84; j++) // top  
207:   {  
208:    gotoXY(j, 0);  
209:    LcdWrite(1, 0x01);  
210:   }  
211:   for (j = 0; j < 84; j++) //Bottom  
212:   {  
213:    gotoXY(j, 5);  
214:    LcdWrite(1, 0x80);  
215:   }  
216:   for (j = 0; j < 6; j++) // Right  
217:   {  
218:    gotoXY(83, j);  
219:    LcdWrite(1, 0xff);  
220:   }  
221:   for (j = 0; j < 6; j++) // Left  
222:   {  
223:    gotoXY(0, j);  
224:    LcdWrite(1, 0xff);  
225:   }  
226:  }  
227:  void Scroll(String message)  
228:  {  
229:   for (int i = scrollPosition; i < scrollPosition + 11; i++)  
230:   {  
231:    if ((i >= message.length()) || (i < 0))  
232:    {  
233:     LcdCharacter(' ');  
234:    }  
235:    else  
236:    {  
237:     LcdCharacter(message.charAt(i));  
238:    }  
239:   }  
240:   scrollPosition++;  
241:   if ((scrollPosition >= message.length()) && (scrollPosition > 0))  
242:   {  
243:    scrollPosition = -10;  
244:   }  
245:  }  
246:  void setup(void)  
247:  {  
248:   pinMode(16,OUTPUT);  
249:   analogWrite(16,125);  
250:   delay(500);  
251:   analogWrite(16,0);  
252:   delay(500);  
253:   analogWrite(16,125);  
254:   delay(500);  
255:   analogWrite(16,0);  
256:   delay(500);  
257:   digitalWrite(16,0);  
258:   Serial.begin(9600);  
259:   delay(100);  
260:   Serial.println("\n\r");  
261:   Serial.println("REBOOT");  
262:   Wire.begin(12, 2); //initializes the gpio for bmp180  
263:   Wire.setClock(400000L);  
264:   LcdInitialise();  
265:   LcdClear();  
266:   drawBox();  
267:   gotoXY(4, 1);  
268:   LcdString("Varduino");  
269:   gotoXY(4, 2);  
270:   LcdString("Init...");  
271:   if (pressure.begin())  
272:    Serial.println("BMP280 init success");  
273:   else  
274:   {  
275:    // Oops, something went wrong, this is usually a connection problem,  
276:    // see the comments at the top of this sketch for the proper connections.  
277:    Serial.println("BMP280 init fail\n\n");  
278:    while (1); // Pause forever.  
279:   }  
280:   pressure.setOversampling(4);  
281:   // Start a temperature measurement:  
282:   // If request is successful, the number of ms to wait is returned.  
283:   // If request is unsuccessful, 0 is returned.  
284:   delay(2000);  
285:   LcdClear();  
286:   drawBox();  
287:   gotoXY(4, 1);  
288:   LcdString("Varduino");  
289:  }  
290:  void readHeight() {  
291:   double tempHeight;  
292:   char result = pressure.startMeasurment();  
293:   if(result!=0){  
294:    delay(result);  
295:    result = pressure.getTemperatureAndPressure(T,P);  
296:     if(result!=0)  
297:     {  
298:      double tempHeight = pressure.altitude(P,P0);  
299:      addedHeight += tempHeight;  
300:      heightReadCounter++;  
301:      Serial.print("T = \t");Serial.print(T,2); Serial.print(" degC\t");  
302:      Serial.print("P = \t");Serial.print(P,2); Serial.print(" mBar\t");  
303:      Serial.print("A = \t");Serial.print(newHeight,2); Serial.println(" m");  
304:     }  
305:     else {  
306:      Serial.println("Error.");  
307:     }  
308:   }  
309:   else {  
310:    Serial.println("Error.");  
311:   }  
312:  }  
313:  void updateDisplay() {  
314:   gotoXY(1, 2);  
315:   LcdString("T:");  
316:   gotoXY(8, 2);  
317:   temp = String(T, 4);  
318:   char tempBuf[5];  
319:   temp.toCharArray(tempBuf, 5);  
320:   LcdString(tempBuf);  
321:   gotoXY(1, 3);  
322:   LcdString("P:");  
323:   gotoXY(8, 3);  
324:   char pressBuf[7];  
325:   temp = String(P, 2);  
326:   temp.toCharArray(pressBuf, 6);  
327:   LcdString(pressBuf);  
328:   gotoXY(1, 4);  
329:   LcdString("H:");  
330:   gotoXY(8, 4);  
331:   char heightBuf[7];  
332:   temp = String(newHeight, 1);  
333:   temp.toCharArray(heightBuf, 6);  
334:   LcdString(heightBuf);  
335:   gotoXY(44, 4);  
336:   LcdString("d:");  
337:   gotoXY(52, 4);  
338:   char diffBuf[6];  
339:   temp = String(heightDiff, 1);  
340:   temp.toCharArray(diffBuf, 5);  
341:   LcdString(diffBuf);  
342:  }  
343:  void loop(void)  
344:  {  
345:   readHeight();  
346:   readHeight();  
347:   if (displayTimeElapsed > displayUpdateInterval)  
348:   {  
349:    newHeight = addedHeight / heightReadCounter;  
350:    if (lastHeight == 0) {  
351:     heightDiff = 0;  
352:    } else {  
353:     heightDiff = newHeight - lastHeight;  
354:    }  
355:    Serial.print("addedHeight: ");  
356:    Serial.print(addedHeight, 2);  
357:    Serial.print(" ");  
358:    Serial.print("counter: ");  
359:    Serial.print(heightReadCounter);  
360:    Serial.print(" ");  
361:    Serial.print("newHeight: ");  
362:    Serial.print(newHeight, 2);  
363:    Serial.print(" ");  
364:    Serial.print("heightDiff: ");  
365:    Serial.print(heightDiff, 2);  
366:    Serial.print(" ");  
367:    Serial.print("\r\n");  
368:    lastHeight = newHeight;  
369:    addedHeight = 0;  
370:    heightReadCounter = 0;  
371:    updateDisplay();  
372:    displayTimeElapsed = 0;    // reset the counter to 0 so the counting starts over...  
373:   }  
374:   if (buzzerTimeElapsed > buzzerToggleInterval)  
375:   {  
376:    if (heightDiff > 0.5) {  
377:     analogWriteFreq(BASEFREQ * heightDiff);  
378:     if (buzzerState == LOW) { //set hight and BUUUUZZZZZZZ  
379:      buzzerState = HIGH;  
380:      analogWrite(16,200);  
381:     } else {  
382:      buzzerState = LOW;  
383:      analogWrite(16,0);  
384:     }  
385:    }else{  
386:     buzzerState = LOW;  
387:     analogWrite(16,0);  
388:    }  
389:    buzzerTimeElapsed = 0;  
390:   }  
391:  }  
Dieses Codebeispiel wurde formatier mit http://codeformatter.blogspot.ch/

Kommentare