1: /*
2: Scrolling text example code
3: Modified from: http://www.arduino.cc/playground/Code/PCD8544
4: Arduvario test BMP180
5: */
6: // Your sketch must #include this library, and the Wire library.
7: // (Wire is a standard library included with Arduino.):
8: #include <SFE_BMP180.h>
9: #include <Wire.h>
10: #include <elapsedMillis.h>
11: elapsedMillis displayTimeElapsed; //declare global if you don't want it reset every time loop runs
12: elapsedMillis buzzerTimeElapsed; //declare global if you don't want it reset every time loop runs
13: String temp;
14: double lastHeight = 0;
15: double newHeight = 0;
16: int heightReadCounter = 0;
17: double addedHeight = 0;
18: double heightDiff = 0;
19: // You will need to create an SFE_BMP180 object, here called "pressure":
20: SFE_BMP180 pressure;
21: #define ALTITUDE 440.0 // Altitude of SparkFun's HQ in Boulder, CO. in meters
22: #define BASEFREQ 900
23: char status;
24: double T, P, a;
25: double altitude;
26: double p0; //sea level pressure
27: // delay in milliseconds
28: unsigned int displayUpdateInterval = 500;
29: unsigned int buzzerToggleInterval = 500;
30: // state of the LED = LOW is off, HIGH is on
31: boolean buzzerState = LOW;
32: // The pins to use on the arduino
33: #define PIN_SCE D8
34: #define PIN_RESET D1
35: #define PIN_DC D2
36: #define PIN_SDIN D7
37: #define PIN_SCLK D5
38: // COnfiguration for the LCD
39: #define LCD_C LOW
40: #define LCD_D HIGH
41: #define LCD_CMD 0
42: // Size of the LCD
43: #define LCD_X 84
44: #define LCD_Y 48
45: int scrollPosition = -10;
46: static const byte ASCII[][5] =
47: {
48: {0x00, 0x00, 0x00, 0x00, 0x00} // 20
49: , {0x00, 0x00, 0x5f, 0x00, 0x00} // 21 !
50: , {0x00, 0x07, 0x00, 0x07, 0x00} // 22 "
51: , {0x14, 0x7f, 0x14, 0x7f, 0x14} // 23 #
52: , {0x24, 0x2a, 0x7f, 0x2a, 0x12} // 24 $
53: , {0x23, 0x13, 0x08, 0x64, 0x62} // 25 %
54: , {0x36, 0x49, 0x55, 0x22, 0x50} // 26 &
55: , {0x00, 0x05, 0x03, 0x00, 0x00} // 27 ‘
56: , {0x00, 0x1c, 0x22, 0x41, 0x00} // 28 (
57: , {0x00, 0x41, 0x22, 0x1c, 0x00} // 29 )
58: , {0x14, 0x08, 0x3e, 0x08, 0x14} // 2a *
59: , {0x08, 0x08, 0x3e, 0x08, 0x08} // 2b +
60: , {0x00, 0x50, 0x30, 0x00, 0x00} // 2c ,
61: , {0x08, 0x08, 0x08, 0x08, 0x08} // 2d –
62: , {0x00, 0x60, 0x60, 0x00, 0x00} // 2e .
63: , {0x20, 0x10, 0x08, 0x04, 0x02} // 2f /
64: , {0x3e, 0x51, 0x49, 0x45, 0x3e} // 30 0
65: , {0x00, 0x42, 0x7f, 0x40, 0x00} // 31 1
66: , {0x42, 0x61, 0x51, 0x49, 0x46} // 32 2
67: , {0x21, 0x41, 0x45, 0x4b, 0x31} // 33 3
68: , {0x18, 0x14, 0x12, 0x7f, 0x10} // 34 4
69: , {0x27, 0x45, 0x45, 0x45, 0x39} // 35 5
70: , {0x3c, 0x4a, 0x49, 0x49, 0x30} // 36 6
71: , {0x01, 0x71, 0x09, 0x05, 0x03} // 37 7
72: , {0x36, 0x49, 0x49, 0x49, 0x36} // 38 8
73: , {0x06, 0x49, 0x49, 0x29, 0x1e} // 39 9
74: , {0x00, 0x36, 0x36, 0x00, 0x00} // 3a :
75: , {0x00, 0x56, 0x36, 0x00, 0x00} // 3b ;
76: , {0x08, 0x14, 0x22, 0x41, 0x00} // 3c <
77: , {0x14, 0x14, 0x14, 0x14, 0x14} // 3d =
78: , {0x00, 0x41, 0x22, 0x14, 0x08} // 3e >
79: , {0x02, 0x01, 0x51, 0x09, 0x06} // 3f ?
80: , {0x32, 0x49, 0x79, 0x41, 0x3e} // 40 @
81: , {0x7e, 0x11, 0x11, 0x11, 0x7e} // 41 A
82: , {0x7f, 0x49, 0x49, 0x49, 0x36} // 42 B
83: , {0x3e, 0x41, 0x41, 0x41, 0x22} // 43 C
84: , {0x7f, 0x41, 0x41, 0x22, 0x1c} // 44 D
85: , {0x7f, 0x49, 0x49, 0x49, 0x41} // 45 E
86: , {0x7f, 0x09, 0x09, 0x09, 0x01} // 46 F
87: , {0x3e, 0x41, 0x49, 0x49, 0x7a} // 47 G
88: , {0x7f, 0x08, 0x08, 0x08, 0x7f} // 48 H
89: , {0x00, 0x41, 0x7f, 0x41, 0x00} // 49 I
90: , {0x20, 0x40, 0x41, 0x3f, 0x01} // 4a J
91: , {0x7f, 0x08, 0x14, 0x22, 0x41} // 4b K
92: , {0x7f, 0x40, 0x40, 0x40, 0x40} // 4c L
93: , {0x7f, 0x02, 0x0c, 0x02, 0x7f} // 4d M
94: , {0x7f, 0x04, 0x08, 0x10, 0x7f} // 4e N
95: , {0x3e, 0x41, 0x41, 0x41, 0x3e} // 4f O
96: , {0x7f, 0x09, 0x09, 0x09, 0x06} // 50 P
97: , {0x3e, 0x41, 0x51, 0x21, 0x5e} // 51 Q
98: , {0x7f, 0x09, 0x19, 0x29, 0x46} // 52 R
99: , {0x46, 0x49, 0x49, 0x49, 0x31} // 53 S
100: , {0x01, 0x01, 0x7f, 0x01, 0x01} // 54 T
101: , {0x3f, 0x40, 0x40, 0x40, 0x3f} // 55 U
102: , {0x1f, 0x20, 0x40, 0x20, 0x1f} // 56 V
103: , {0x3f, 0x40, 0x38, 0x40, 0x3f} // 57 W
104: , {0x63, 0x14, 0x08, 0x14, 0x63} // 58 X
105: , {0x07, 0x08, 0x70, 0x08, 0x07} // 59 Y
106: , {0x61, 0x51, 0x49, 0x45, 0x43} // 5a Z
107: , {0x00, 0x7f, 0x41, 0x41, 0x00} // 5b [
108: , {0x02, 0x04, 0x08, 0x10, 0x20} // 5c ¥
109: , {0x00, 0x41, 0x41, 0x7f, 0x00} // 5d ]
110: , {0x04, 0x02, 0x01, 0x02, 0x04} // 5e ^
111: , {0x40, 0x40, 0x40, 0x40, 0x40} // 5f _
112: , {0x00, 0x01, 0x02, 0x04, 0x00} // 60 `
113: , {0x20, 0x54, 0x54, 0x54, 0x78} // 61 a
114: , {0x7f, 0x48, 0x44, 0x44, 0x38} // 62 b
115: , {0x38, 0x44, 0x44, 0x44, 0x20} // 63 c
116: , {0x38, 0x44, 0x44, 0x48, 0x7f} // 64 d
117: , {0x38, 0x54, 0x54, 0x54, 0x18} // 65 e
118: , {0x08, 0x7e, 0x09, 0x01, 0x02} // 66 f
119: , {0x0c, 0x52, 0x52, 0x52, 0x3e} // 67 g
120: , {0x7f, 0x08, 0x04, 0x04, 0x78} // 68 h
121: , {0x00, 0x44, 0x7d, 0x40, 0x00} // 69 i
122: , {0x20, 0x40, 0x44, 0x3d, 0x00} // 6a j
123: , {0x7f, 0x10, 0x28, 0x44, 0x00} // 6b k
124: , {0x00, 0x41, 0x7f, 0x40, 0x00} // 6c l
125: , {0x7c, 0x04, 0x18, 0x04, 0x78} // 6d m
126: , {0x7c, 0x08, 0x04, 0x04, 0x78} // 6e n
127: , {0x38, 0x44, 0x44, 0x44, 0x38} // 6f o
128: , {0x7c, 0x14, 0x14, 0x14, 0x08} // 70 p
129: , {0x08, 0x14, 0x14, 0x18, 0x7c} // 71 q
130: , {0x7c, 0x08, 0x04, 0x04, 0x08} // 72 r
131: , {0x48, 0x54, 0x54, 0x54, 0x20} // 73 s
132: , {0x04, 0x3f, 0x44, 0x40, 0x20} // 74 t
133: , {0x3c, 0x40, 0x40, 0x20, 0x7c} // 75 u
134: , {0x1c, 0x20, 0x40, 0x20, 0x1c} // 76 v
135: , {0x3c, 0x40, 0x30, 0x40, 0x3c} // 77 w
136: , {0x44, 0x28, 0x10, 0x28, 0x44} // 78 x
137: , {0x0c, 0x50, 0x50, 0x50, 0x3c} // 79 y
138: , {0x44, 0x64, 0x54, 0x4c, 0x44} // 7a z
139: , {0x00, 0x08, 0x36, 0x41, 0x00} // 7b {
140: , {0x00, 0x00, 0x7f, 0x00, 0x00} // 7c |
141: , {0x00, 0x41, 0x36, 0x08, 0x00} // 7d }
142: , {0x10, 0x08, 0x08, 0x10, 0x08} // 7e ←
143: , {0x00, 0x06, 0x09, 0x09, 0x06} // 7f →
144: };
145: void LcdCharacter(char character)
146: {
147: LcdWrite(LCD_D, 0x00);
148: for (int index = 0; index < 5; index++)
149: {
150: LcdWrite(LCD_D, ASCII[character - 0x20][index]);
151: }
152: LcdWrite(LCD_D, 0x00);
153: }
154: void LcdClear(void)
155: {
156: for (int index = 0; index < LCD_X * LCD_Y / 8; index++)
157: {
158: LcdWrite(LCD_D, 0x00);
159: }
160: }
161: void LcdInitialise(void)
162: {
163: pinMode(PIN_SCE, OUTPUT);
164: pinMode(PIN_RESET, OUTPUT);
165: pinMode(PIN_DC, OUTPUT);
166: pinMode(PIN_SDIN, OUTPUT);
167: pinMode(PIN_SCLK, OUTPUT);
168: digitalWrite(PIN_RESET, LOW);
169: digitalWrite(PIN_RESET, HIGH);
170: LcdWrite(LCD_CMD, 0x21); // LCD Extended Commands.
171: LcdWrite(LCD_CMD, 0xBf); // Set LCD Vop (Contrast). //B1
172: LcdWrite(LCD_CMD, 0x04); // Set Temp coefficent. //0x04
173: LcdWrite(LCD_CMD, 0x14); // LCD bias mode 1:48. //0x13
174: LcdWrite(LCD_CMD, 0x0C); // LCD in normal mode. 0x0d for inverse
175: LcdWrite(LCD_C, 0x20);
176: LcdWrite(LCD_C, 0x0C);
177: }
178: void LcdString(char *characters)
179: {
180: while (*characters)
181: {
182: LcdCharacter(*characters++);
183: }
184: }
185: void LcdWrite(byte dc, byte data)
186: {
187: digitalWrite(PIN_DC, dc);
188: digitalWrite(PIN_SCE, LOW);
189: shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);
190: digitalWrite(PIN_SCE, HIGH);
191: }
192: /**
193: gotoXY routine to position cursor
194: x – range: 0 to 84
195: y – range: 0 to 5
196: */
197: void gotoXY(int x, int y)
198: {
199: LcdWrite( 0, 0x80 | x); // Column.
200: LcdWrite( 0, 0x40 | y); // Row.
201: }
202: void drawBox(void)
203: {
204: int j;
205: for (j = 0; j < 84; j++) // top
206: {
207: gotoXY(j, 0);
208: LcdWrite(1, 0x01);
209: }
210: for (j = 0; j < 84; j++) //Bottom
211: {
212: gotoXY(j, 5);
213: LcdWrite(1, 0x80);
214: }
215: for (j = 0; j < 6; j++) // Right
216: {
217: gotoXY(83, j);
218: LcdWrite(1, 0xff);
219: }
220: for (j = 0; j < 6; j++) // Left
221: {
222: gotoXY(0, j);
223: LcdWrite(1, 0xff);
224: }
225: }
226: void Scroll(String message)
227: {
228: for (int i = scrollPosition; i < scrollPosition + 11; i++)
229: {
230: if ((i >= message.length()) || (i < 0))
231: {
232: LcdCharacter(' ');
233: }
234: else
235: {
236: LcdCharacter(message.charAt(i));
237: }
238: }
239: scrollPosition++;
240: if ((scrollPosition >= message.length()) && (scrollPosition > 0))
241: {
242: scrollPosition = -10;
243: }
244: }
245: void setup(void)
246: {
247: pinMode(16,OUTPUT);
248: delay(100);
249: digitalWrite(16,0);
250: Serial.begin(9600);
251: delay(100);
252: Serial.println("\n\r");
253: Serial.println("REBOOT");
254: Wire.begin(12, 2); //initializes the gpio for bmp180
255: Wire.setClock(400000L);
256: LcdInitialise();
257: LcdClear();
258: drawBox();
259: gotoXY(4, 1);
260: LcdString("Varduino");
261: gotoXY(4, 2);
262: LcdString("Init...");
263: if (pressure.begin())
264: Serial.println("BMP180 init success");
265: else
266: {
267: // Oops, something went wrong, this is usually a connection problem,
268: // see the comments at the top of this sketch for the proper connections.
269: Serial.println("BMP180 init fail\n\n");
270: while (1); // Pause forever.
271: }
272: // Start a temperature measurement:
273: // If request is successful, the number of ms to wait is returned.
274: // If request is unsuccessful, 0 is returned.
275: status = pressure.startTemperature();
276: if (status != 0)
277: {
278: // Wait for the measurement to complete:
279: delay(status);
280: // Retrieve the completed temperature measurement:
281: // Note that the measurement is stored in the variable T.
282: // Function returns 1 if successful, 0 if failure.
283: status = pressure.getTemperature(T);
284: if (status != 0)
285: {
286: // Print out the measurement:
287: Serial.print("temperature: ");
288: Serial.print(T, 2);
289: Serial.print(" deg C, ");
290: // Serial.print((9.0/5.0)*T+32.0,2);
291: // Serial.println(" deg F");
292: status = pressure.startPressure(3);
293: if (status != 0)
294: {
295: // Wait for the measurement to complete:
296: delay(status);
297: // Retrieve the completed pressure measurement:
298: // Note that the measurement is stored in the variable P.
299: // Note also that the function requires the previous temperature measurement (T).
300: // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
301: // Function returns 1 if successful, 0 if failure.
302: status = pressure.getPressure(P, T);
303: if (status != 0)
304: {
305: // Print out the measurement:
306: Serial.print("absolute pressure: ");
307: Serial.print(P, 2);
308: Serial.print(" mb, ");
309: // Serial.print(P*0.0295333727,2);
310: // Serial.println(" inHg");
311: // The pressure sensor returns abolute pressure, which varies with altitude.
312: // To remove the effects of altitude, use the sealevel function and your current altitude.
313: // This number is commonly used in weather reports.
314: // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
315: // Result: p0 = sea-level compensated pressure in mb
316: p0 = 1013.25; //on init set medium sealevel
317: // On the other hand, if you want to determine your altitude from the pressure reading,
318: // use the altitude function along with a baseline pressure (sea-level or other).
319: // Parameters: P = absolute pressure in mb, p0 = baseline pressure in mb.
320: // Result: a = altitude in m.
321: altitude = pressure.altitude(P, p0);
322: Serial.print("theoretical altitude: ");
323: Serial.print(altitude, 3);
324: Serial.print(" meters, ");
325: }
326: else Serial.println("error retrieving pressure measurement\n");
327: }
328: else Serial.println("error starting pressure measurement\n");
329: }
330: else Serial.println("error retrieving temperature measurement\n");
331: }
332: else Serial.println("error starting temperature measurement\n");
333: delay(2000);
334: LcdClear();
335: drawBox();
336: gotoXY(4, 1);
337: LcdString("Varduino");
338: }
339: void readHeight() {
340: // Start a temperature measurement:
341: // If request is successful, the number of ms to wait is returned.
342: // If request is unsuccessful, 0 is returned.
343: status = pressure.startTemperature();
344: if (status != 0)
345: {
346: // Wait for the measurement to complete:
347: delay(status);
348: // Retrieve the completed temperature measurement:
349: // Note that the measurement is stored in the variable T.
350: // Function returns 1 if successful, 0 if failure.
351: status = pressure.getTemperature(T);
352: if (status != 0)
353: {
354: // Print out the measurement:
355: //Serial.print("temperature: ");
356: //Serial.print(T, 2);
357: //Serial.print(" deg C, ");
358: // Serial.print((9.0/5.0)*T+32.0,2);
359: // Serial.println(" deg F");
360: status = pressure.startPressure(3);
361: if (status != 0)
362: {
363: // Wait for the measurement to complete:
364: delay(status);
365: // Retrieve the completed pressure measurement:
366: // Note that the measurement is stored in the variable P.
367: // Note also that the function requires the previous temperature measurement (T).
368: // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
369: // Function returns 1 if successful, 0 if failure.
370: status = pressure.getPressure(P, T);
371: if (status != 0)
372: {
373: // Print out the measurement:
374: // Serial.print("absolute pressure: ");
375: //Serial.print(P, 2);
376: //Serial.print(" mb, ");
377: // Serial.print(P*0.0295333727,2);
378: // Serial.println(" inHg");
379: // The pressure sensor returns abolute pressure, which varies with altitude.
380: // To remove the effects of altitude, use the sealevel function and your current altitude.
381: // This number is commonly used in weather reports.
382: // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
383: // Result: p0 = sea-level compensated pressure in mb
384: /*
385: p0 = pressure.sealevel(P, ALTITUDE); // we're at 1655 meters (Boulder, CO)
386: Serial.print("relative (sea-level) pressure: ");
387: Serial.print(p0, 2);
388: Serial.print(" mb, ");
389: // Serial.print(p0*0.0295333727,2);
390: // Serial.println(" inHg");
391: */
392: // On the other hand, if you want to determine your altitude from the pressure reading,
393: // use the altitude function along with a baseline pressure (sea-level or other).
394: // Parameters: P = absolute pressure in mb, p0 = baseline pressure in mb.
395: // Result: a = altitude in m.
396: a = pressure.altitude(P, p0);
397: // Serial.print("computed altitude: ");
398: // Serial.print(a, 3);
399: // Serial.print(" meters, ");
400: // Serial.print(a*3.28084,0);
401: // Serial.println(" feet");
402: addedHeight += a;
403: heightReadCounter++;
404: Serial.print("\r\n");
405: }
406: else Serial.println("error retrieving pressure measurement\n");
407: }
408: else Serial.println("error starting pressure measurement\n");
409: }
410: else Serial.println("error retrieving temperature measurement\n");
411: }
412: else Serial.println("error starting temperature measurement\n");
413: }
414: void updateDisplay() {
415: gotoXY(1, 2);
416: LcdString("T:");
417: gotoXY(8, 2);
418: temp = String(T, 4);
419: char tempBuf[5];
420: temp.toCharArray(tempBuf, 5);
421: LcdString(tempBuf);
422: gotoXY(1, 3);
423: LcdString("P:");
424: gotoXY(8, 3);
425: char pressBuf[7];
426: temp = String(P, 2);
427: temp.toCharArray(pressBuf, 6);
428: LcdString(pressBuf);
429: gotoXY(1, 4);
430: LcdString("H:");
431: gotoXY(8, 4);
432: char heightBuf[7];
433: temp = String(newHeight, 1);
434: temp.toCharArray(heightBuf, 6);
435: LcdString(heightBuf);
436: gotoXY(44, 4);
437: LcdString("d:");
438: gotoXY(52, 4);
439: char diffBuf[6];
440: temp = String(heightDiff, 1);
441: temp.toCharArray(diffBuf, 5);
442: LcdString(diffBuf);
443: }
444: void loop(void)
445: {
446: readHeight();
447: readHeight();
448: if (displayTimeElapsed > displayUpdateInterval)
449: {
450: newHeight = addedHeight / heightReadCounter;
451: if (lastHeight == 0) {
452: heightDiff = 0;
453: } else {
454: heightDiff = newHeight - lastHeight;
455: }
456: Serial.print("addedHeight: ");
457: Serial.print(addedHeight, 2);
458: Serial.print(" ");
459: Serial.print("counter: ");
460: Serial.print(heightReadCounter);
461: Serial.print(" ");
462: Serial.print("newHeight: ");
463: Serial.print(newHeight, 2);
464: Serial.print(" ");
465: Serial.print("heightDiff: ");
466: Serial.print(heightDiff, 2);
467: Serial.print(" ");
468: Serial.print("\r\n");
469: lastHeight = newHeight;
470: addedHeight = 0;
471: heightReadCounter = 0;
472: updateDisplay();
473: displayTimeElapsed = 0; // reset the counter to 0 so the counting starts over...
474: }
475: if (buzzerTimeElapsed > buzzerToggleInterval)
476: {
477: if (heightDiff > 0.5) {
478: analogWriteFreq(BASEFREQ * heightDiff);
479: if (buzzerState == LOW) { //set hight and BUUUUZZZZZZZ
480: buzzerState = HIGH;
481: analogWrite(16,200);
482: } else {
483: buzzerState = LOW;
484: analogWrite(16,0);
485: }
486: }else{
487: buzzerState = LOW;
488: analogWrite(16,0);
489: }
490: buzzerTimeElapsed = 0;
491: }
492: }
Kommentare
Kommentar veröffentlichen