# How to determine if a value is decreasing

Hey,
I’m currently working on my first program, which is a flight computer for a rocket. I need to come up with a statement that essentially says “if the current altitude is <= to 300 && that value is decreasing”
So far I have this

while(armed == 4)
{
currentalt = bmp.readAltitude(); //Get the current altitude
adjustedalt = currentalt - groundalt; //Calculate adjusted altitude (altitude above ground level)
gyro_calcs(); //Calculate filtered values for pitch, roll
recordData(); //Data dump

if((adjustedalt <= 300.0 && armed ==4) || (armed > 2 && adjustedalt <= 300 && **********))
{
digitalWrite(mainChute, HIGH);
armed = 5;
Serial.println("Main Chute Deployed @ ");

I also have a similar problem with landing. As of right now it detects a landing when the altitude is within 1 meter of the takeoff altitude, which is unreliable because of elevation changes, therefore I added an or statement, which basically ends the program if the flight time exceeds 10 minutes. However, I would like to say something like “if (altitude <= touchdownalt || current altitude is neither increasing nor decreasing”

This is what I have for that portion:

if((armed == 5 && adjustedalt <= touchdownalt) || (armed == 5 && ((millis() - launchmillis) / 1000) > 600))

I’ll include an attachment of the whole sketch, any help would be greatly appreciated, thank you!

Here is the whole sketch

FCC_V_1_9_3.ino (25.2 KB)

You need to compare an old altitude vs a new altitude to determine if it is decreasing or not. Include that result in your if statement. Track the altitude difference on every pass thru loop( ).

Simple definition of positive and negative numbers

if (prevAlt - currentAlt >0) gives you positive (meaning previous was higher, i.e., it’s falling)

if (prevAlt - currentAlt <0) gives you negative (meaning previous was lower, i.e., it’s climbing)

just remember at the end before starting the loop over again to set prevAlt=currentAlt

Ah, so simple, Thank you!

This is probably a dumb question, but how would I get the previous altitude?

Brandon_m_wilson: This is probably a dumb question, but how would I get the previous altitude?

Save the altitude to a variable each time you read it. This becomes the previous altitude next time you read it and so on.

Thanks!