1
2#include "U8glib.h"
3#include "BMP280.h"
4#include "Wire.h"
5#define P0 1013.25
6BMP280 bmp;
7
8int piezoPin = 9;
9int waiter = 0;
10int result;
11int resulta;
12int resultb;
13
14U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);
15
16char sT[20];
17char sP[9];
18char sA[9];
19char sA_MIN[9];
20char sA_MAX[9];
21char sA_result[9];
22char sA_resulta[9];
23char sA_resultb[9];
24double A_MIN = 0;
25double A_MAX = 0;
26
27void draw2(double T, double P, double A, double A_MIN, double A_MAX, double resulta) {
28 u8g.setFont(u8g_font_unifont);
29
30resulta = A_MAX - A_MIN;
31float resultb = resulta / 3.281;
32
33 dtostrf(A_MIN, 4, 2, sA_MIN);
34 dtostrf(A_MAX, 4, 2, sA_MAX);
35 dtostrf(resulta, 4, 4, sA_resulta);
36 dtostrf(resultb, 4, 4, sA_resultb);
37 dtostrf(T, 4, 2, sT);
38 dtostrf(P, 4, 2, sP);
39 dtostrf(A, 4, 2, sA);
40
41 u8g.drawStr( 5, 10 , "Feet : ");
42 u8g.drawStr( 60, 10, sA_resulta);
43
44 u8g.drawStr( 5, 30 , "m : ");
45 u8g.drawStr( 60, 30, sA_resultb);
46
47 u8g.drawStr( 5, 45 , "TEMP : ");
48 u8g.drawStr( 60, 45, sT);
49
50 u8g.drawStr( 5, 62, sA_MAX);
51 u8g.drawStr( 60, 62, sA_MIN);
52
53}
54
55void setup() {
56
57 pinMode(2, OUTPUT);
58 pinMode(3, OUTPUT);
59 pinMode(4, OUTPUT);
60 Serial.begin(9600);
61 if (!bmp.begin()) {
62 Serial.println("BMP init failed!");
63 while (1);
64 }
65 else Serial.println("BMP init success!");
66
67 bmp.setOversampling(4);
68
69 u8g.setColorIndex(1);
70 u8g.setFont(u8g_font_unifont);
71}
72
73
74void loop(void) {
75
76int redl = 2;
77int white = 3;
78int redr = 4;
79
80int wait = 50;
81int wait2 = 30;
82int wait3 = 10;
83
84waiter = waiter + 1;
85if (waiter >= 600) tone(piezoPin, 2000, 50);
86
87digitalWrite(redl, HIGH);
88delay(wait2);
89digitalWrite(redl, LOW);
90delay(wait);
91
92digitalWrite(white, HIGH);
93delay(wait2);
94digitalWrite(white, LOW);
95delay(wait);
96
97digitalWrite(redr, HIGH);
98delay(wait2);
99digitalWrite(redr, LOW);
100delay(wait);
101
102 double T, P;
103 char result = bmp.startMeasurment();
104
105 if (result != 0) {
106 delay(result);
107 result = bmp.getTemperatureAndPressure(T, P);
108
109 if (result != 0) {
110 double A = bmp.altitude(P, P0);
111
112 if ( A > A_MAX) {
113 A_MAX = A;
114 }
115
116 if ( A < A_MIN || A_MIN == 0) {
117 A_MIN = A;
118 }
119
120 do {
121 draw2(T, P, A, A_MIN, A_MAX, resulta);
122 } while ( u8g.nextPage() );
123 u8g.firstPage();
124 delay(wait3);
125
126 }
127 else {
128 Serial.println("Error.");
129 }
130 }
131 else {
132 Serial.println("Error.");
133 }
134
135 delay(wait3);
136
137}
138