Rocket dual deploy recording altimeter

I've been having trouble making the MOS module i have connected to pin 8 activate correctly. When the if statement regarding it is standing alone in the loop it activates immediately even if the conditions are not met, and if i put inside the If statement regarding the maxAlt variable it doesnt activate at all. I was wondering if anyone could give me a hand with this as i am very new to C++ and arduino.

Here is my code:

int Eject = 250;  //set ejection charge to desired height before flight
int Pressure = 1015.00; //set according to the weather on launch day


//BMP280
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>

//SD CARD
#include <SPI.h>
#include <SD.h>

File myFile;

#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)

Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);


void setup() {
  Serial.begin(9600);
  while ( !Serial ) delay(100);   // wait for native usb
  unsigned status;
  //status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
  status = bmp.begin(0x76);
 

  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
  

 
  !SD.begin(4);
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("Flight.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    myFile.println("");
    myFile.println("Time, Altitude m, Apogee m, Ejection");
    // close the file:
    myFile.close();
  }
  pinMode(8, OUTPUT);

  Serial.println(bmp.readPressure()); //check current air pressure
}

void loop() {

  static int maxAlt = 0; //Apogee code
  int A = bmp.readAltitude(Pressure);
  if ( A > maxAlt ) {
    maxAlt = A;
  }

  if (( A < maxAlt ) and ( A <= Eject )) { //Ejection charge firing
    digitalWrite(8, HIGH);
  }

    myFile = SD.open("Flight.txt", FILE_WRITE); //Recording data
    myFile.print(millis());
    myFile.print(",");
    myFile.print(bmp.readAltitude(Pressure));
    myFile.print(",");
    myFile.print(maxAlt);
    myFile.print(",");
    myFile.println(digitalRead(8));
    myFile.close();
  
    delay(50);
}

-Thanks!

Can you show the contents of the file you write out? That should tell you what's going on.

Also... this line...

Should probably be changed to...

    myFile.print(A);

otherwise you're not writing out the data that you made your decision on.

Also... based on your code the "eject" will only happen when the height is decreasing (coming down)... is that what you want?

thanks for the response.

good observation on the print, i should have noticed that earlier haha :stuck_out_tongue:

The eject is supposed to happen when the rocket is coming down as it will be controlling the main parachute release.

I have not yet flown this on a rocket but i have forced a vacuum on it to simulate a flight i will attach that file. as you can see from the code it records the status of pin 8.

