Altimeter code help

I need some help in stabilizing the BMP sensor altitude numbers and recording the highest apogee found; is there any thing i can fix or improve?

#include <Wire.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

///////// fonts:freeMono,FreeSans, FreeSerif; italiucs version is for all - FreeSerifItalic; 9pt7b, 12pts7b, 18pt7b, 24pt7b,Adafruit5x7(default) & TomThumb;

//#include <Fonts/TomThumb.h>

#define screen_width 128
#define screen_height 32
#define reset_OLED -1

Adafruit_SSD1306 display( screen_width, screen_height, &Wire, reset_OLED);

Adafruit_BMP280 bmp;
float alt;
float seaPressure;// 1023.37;
 float apogee;
int dy = 500;


float averagePressure()
{
 float N = 100;
 float sum = 0;
for (int i = 0; i < N; i++)
{

 sum += bmp.readPressure()/100.;
delay(100);
}
return sum / N;
}


void setup() 
{

Serial.begin(9600);
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3c);

if( !bmp.begin(0x76) )
{
  display.setCursor(32,16);
  display.println( "BMP ERROR" );
  display.display();
  while(1);
  delay(dy);
}
seaPressure = averagePressure();
alt = bmp.readAltitude(seaPressure);

/////////////////// These can be setup once here forever///////////////////
  display.setFont();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  
}

void loop() {

  alt = bmp.readAltitude(seaPressure);
  Serial.println( alt );
  Serial.print("  ");
  Serial.println( seaPressure);

  display.clearDisplay();
  display.setCursor(5,10);
  display.println("Alt (m):");
  
  display.setCursor(60,10);
  display.println( alt,2 );
  display.display();
  delay(dy);


}

IF they are stable at the beginning and then go bad, it may be because you are flooding the serial output with data and it will block until space is available. Run your Baud rate up to the highest value that you can.

Please do not cross post. Continuation of Rocket Altimeter

My bad, i thought since this is the code it was separate from the other. Ill try to remember this for next time.

i see, thx

Please clearly describe the problem. Post examples of the offending data.

Hi, @professor_05
Can you please post some images of your project?
So we can see your component layout.

Tom.... :smiley: :+1: :coffee: :australia:


This i what i have at the moment

well i think i got it now, i decided to take the average and use the millis function instead of delay making the numbers not jump up and down too crazy


this use to go +- 1 before

To stop the jump...crazy, add two constants... minimum (+0.005 for example) and maximum (-0.035 for example). You will see two more traces, but they will constrain your real data and keep the plot steady.

1 Like