Troubleshooting code to make a variometer

Good evening to all
I'm new to the community.
I have a paraglider and I'm trying to make a variometer with the SparkFun's sensor MPL3115A2. I have modified the following code from SparkFun, for my Arduino Mega 2560 r3 board. I noticed that the difference is not constant when the sensor is stationary, and I get no indications when the sensor is immobilized after moving it up or down. Can or have some one tryed my setup, or can some one help me figure out what's wrong with the code I'v wrote?
Thanks in advance

Code:

#include <Wire.h>
#include "SparkFunMPL3115A2.h"

//Create an instance of the object
MPL3115A2 myPressure;

float previousAlt, currentAlt, da;
unsigned long previousTime, currentTime, dT;

void setup()
{
  Wire.begin();        // Join i2c bus
  Serial.begin(9600);  // Start serial for output

  myPressure.begin(); // Get sensor online

  //Configure the sensor
  myPressure.setModeAltimeter(); // Measure altitude above sea level in meters
  //myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa

  myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
  myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
  //float altitude = myPressure.readAltitude();
  previousAlt = myPressure.readAltitude(); // getting initial altitude measurement
}

void loop()
{
  // using millis function to get a time difference for the next altitude measurement
  previousTime = millis(); 
  delay(150);
  currentTime = millis();
  dT = (currentTime - previousTime); // time difference
  //Serial.print(dT); // code to check that actually there is a dT
  //Serial.print("   "); // just a separating space
  if(dT >= 149){
    currentAlt = myPressure.readAltitude();
    da = currentAlt - previousAlt;
    Serial.println(da);
  }

Hi @grandflyover,

there seem to be at least some glitches with your time scheduling and the calculation of the altitude differences:

void loop()
{
  // using millis function to get a time difference for the next altitude measurement
  previousTime = millis(); 
  delay(150);
  currentTime = millis();
  dT = (currentTime - previousTime); // time difference
  //Serial.print(dT); // code to check that actually there is a dT
  //Serial.print("   "); // just a separating space
  if(dT >= 149){
    // You do not set previousTime to currentTime here for the next measurement
    currentAlt = myPressure.readAltitude();      
    da = currentAlt - previousAlt;           //  You do not set previousAlt to currentAlt after this
    Serial.println(da);                      // as the recent currentAlt will be the previousAlt in the next calculation
  }

Please feel free to check this (I only post the changed loop() part and the changes I suggest for the declaration to make sure that previousTime and previousAlt start with a value of Zero!):

unsigned long currentTime;
unsigned long previousTime = 0;
int currentAlt;
int previousAlt = 0;

void loop()
{
  // using millis function to get a time difference for the next altitude measurement
  currentTime = millis();
  if(currentTime -  previousTime >= 149){
    previousTime = millis();
    currentAlt = myPressure.readAltitude();
    da = currentAlt - previousAlt;
    previousAlt = currentAlt;
    Serial.println(da);
  }
} 

As you see you do not need a delay() in the loop, as the function

if (currentTime - previousTime >= 149) { ...}

makes sure that you read the data only every 149 msec ...

As a side note, I hope you are only going to use this for logging purposes?

1 Like

That's a valid question :wink: None of this material and software is qualified and validated for flight purposes...

ec2021, thanks a lot for the quick response and correction in the code. Obviously I need more reading. And of course I'm strictly experimenting in my lab (...on the ground. Thanks for the notice!!!)

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