Here we go now. Idaho thank you for making me realize I needed to switch to floats not int. The code also was wrong for the BMP280. For some reason it interchanges "BMP" and "BMP280" with reckless abandon. THIS code compiles and works, it shows up on the serial monitor and the sensors respond to my physical input.
I also switched to an "if" statement so the servo wont move unless the current altitude is less than the previous altitude AND the current altitude is lower than a specific altitude. I set that specific altitude to 530 so I could test it holding it over my head in my living room.
Idaho I see what you're saying though about having a rolling average and one reading just happening to be lower than the previous one which would deploy the chute on the ascent. I haven't addressed that yet but I'm thinking I can also add a timer to my "if" statement. Balloons have like a 2 hour ascent so I could lock it out with an easy "millis()" function. I would have to time it with my calculated ascent speed and how long it takes to get to the desired altitude. I could ballpark it and over estimate by like 10 minutes and still be fine.
The eagle eyed among you will also see that I added an accelerometer and gyroscope to it. Thats just for fun.
#include <Adafruit_LSM6DS33.h>
#include <Adafruit_BMP280.h>
#include <Servo.h>
#include <Wire.h>
#include <SPI.h>
Adafruit_LSM6DS33 lsm6ds33; // accelerometer, gyroscope
Adafruit_BMP280 bmp; // temperautre, barometric pressure
Servo servo; // create servo object to control a servo
float temperature, pressure, altitude;
float accel_x, accel_y, accel_z;
float gyro_x, gyro_y, gyro_z;
float A;
void setup() {
lsm6ds33.begin_I2C();
servo.attach(5); // attaches the servo on pin whatever to the servo object
bmp.begin();
servo.write(90);
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. */
A = 1018.75; //hPa at sea level on each day. 538m is home altitude for reference
}
void loop() {
pressure = bmp.readPressure();
altitude = bmp.readAltitude(A);
sensors_event_t accel;
sensors_event_t gyro;
sensors_event_t temp;
lsm6ds33.getEvent(&accel, &gyro, &temp);
accel_x = accel.acceleration.x;
accel_y = accel.acceleration.y;
accel_z = accel.acceleration.z;
gyro_x = gyro.gyro.x;
gyro_y = gyro.gyro.y;
gyro_z = gyro.gyro.z;
Serial.print("Acceleration: X:");
Serial.print(accel_x);
Serial.print(" Y:");
Serial.print(accel_y);
Serial.print(" Z:");
Serial.print(accel_z);
Serial.println(" m/s^2");
delay(250);
Serial.print("Barometric pressure: ");
Serial.print(pressure);
Serial.print(" hPa Altitude: ");
Serial.print(altitude);
Serial.println(" m");
if (bmp.readAltitude(A) < altitude && bmp.readAltitude(A) < 530) {
servo.write(90); // moves servo to 90 degrees
Serial.println("Deploy Chute");
} else {
Serial.println("No Deploy");
}
}