Hi,
I am building an altimeter for a rocket project, have it up and running but there is a change that I would like to make.
The code is
// GM_U8glib - Version: Latest
#include <force_GM_U8glib.h>
#include <U8glib.h>
// GM_BMP280 - Version: Latest
#include <force_GM_BMP280.h>
#include <BMP280.h>
#include "Wire.h"
//Set up QNH pressure reference
#define P0 1014.00 //1013.25
BMP280 bmp;
// OLED Type
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);
char sT[20];
char sP[9];
char sA[9];
char sA_MIN[9];
char sA_MAX[9];
char sA_CHG[9];
double A_MIN = 0;
double A_MAX = 0;
double A_CHG = 0;
void draw(double T, double P, double A) {
u8g.setFont(u8g_font_unifont);
dtostrf(T, 4, 2, sT);
dtostrf(P, 4, 2, sP);
dtostrf(A, 4, 2, sA);
u8g.drawStr( 5, 10, "Temp: ");
u8g.drawStr( 5, 30, "Bar : ");
u8g.drawStr( 5, 50, "Alt : ");
u8g.drawStr( 50, 10, sT);
u8g.drawStr( 50, 30, sP);
u8g.drawStr( 50, 50, sA);
}
void draw2(double A_MIN, double A_MAX, double A_CHG) {
u8g.setFont(u8g_font_unifont);
dtostrf(A_MIN, 4, 2, sA_MIN);
dtostrf(A_MAX, 4, 2, sA_MAX);
dtostrf(A_CHG, 4, 2, sA_CHG);
u8g.drawStr( 5, 10, "A Min: ");
u8g.drawStr( 60, 10, sA_MIN);
u8g.drawStr( 5, 30, "A Max: ");
u8g.drawStr( 60, 30, sA_MAX);
u8g.drawStr(5, 50, "A Chg: ");
u8g.drawStr(60, 50, sA_CHG);
}
void setup() {
Serial.begin(9600);
if (!bmp.begin()) {
Serial.println("BMP init failed!");
while (1);
}
else Serial.println("BMP init success!");
bmp.setOversampling(4);
u8g.setColorIndex(1);
u8g.setFont(u8g_font_unifont);
}
void loop(void) {
double T, P;
char result = bmp.startMeasurment();
if (result != 0) {
delay(result);
result = bmp.getTemperatureAndPressure(T, P);
if (result != 0) {
double A = bmp.altitude(P, P0)*3.28084;
A_CHG = A_MAX-A_MIN;
if ( A > A_MAX) {
A_MAX = A;
}
if ( A < A_MIN || A_MIN == 0) {
A_MIN = A;
}
/*
Serial.print("T: "); Serial.print(T, 2); Serial.println(" degC\t");
Serial.print("P: "); Serial.print(P, 2); Serial.println(" mBar\t");
Serial.print("QNH: "); Serial.print(P0, 2); Serial.println(" mBar\t");
Serial.print("A: "); Serial.print(A, 2); Serial.println(" ft");
Serial.print("Max A: "); Serial.print(A_MAX, 2); Serial.println(" ft");
Serial.print("Min A: "); Serial.print(A_MIN, 2); Serial.println(" ft");
Serial.print("Delta A: "); Serial.print(A_MAX-A_MIN, 2); Serial.println(" ft");
Serial.println("");
*/
u8g.firstPage();
do {
draw(T, P, A);
} while ( u8g.nextPage() );
u8g.firstPage();
delay(1000);
do {
draw2(A_MIN, A_MAX, A_CHG);
} while ( u8g.nextPage() );
u8g.firstPage();
delay(1000);
}
else {
Serial.println("Error.");
}
}
else {
Serial.println("Error.");
}
delay(100);
}
At the moment (if I understand it correctly) the sketch updates the Temp (T), Pressure (P) and Altitude (A) every two seconds because of the two delay settings in the draw page 1 and draw page 2 segments.
I would like T, P, A, A_MAX and A_MIN to be updated at a much higher rate (<100ms?), and also be able to set the delay for each page display to 2 or 3 seconds.
I assume that this would involve separate loops for the calculations part and the displays part, but am far too new to all this to figure out how to achieve it. Any guidance would be very gratefully appreciated please.