//V1 -BMP280 has an accuracy of +-3'. The initial value is of no concern as the overall Altitude is determine by subtracting the MIN reading from the MAX reading.
#include "U8glib.h" //Find and Install this library
#include "BMP280.h" //Find and Install this library
#include "Wire.h" //Find and Install this library
#define P0 1013.25 //1021.97 //1013.25 //1028.86 //Height above sea level? https://whatismyelevation.com/ I really need help with this
BMP280 bmp;
int piezoPin = 9; //PIN 9 connected to BUZZERs POSITIVE PIN
int waiter = 0; //Timers Initial State
int result;
int resulta;
int resultb;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); // OLED DISPLAY Type
char sT[20];
char sP[9];
char sA[9];
char sA_MIN[9];
char sA_MAX[9];
char sA_result[9];
char sA_resulta[9];
char sA_resultb[9];
double A_MIN = 0;
double A_MAX = 0;
void draw2(double T, double P, double A, double A_MIN, double A_MAX, double resulta) {
u8g.setFont(u8g_font_unifont);
resulta = A_MAX - A_MIN; //Determine difference in feet between the BMP's MIN and MAX readings
float resultb = resulta / 3.281; //Result is in METERS
dtostrf(A_MIN, 4, 2, sA_MIN);
dtostrf(A_MAX, 4, 2, sA_MAX);
dtostrf(resulta, 4, 4, sA_resulta);
dtostrf(resultb, 4, 4, sA_resultb);
dtostrf(T, 4, 2, sT);
dtostrf(P, 4, 2, sP);
dtostrf(A, 4, 2, sA);
u8g.drawStr( 5, 10 , "Feet : ");
u8g.drawStr( 60, 10, sA_resulta);
u8g.drawStr( 5, 30 , "m : ");
u8g.drawStr( 60, 30, sA_resultb);
u8g.drawStr( 5, 45 , "TEMP : ");
u8g.drawStr( 60, 45, sT);
u8g.drawStr( 5, 62, sA_MAX);
u8g.drawStr( 60, 62, sA_MIN);
}
void setup() {
pinMode(2, OUTPUT); //Set PIN 2 as an OUTPUT for the RED LED
pinMode(3, OUTPUT); //Set PIN 3 as an OUTPUT for the BLUE LED
pinMode(4, OUTPUT); //Set PIN 4 as an OUTPUT for the Green LED
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) {
int redl = 2; //Name Pin 2 redl
int white = 3; //Name PIN 3 white
int redr = 4; //Name Pin 4 redr
int wait = 50; //delay in ms, 1000ms equals one second
int wait2 = 30; // delay in ms, 1000ms equals one second
int wait3 = 10; // delay in ms, 1000ms equals one second
waiter = waiter + 1; //Increases the VALUE of WAITER by ONE every PROGRAM CYCLE
if (waiter >= 600) tone(piezoPin, 2000, 50); // Set Buzzer Delay based on # of program cycles. Sets Buzzers frequency and how long it beeps for
digitalWrite(redl, HIGH); // turn the redl LED ON
delay(wait2); // PAUSE
digitalWrite(redl, LOW); // turn the redl LED OFF
delay(wait); // PAUSE
digitalWrite(white, HIGH); // turn the white LED ON
delay(wait2); // PAUSE
digitalWrite(white, LOW); // turn the white LED OFF
delay(wait); //PAUSE
digitalWrite(redr, HIGH); // turn the redr LED ON
delay(wait2); // PAUSE
digitalWrite(redr, LOW); // turn the redr LED OFF
delay(wait); //PAUSE
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);
if ( A > A_MAX) {
A_MAX = A;
}
if ( A < A_MIN || A_MIN == 0) {
A_MIN = A;
}
do {
draw2(T, P, A, A_MIN, A_MAX, resulta); //(A_MIN, A_MAX, resulta); //Tells the program to go to DRAW2
} while ( u8g.nextPage() );
u8g.firstPage();
delay(wait3);
}
else {
Serial.println("Error.");
}
}
else {
Serial.println("Error.");
}
delay(wait3);
}
Comments