Time, Altitude m, Apogee m, Ejection
636,-0.43,0,0
717,1.07,1,0
799,1.07,1,0
901,1.07,1,0
982,1.07,1,0
1062,1.07,1,0
1143,1.07,1,0
1224,1.07,1,0
1305,1.01,1,0
1385,1.01,1,0
1466,1.01,1,0
1547,1.01,1,0
1628,1.01,1,0
1709,1.01,1,0
1789,1.01,1,0
1870,1.01,1,0
1951,0.96,1,0
2032,0.96,1,0
2113,0.96,1,0
2194,0.96,1,0
2275,0.96,1,0
2356,0.96,1,0
2437,0.96,1,0
2518,0.92,1,0
2598,0.92,1,0
2679,0.92,1,0
2760,0.92,1,0
2841,0.92,1,0
2922,0.92,1,0
3003,0.92,1,0
3084,29.58,29,0
3165,29.58,29,0
3273,29.58,29,0
3355,29.58,29,0
3436,29.58,29,0
3517,29.58,29,0
3598,29.58,29,0
3679,98.72,98,0
3761,98.72,98,0
3842,98.72,98,0
3922,98.72,98,0
4003,98.72,98,0
4084,98.72,98,0
4165,98.72,98,0
4246,170.64,170,0
4327,170.64,170,0
4408,170.64,170,0
4489,170.64,170,0
4570,170.64,170,0
4651,170.64,170,0
4731,170.64,170,0
4812,239.86,239,0
4894,239.86,239,0
4975,239.86,239,0
5056,239.86,239,0
5137,239.86,239,0
5218,239.86,239,0
5299,239.86,239,0
5380,239.86,239,0
5460,305.29,305,0
5570,305.29,305,0
5651,305.29,305,0
5733,305.29,305,0
5814,305.29,305,0
5896,305.29,305,0
5977,366.98,366,0
6057,366.98,366,0
6138,366.98,366,0
6219,366.98,366,0
6300,366.98,366,0
6381,366.98,366,0
6462,366.98,366,0
6543,366.98,366,0
6625,425.02,425,0
6706,425.02,425,0
6787,425.02,425,0
6867,425.02,425,0
6948,425.02,425,0
7029,425.02,425,0
7110,425.02,425,0
7192,479.41,479,0
7273,479.41,479,0
7354,479.41,479,0
7435,479.41,479,0
7516,479.41,479,0
7597,479.41,479,0
7677,479.41,479,0
7788,530.56,530,0
7869,530.56,530,0
7950,530.56,530,0
8031,530.56,530,0
8112,530.56,530,0
8193,530.56,530,0
8273,530.56,530,0
8354,578.53,578,0
8436,578.53,578,0
8517,578.53,578,0
8598,578.53,578,0
8679,578.53,578,0
8761,578.53,578,0
8842,578.53,578,0
8923,613.31,613,0
9004,613.31,613,0
9084,613.31,613,0
9165,613.31,613,0
9246,613.31,613,0
9327,613.31,613,0
9409,613.31,613,0
9490,614.75,613,0
9571,614.75,614,0
9652,614.75,614,0
9733,614.75,614,0
9814,614.75,614,0
9894,614.75,614,0
10012,614.75,614,0
10153,598.70,614,0
10255,598.70,614,0
10341,598.70,614,0
10428,598.70,614,0
10514,598.70,614,0
10599,598.70,614,0
10685,574.27,614,0
10771,574.27,614,0
10857,574.27,614,0
10942,574.27,614,0
11028,574.27,614,0
11114,574.27,614,0
11200,574.27,614,0
11285,545.70,614,0
11371,545.70,614,0
11457,545.70,614,0
11543,545.70,614,0
11628,545.70,614,0
11714,545.70,614,0
11800,545.70,614,0
11886,515.47,614,0
11971,515.47,614,0
12057,515.47,614,0
12143,515.47,614,0
12229,515.47,614,0
12323,515.47,614,0
12409,515.47,614,0
12495,485.03,614,0
12580,485.03,614,0
12666,485.03,614,0
12752,485.03,614,0
12855,485.03,614,0
12940,485.03,614,0
13026,455.17,614,0
13113,455.17,614,0
13199,455.17,614,0
13284,455.17,614,0
13370,455.17,614,0
13456,455.17,614,0
13542,455.17,614,0
13628,426.54,614,0
13713,426.54,614,0
13799,426.54,614,0
13885,426.54,614,0
13971,426.54,614,0
14056,426.54,614,0
14142,426.54,614,0
14228,399.43,614,0
14314,399.43,614,0
14400,399.43,614,0
14494,399.43,614,0
14580,399.43,614,0
14666,399.43,614,0
14752,399.43,614,0
14837,374.08,614,0
14923,374.08,614,0
15009,374.08,614,0
15095,374.08,614,0
15181,374.08,614,0
15266,374.08,614,0
15352,374.08,614,0
15456,350.36,614,0
15541,350.36,614,0
15627,350.36,614,0
15713,350.36,614,0
15800,350.36,614,0
15886,350.36,614,0
15972,328.14,614,0
16057,328.14,614,0
16143,328.14,614,0
16229,328.14,614,0
16315,328.14,614,0
16401,328.14,614,0
16487,328.14,614,0
16572,307.35,614,0
16658,307.35,614,0
16753,307.35,614,0
16839,307.35,614,0
16924,307.35,614,0
17010,307.35,614,0
17096,307.35,614,0
17182,287.95,614,0
17268,287.95,614,0
17354,287.95,614,0
17439,287.95,614,0
17525,287.95,614,0
17611,287.95,614,0
17697,287.95,614,0
17783,269.77,614,0
17868,269.77,614,0
17954,269.77,614,0
18058,269.77,614,0
18144,269.77,614,0
18229,269.77,614,0
18315,252.75,614,0
18401,252.75,614,0
18488,252.75,614,0
18573,252.75,614,0
18659,252.75,614,0
18745,252.75,614,0
18831,252.75,614,0
18916,236.81,614,0
19035,236.81,614,0
19121,236.81,614,0
19206,236.81,614,0
19292,236.81,614,0
19378,236.81,614,0
19464,236.81,614,0
19549,221.92,614,0
19635,221.92,614,0
19721,221.92,614,0
19807,221.92,614,0
19892,221.92,614,0
19978,221.92,614,0
20064,207.94,614,0
20150,207.94,614,0
20236,207.94,614,0
20321,207.94,614,0
20407,207.94,614,0
20493,207.94,614,0
20579,207.94,614,0
20682,194.84,614,0
20768,194.84,614,0
20853,194.84,614,0
20939,194.84,614,0
21025,194.84,614,0
21111,194.84,614,0
21207,194.84,614,0
21293,182.59,614,0
21379,182.59,614,0
21465,182.59,614,0
21551,182.59,614,0
21636,182.59,614,0
21722,182.59,614,0
21808,182.59,614,0
21894,171.16,614,0
21980,171.16,614,0
22066,171.16,614,0
22151,171.16,614,0
22237,171.16,614,0
22323,171.16,614,0
22409,160.44,614,0
22495,160.44,614,0
22581,160.44,614,0
22666,160.44,614,0
22752,160.44,614,0
22838,160.44,614,0
22924,160.44,614,0
23010,150.37,614,0
23096,150.37,614,0
23199,150.37,614,0
23284,150.37,614,0
23370,150.37,614,0
23465,150.37,614,0
23550,150.37,614,0
23636,140.93,614,0
23723,140.93,614,0
23809,140.93,614,0
23896,140.93,614,0
23982,140.93,614,0
24067,140.93,614,0
24153,140.93,614,0
24239,132.08,614,0
24325,132.08,614,0
24411,132.08,614,0
24496,132.08,614,0
24582,132.08,614,0
24668,132.08,614,0
24754,132.08,614,0
24840,123.81,614,0
24926,123.81,614,0
25011,123.81,614,0
25097,123.81,614,0
25183,123.81,614,0
25269,123.81,614,0
25355,116.06,614,0
25441,116.06,614,0
25526,116.06,614,0
25621,116.06,614,0
25725,116.06,614,0
25811,116.06,614,0
25896,116.06,614,0
25982,108.77,614,0
26068,108.77,614,0
26155,108.77,614,0
26241,108.77,614,0
26326,108.77,614,0
26412,108.77,614,0
26498,108.77,614,0
26585,101.94,614,0
26670,101.94,614,0
26756,101.94,614,0
26842,101.94,614,0
26928,101.94,614,0
27014,101.94,614,0
27099,101.94,614,0
27185,95.59,614,0
27271,95.59,614,0
27357,95.59,614,0
27443,95.59,614,0
27528,95.59,614,0
27614,95.59,614,0
27700,95.59,614,0
27786,89.65,614,0
27904,89.65,614,0
27990,89.65,614,0
28075,89.65,614,0
28161,89.65,614,0
28265,89.65,614,0
28351,84.04,614,0
28437,84.04,614,0
28523,84.04,614,0
28608,84.04,614,0
28694,84.04,614,0
28780,84.04,614,0
28865,84.04,614,0
28951,78.77,614,0
29037,78.77,614,0
29123,78.77,614,0
29208,78.77,614,0
29295,78.77,614,0
29381,78.77,614,0
29467,78.77,614,0
29552,73.89,614,0
29638,73.89,614,0
29724,73.89,614,0
29810,73.89,614,0
29895,73.89,614,0
29981,73.89,614,0
30067,69.29,614,0
30153,69.29,614,0
30247,69.29,614,0
30333,69.29,614,0
30419,69.29,614,0
30504,69.29,614,0
30590,69.29,614,0
30676,64.96,614,0
30781,64.96,614,0
30867,64.96,614,0
30952,64.96,614,0
31038,64.96,614,0
31124,64.96,614,0
31210,64.96,614,0
31295,60.92,614,0
31381,60.92,614,0
31467,60.92,614,0
31553,60.92,614,0
31639,60.92,614,0
31724,60.92,614,0
31810,60.92,614,0
31896,57.13,614,0
31983,57.13,614,0
32068,57.13,614,0
32154,57.13,614,0
32240,57.13,614,0
32326,57.13,614,0
32411,57.13,614,0
32497,53.61,614,0
32592,53.61,614,0
32677,53.61,614,0
32763,53.61,614,0
32849,53.61,614,0
32935,53.61,614,0
33021,50.30,614,0
33106,50.30,614,0
33192,50.30,614,0
33334,50.30,614,0
33435,50.30,614,0
33521,50.30,614,0
33607,47.19,614,0
33692,47.19,614,0
33778,47.19,614,0
33864,47.19,614,0
33950,47.19,614,0
34036,47.19,614,0
34122,47.19,614,0
34207,44.29,614,0
34293,44.29,614,0
34379,44.29,614,0
34465,44.29,614,0
34551,44.29,614,0
34636,44.29,614,0
34722,44.29,614,0
34808,41.56,614,0
34894,41.56,614,0
34989,41.56,614,0
35075,41.56,614,0
35161,41.56,614,0
35247,41.56,614,0
35332,41.56,614,0
35419,38.96,614,0
35505,38.96,614,0
35591,38.96,614,0
35676,38.96,614,0
35762,38.96,614,0
35848,38.96,614,0
35934,38.96,614,0
36037,36.56,614,0
36122,36.56,614,0
36208,36.56,614,0
36294,36.56,614,0
36380,36.56,614,0
36466,36.56,614,0
36552,34.29,614,0
36637,34.29,614,0
36723,34.29,614,0
36809,34.29,614,0
36895,34.29,614,0
36981,34.29,614,0
37067,34.29,614,0
37152,32.18,614,0
37238,32.18,614,0
37357,32.18,614,0
37443,32.18,614,0
37528,32.18,614,0
37614,32.18,614,0
37700,32.18,614,0
37786,30.19,614,0
37872,30.19,614,0
37958,30.19,614,0
38043,30.19,614,0
38130,30.19,614,0
38216,30.19,614,0
38302,30.19,614,0
38388,28.31,614,0

