help sought to rearrange programme timing please

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. :slight_smile:

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.

If you are going to display the data for 2 or 3 seconds, why does it matter how much it changes while you are displaying old data?

How are you going to see the screen when the device is mounted in a rocket and launched?

I assume that this would involve separate loops for the calculations part and the displays part

What part of reading the temperature, pressure, and altitude involves a loop? How does displaying page 1 and then page 2 involve a loop?

You can separate the FUNCTIONS of reading and drawing, and only call the draw function periodically. See the blink without delay example. The philosophy applies to more than blinking a fool LED.

PaulS:
If you are going to display the data for 2 or 3 seconds, why does it matter how much it changes while you are displaying old data?

Because I want the values to update in real time while the rocket is flying, but I will only be reading the data once it has returned. Mainly interested in max and min altitude, the other data is for calibration and verification before launch.

How are you going to see the screen when the device is mounted in a rocket and launched?
What part of reading the temperature, pressure, and altitude involves a loop? How does displaying page 1 and then page 2 involve a loop?

See above for first part of your question.
I am assuming that the program runs through the entire void loop from the first '{' to the last '}', (after delay(100)) before repeating, hence they are in a loop?

You can separate the FUNCTIONS of reading and drawing, and only call the draw function periodically. See the blink without delay example. The philosophy applies to more than blinking a fool LED.

I am assuming that the program runs through the entire void loop from the first '{' to the last '}', (after delay(100)) before repeating, hence they are in a loop?

True, but "do this" and "do that" are not loops, necessarily. They are functions to be performed.

I can't see the point of the display that you can't see until after the device has landed.

I can see the point of knowing the maximum altitude reached, and can see the need to sample altitude more often, so don't miss the maximum. I can't see the point of measuring the pressure and temperature any more often, since the only values that you will ever see are those prior to launch and after landing.

But, the key is to use the blink without delay philosophy to determine when to draw page one and when to draw page two, reading the sensor(s) on every pass through loop(). Just hope that you are not drawing a page when the rocket is nearing its highest point, or you'll miss that.

Hi,

How are you going to store the readings to look at them after the flight?

I would think displaying the data in realtime is not the major requirement.

Tom.... :slight_smile:

TomGeorge:
Hi,
How are you going to store the readings to look at them after the flight?
I would think displaying the data in realtime is not the major requirement.

Tom.... :slight_smile:

The relevant values are max and min altitude which are stored in A_MAX and A_MIN until reset or power off.

PaulS:
True, but "do this" and "do that" are not loops, necessarily. They are functions to be performed.

I can't see the point of the display that you can't see until after the device has landed.

Agreed, I want to see the display before launch for calibration and again after the flight for height info. There is no need for the display to be writing while in flight.

I can see the point of knowing the maximum altitude reached, and can see the need to sample altitude more often, so don't miss the maximum. I can't see the point of measuring the pressure and temperature any more often, since the only values that you will ever see are those prior to launch and after landing.

The altitude is a calculated item that is derived from pressure and temperature so they need to be updated.

But, the key is to use the blink without delay philosophy to determine when to draw page one and when to draw page two, reading the sensor(s) on every pass through loop(). Just hope that you are not drawing a page when the rocket is nearing its highest point, or you'll miss that.

Thanks, I will look into this.

GrahamM:
Thanks, I will look into [the blink without delay philosophy]

It's one of two or three key things to get your mind round; time invested in understanding the blink without delay philosophy will be well spent.

Ok I have it working, it may be 'quick and dirty' and for reasons that I may not realise it could be inefficient.

I put in a couple of checks on the screen drawing so that the screens are only drawn

  • if A_CHG <20 or if A <(A_MAX-20) otherwise screen drawing is disabled.

With this in place the screens are drawn until such time as the rocket has climbed 20ft or more, then the screen drawing is halted until the rocket has reached maximum altitude and then dropped at least 20ft in the descent.

Tested it in my building elevator and it seems to work fine.

Thanks for the replies to my question folks, appreciated :slight_smile: