-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvariometro_thiagogo.ino
312 lines (260 loc) · 6.96 KB
/
variometro_thiagogo.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <Wire.h> // Biblioteca de interface I2C
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h> // Biblioteca Piezo Tone
#include "U8glib.h"
#define tone_out1 8
#define tone_out2 9
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send AC
Adafruit_BMP280 sensor_bmp;
short speaker_pin1 = 8; //arduino speaker output -
short speaker_pin2 = 9; //arduino speaker output +
float vario_down = -1.1; // Definir a redução
float vario_up;
float alt[51];
float tim[51];
float beep;
float Beep_period;
float mux;
float Alt0; // Altura zero, no momento da inclusão +!
float vario=0;
float Altitude = 0;
float Temperature = 0;
unsigned long bounseInput4P = 0UL;
unsigned long time;
unsigned char samples=10;
unsigned char maxsamples=50;
unsigned char countPressVal = 0;
unsigned char tela = 0;
bool tmp1 = 0;
bool countPress = 0;
bool bounseInput4S = 0;
bool bounseInput4O = 0;
//relogio
unsigned char relogio=0;
unsigned char segundo = 0, minuto = 0, hora = 0;
unsigned long UtlTime;
unsigned long lastMillis;
boolean thermalling = false;
void play_welcome_beep() // Saudações de áudio
{
for (int aa=100;aa<=800;aa=aa+100)
{
tone(tone_out1,aa,200);
tone(tone_out2,aa+3,200);
delay(50);
}
}
void draw(void) {
u8g.setColorIndex(1); // Instructs the display to draw with a pixel on.
u8g.setFont(u8g_font_8x13Br); //para o alfabeto completo com caracteres tirar o r do font_8x13Br
u8g.setColorIndex(0);
u8g.setColorIndex(1);
unsigned char posVar = 0;
unsigned char posAlt = 0;
//temperatura
u8g.setPrintPos(0, 14);
u8g.print(Temperature,1);
u8g.drawStr( 35, 14, "C"); //acentos http://www.ascii-code.com/
//caixa e m/s vario
u8g.drawFrame(50,0,78,35); //quadrado vario
u8g.drawStr(103, 30, "M\057s"); //m/s
u8g.drawStr(120, 64, "M"); //M altimetro
//sensibilidade e relogio
if(relogio==1){
u8g.drawStr(0, 30, "v");
u8g.setPrintPos(10, 30);
u8g.print(mux);
}
else{
String timex = "";
if(hora < 10){
timex += "0";
}
timex += hora;
if (segundo%2==0)
{
timex += ":";
}
else{
timex += " ";
}
if(minuto < 10){
timex += "0";
}
timex += minuto;
u8g.setPrintPos(0, 34);
u8g.print(timex);
}
//altimetro
if (Altitude >= 1000)
{
posAlt = 28;
}
else
{
posAlt = 48;
}
u8g.setFont(u8g_font_fub20n); //se for so numeros usar o n no fim u8g.setFont(u8g_font_fub20n);
u8g.setPrintPos(posAlt, 64);
u8g.print(Altitude, 1);
//vario
if (vario < 0)
{
posVar = 52;
if(vario < -0.1){
u8g.drawTriangle(113,15, 122,7, 105,7);
}
}
else
{
posVar = 61;
if(vario > 0.1){
u8g.drawTriangle(114,6, 124,15, 104,15);
}
}
u8g.setPrintPos(posVar, 27);
u8g.print(vario,1);
//bateria
int bateria = analogRead(A0);
u8g.drawFrame(4,42,4,2);
u8g.drawFrame(2,44,8,18);
if(bateria > 716){
u8g.drawStr( 1, 60, ".");
}
if(bateria > 770){
u8g.drawStr( 1, 55, ".");
}
if(bateria > 830){
u8g.drawStr( 1, 50, ".");
}
// conta para conversão voltagem*1024:5= valor arduino analog
/*
* usar divisor de tensão vcc--10k -A0-100K--gnd
*/
}
void setup()
{
Wire.begin(); // Inicializa i2c
pinMode(4, INPUT);
digitalWrite(4, HIGH);
bounseInput4O = digitalRead(4);// Resistor Pullup
pinMode(tone_out1, OUTPUT); // Dinâmica pin8 de saída -
pinMode(tone_out2, OUTPUT); // Speaker pin9 saída +
sensor_bmp.begin(); // Sensibilidade do sensor de pressão
play_welcome_beep();
}
void loop(void)
{
//oled display
u8g.firstPage();
bool bounceTmp = (digitalRead (4));
if (bounseInput4S) // Bounce protecção
{
if (millis() >= (bounseInput4P + 40))
{bounseInput4O= bounceTmp; bounseInput4S=0;}
}
else{
if (bounceTmp != bounseInput4O )
{bounseInput4S=1; bounseInput4P = millis();}
}
if (!(bounseInput4O))
{
if (! countPress)
{
countPressVal++;
//mostra relogio e variaçaõ
lastMillis = millis();
relogio = 1;
for (int i = 0; i < countPressVal; i++) // Perder o número do menu
{
tone(tone_out2,1800,40);
delay(80);
}
countPress = 1;
}
}
else{
countPress=0;
}
if (countPressVal < 0 ) countPressVal = 0;
if (tmp1) countPressVal = 0; // == MENU SENSIBILIDADE PARA LEVANTAR ==
if((countPressVal) == 0) {mux = 0.3;} // 1 sinal
if((countPressVal) == 1) {mux = 0.5;} // 2 sinal
if((countPressVal) == 2) {mux = 0.8;} // 3 sinal
tmp1 = countPressVal >= 3;
vario_up = mux;
//mostra relogio e variaçaõ
if (lastMillis > 0 && (millis() - lastMillis > 3000))
{
relogio = 0;
lastMillis = 0;
}
float tempo=millis();
float N1=0;
float N2=0;
float N3=0;
float D1=0;
float D2=0;
Altitude = (sensor_bmp.readAltitude(1013.25));
Temperature = (sensor_bmp.readTemperature());
for(int cc=1;cc<=maxsamples;cc++){ // averager
alt[(cc-1)]=alt[cc];
tim[(cc-1)]=tim[cc];
};
alt[maxsamples]=Altitude;
tim[maxsamples]=tempo;
float stime=tim[maxsamples-samples];
for(int cc=(maxsamples-samples);cc<maxsamples;cc++){
N1+=(tim[cc]-stime)*alt[cc];
N2+=(tim[cc]-stime);
N3+=(alt[cc]);
D1+=(tim[cc]-stime)*(tim[cc]-stime);
D2+=(tim[cc]-stime);
};
vario=1000*((samples*N1)-N2*N3)/(samples*D1-D2*D2); // Cálculo de som
if ((tempo-beep)>Beep_period)
{
beep=tempo;
if (vario>vario_up && vario<15 )
{
Beep_period=350-(vario*5);
tone(tone_out1,(1000+(100*vario)),300-(vario*5)); // Som em ascensão
tone(tone_out2,(1003+(100*vario)),300-(vario*5));
thermalling = true;
}
else if ((vario < 0 ) && (thermalling == true))
{
thermalling = false;
// tone_out2.play(200, 800); Predpotok // Som (é opcional)
}
else if (vario< vario_down){ // Som a afundar
Beep_period=200;
tone(tone_out1,(300-(vario)),340);
tone(tone_out2,(303-(vario)),340);
thermalling = false;
}
}
//relogio
if(millis()-UtlTime<0)
{
UtlTime=millis();
}
else{
segundo=int((millis()-UtlTime)/1000);
}
if(segundo>59)
{
segundo=0;
minuto++;
UtlTime=millis();
if(minuto>59)
{
hora++;
minuto=0;
}
}
do {
draw();
} while( u8g.nextPage() );
//fim oled display
}