# Detecting decrease (apogee)

**URL:** <https://forum.arduino.cc/t/detecting-decrease-apogee/909834>\
**Category:** Programming\
**Created:** [September 28, 2021, 3:24pm UTC](https://forum.arduino.cc/t/detecting-decrease-apogee/909834 "2021-09-28T15:24:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![yarneman](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@yarneman](https://forum.arduino.cc/u/yarneman)\
**Post date:** [September 28, 2021, 3:24pm UTC](https://forum.arduino.cc/t/detecting-decrease-apogee/909834/1 "2021-09-28T15:24:45Z")

</div>

Dear arduino users,

I am working on a rocket project where a parachute should be deployed when it has hit apogee.  
I have a working script which measures the pressure (BMP180) and converts this to relative height. A kalman filter is used to filter out the noise. My idea was that as soon apogee (maximum height) would have been reached and the height would decrease a parachute can be deployed. This was tried with the following line:  
if (estimated\_altitude\>2 && estimated\_altitude--)  
{Serial.println("descending");  
}

I tried to see if this would work and walked up the stairs and almost as soon I was above 2 meters it would print 'descending', but I was still walking up the stairs and not down the stairs. Can somebody help me how to create a sort of if current\_value \< previous\_value then deploy parachute?

Some clearification:

estimated\_alittude is the height estimated using the Kalman filter and apogee should be based on this number.

This is the code that has been used:

#include \<SimpleKalmanFilter.h\>  
#include \<SFE\_BMP180.h\>

SimpleKalmanFilter pressureKalmanFilter(1, 1, 0.01);

SFE\_BMP180 pressure;

// Serial output refresh time  
const long SERIAL\_REFRESH\_TIME = 100;  
long refresh\_time;

float baseline; // baseline pressure

double getPressure() {  
char status;  
double T,P;  
status = pressure.startTemperature();  
if (status != 0) {  
delay(status);  
status = pressure.getTemperature(T);  
if (status != 0) {  
status = pressure.startPressure(3);  
if (status != 0) {  
delay(status);  
status = pressure.getPressure(P,T);  
if (status != 0) {  
return(P);  
}  
}  
}  
}  
}

void setup() {

Serial.begin(115200);

// BMP180 Pressure sensor start  
if (!pressure.begin()) {  
Serial.println("BMP180 Pressure Sensor Error!");  
while(1); // Pause forever.  
}  
baseline = getPressure();

}

void loop() {

float p = getPressure();  
float altitude = pressure.altitude(p,baseline);  
float estimated\_altitude = pressureKalmanFilter.updateEstimate(altitude);

if (millis() \> refresh\_time) {  
Serial.print(altitude,6);  
Serial.print(",");  
Serial.print(estimated\_altitude,6);  
Serial.println();  
refresh\_time=millis()+SERIAL\_REFRESH\_TIME;  
}

if (estimated\_altitude\>2 && estimated\_altitude--)  
{Serial.println("descending");  
}

}

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [September 28, 2021, 3:32pm UTC](https://forum.arduino.cc/t/detecting-decrease-apogee/909834/2 "2021-09-28T15:32:43Z")

</div>

> [@yarneman](#):
>
> if (estimated\_altitude\>2 && estimated\_altitude--)  
> {Serial.println("descending");

This is wrong. Think again what you want to do.

You want to compare the value to the last time you took a reading.

---

<div class="post-metadata">

**Author:** ![yarneman](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@yarneman](https://forum.arduino.cc/u/yarneman)\
**Post date:** [September 28, 2021, 3:34pm UTC](https://forum.arduino.cc/t/detecting-decrease-apogee/909834/3 "2021-09-28T15:34:12Z")

</div>

Exactly, I want to compare the current reading to the last time reading. I am quite new to Arduino. How would you do this?

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 28, 2021, 3:38pm UTC](https://forum.arduino.cc/t/detecting-decrease-apogee/909834/4 "2021-09-28T15:38:17Z")

</div>

In the old days we used a mercury switch.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [September 28, 2021, 3:43pm UTC](https://forum.arduino.cc/t/detecting-decrease-apogee/909834/5 "2021-09-28T15:43:54Z")

</div>

> [@yarneman](#):
>
> How would you do this?

You would have a global variable called perhaps "lastAltitude" and use that to compare it to the current altitude. Then after the comparison update this "lastAltitude" variable to the latest reading.  
Note a global variable is one declared outside of any function.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [September 28, 2021, 3:46pm UTC](https://forum.arduino.cc/t/detecting-decrease-apogee/909834/6 "2021-09-28T15:46:40Z")

</div>

> [@LarryD](#):
>
> In the old days we used a mercury switch.

Yes that was before the world got paranoid about mercury.  
It was fascinating stuff to handle.

---

<div class="post-metadata">

**Author:** ![yarneman](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@yarneman](https://forum.arduino.cc/u/yarneman)\
**Post date:** [September 28, 2021, 3:52pm UTC](https://forum.arduino.cc/t/detecting-decrease-apogee/909834/7 "2021-09-28T15:52:05Z")

</div>

I see what you mean. I can see how this would work if you get 10 readings.  
For example altitude[9] \< altitude[10], means increasing  
altitude [9] \> altitude [10], means decreasing and falling.

However, how is this done with 'live' readings?

Sorry for not completely understanding how to do this and being a noob to arduino.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [September 28, 2021, 3:56pm UTC](https://forum.arduino.cc/t/detecting-decrease-apogee/909834/8 "2021-09-28T15:56:05Z")

</div>

> [@yarneman](#):
>
> I see what you mean.

No you don't.

```arduino
if (estimated_altitude < lastReading) {
Serial.println("descending");
}
lastReading = estimated_altitude;

```

---

<div class="post-metadata">

**Author:** ![yarneman](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@yarneman](https://forum.arduino.cc/u/yarneman)\
**Post date:** [September 28, 2021, 4:00pm UTC](https://forum.arduino.cc/t/detecting-decrease-apogee/909834/9 "2021-09-28T16:00:15Z")

</div>

How is lastReading declared?

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [September 28, 2021, 4:02pm UTC](https://forum.arduino.cc/t/detecting-decrease-apogee/909834/10 "2021-09-28T16:02:28Z")

</div>

> [@yarneman](#):
>
> How is lastReading declared

> [@Grumpy\_Mike](#):
>
> Note a global variable is one declared outside of any function.

So anywhere outside of a function, it is normal for this to be just before the setup function.

```arduino
float lastReading = 0;

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 27, 2022, 4:08pm UTC](https://forum.arduino.cc/t/detecting-decrease-apogee/909834/12 "2022-03-27T16:08:12Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
