I'm working on a water rocket and i'm using a BMP390L (presion sensor) to measure the altitude.
that works.
Now i'd want to use this sensor to release a parachute with a servo motor when the altitude is decreasing. to do so, i've created two variables called altitudeactuelle and altitudeprecedente and a loop to check if the new altitude value is under the old one.
But it doesn't work. altitudeprecedente is always equal to zero.
I think the problem is on lines 84 to 86.
Any help about how fix it?
Code below :
/***************************************************************************
This is a library for the BMP3XX temperature & pressure sensor. Designed specifically to work with the Adafruit BMP388 Breakout ----> http://www.adafruit.com/products/3966
These sensors use I2C or SPI to communicate, 2 or 4 pins are required to interface. Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!
Written by Limor Fried & Kevin Townsend for Adafruit Industries. BSD license, all text above must be included in any redistribution
***************************************************************************/
// Complete project details: https://RandomNerdTutorials.com/arduino-bmp388/
#include <Servo.h>
Servo myservo;
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP3XX.h"
#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11
#define BMP_CS 10
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BMP3XX bmp;
int altitudeprecedente = 1;
int altitudeactuelle = 1;
void setup() {
int altitudeprecedente = bmp.readAltitude(SEALEVELPRESSURE_HPA);
int altitudeactuelle = bmp.readAltitude(SEALEVELPRESSURE_HPA);
myservo.attach(9);
myservo.write (90);
Serial.begin(115200);
Serial.println("Adafruit BMP388 / BMP390 test");
//if (!bmp.begin_I2C()) { // hardware I2C mode, can pass in address & alt Wire
//if (! bmp.begin_SPI(BMP_CS)) { // hardware SPI mode
if (! bmp.begin_SPI(BMP_CS, BMP_SCK, BMP_MISO, BMP_MOSI)) { // software SPI mode
Serial.println("Could not find a valid BMP3 sensor, check wiring!");
while (1);
}
// Set up oversampling and filter initialization
bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
bmp.setOutputDataRate(BMP3_ODR_50_HZ);
if (! bmp.performReading()) {
Serial.println("Failed to perform reading :(");
return;
}
//Serial.print("Temperature = ");
//Serial.print(bmp.temperature);
//Serial.println(" *C");
//Serial.print("Pressure = ");
//Serial.print(bmp.pressure / 100.0);
//Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(bmp.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.println();
delay(2000);
}
void loop() {
if (! bmp.performReading()) {
Serial.println("Failed to perform reading :(");
return;
}
altitudeprecedente = altitudeactuelle;
delay(15);
int altitudeactuelle = bmp.readAltitude(SEALEVELPRESSURE_HPA);
Serial.println(altitudeprecedente);
Serial.println(altitudeactuelle);
//Serial.print("Temperature = ");
//Serial.print(bmp.temperature);
//Serial.println(" *C");
//Serial.print("Pressure = ");
//Serial.print(bmp.pressure / 100.0);
//Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(bmp.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.println();
if (altitudeactuelle < altitudeprecedente) {
myservo.write (180);
}
delay (1000);
}
Here you declare altitudeactuelle as an int. But in the line before you use the same variable... remove int.
It makes a new local variable that shadows your global variable with the same name.
Here you create two new variables local to the setup() function. As far as the compiler is concerned, they are completely different even though humans think they are the same name.
Here you are accessing your global variables, which contain the value 1
Here you are creating yet another new variable, local to loop().
You are accessing your global variables, which are both equal to 1
When to declare a variable, you specify its type
int my_variable;
when you use that variable, you do not specify it's type