-1

I am programming a bmp280 pressure sensor to deploy a parachute when the altitude starts to decrease on a water rocket but only when it starts to significantly decrease so it doesn’t deploy as soon as altitude decreases how would I detect the number going down but only when it decreases by a large amount?

I tried messing around with if statements to detect when it is decreasing but can not figure out how to only detect significant drops in the alltitude number.

1 Answers1

1

You'll need to store the previous value of the sensor in a global (or static) variable and compare the current measurement with that, to check that the difference is greater than a predefined threshold.

Something like this:

#define threshold 100

int prevValue = 0;

void setup() { 
    // setup code
}


void loop() {
    // Read the current value from the sensor
    int currValue = sensor.read();

    // Init prevValue on first loop
    if (prevValue = 0) {
        prevValue = currValue;
    }

    // Check the deploy condition
    if ((currValue - prevValue) > threshold) {
        // deploy parachute
    }

    // Update for the next loop
    prevValue = currValue;

    delay(5000);
}
Gabriel T
  • 23
  • 5