as you can see from the recorded data pin 8 remains 0

and i just realized i had edited the code after this vacuum simulation, for the data i am posting the code at the time read:


  static int maxAlt = 0; //Apogee code
  int A = bmp.readAltitude(Pressure);
  if ( A > maxAlt ) {
    maxAlt = A;
    if (( A < maxAlt ) and ( A <= Eject )) { //Ejection charge firing
       digitalWrite(8, HIGH);
    }
  }

here is a graph of whats happening to help visualize

image

The code i had posted results in pin 8 being active as soon as i turn the arduino on so the last column would read 1 the entire time.

If needed i can record that data quickly and post it as well

I also got some feedback from the rocketry discord, they're saying that i need the loop to start only once the rocket starts ascending because if the altitude measurement is noisy it will guarantee that ejection will fire prematurely. unfortunately I'm not that good at coding so i dont really know how i would do that..... any tips?

OK.. that might explain it as this code will never set pin 8 HIGH... as if A>maxAlt is true then A<maxAlt can never be true.

The original code looks like it would do what you want.

ok that makes sense, im also getting tips from the rocketry discord as we speak. they're saying i should have a way to make it detect launch before starting the loop or eject (pin 8) will activate prematurely. im not sure how i would do that, would you happen to know any resources that could help?

I was also told that pressure* should be a global stored as a float

I don't think so. maxAlt will be 0 before launch... and you only set pin 8 when A<maxAlt... so that won't happen until maxAlt > 0... that is after launch.

Yes probably all your variables should be floats, as this seems to be what is returned by BMP280 library... but it doesn't make that much difference if you use int for everything. The logic is still the same.

I solved the issue thanks for all the help

  if (( A+5 < maxAlt ) and ( A <= Eject )) { //Ejection charge firing
    digitalWrite(8, HIGH);
  }

it needed a buffer for noise 5meters is a pretty large buffer so it shouldnt go off while sitting on the pad as it will only be used in small rockets

Cool... I actually wrote that in the post above... then removed it to keep it simple... but yes a buffer will stop any rogue measurements from causing problems. Good luck with the real launch :slight_smile: