Problems with Arduino datalogging

Hello,
I have been developing a model rocket flight computer and have run into a problem with data logging. Some values in the file are insanely big like altitude, altitudeAGL, and temp. Why?
The file:

Time, X_ACC, Y_ACC, Z_ACC, X_GYRO, Y_GYRO, Z_GYRO, System_State, altitude, altitudeAGL, Pressure, Temp
4087,16,16164,-104,-188,46,-94,0,-536870912,1081485777,0,-1073329600
4251,-56,16360,-288,-253,46,-87,0,-536870912,1081485777,0,-1073329600
4414,44,16104,-240,-246,30,-85,0,536870912,1081485603,0,-1073307232
4577,128,16220,-300,-269,14,-106,0,536870912,1081485603,0,-1073307232
4741,152,16272,-308,-261,34,-97,0,536870912,1081485603,0,-1073307232
4904,8,16904,12332,-479,2733,-211,0,-2147483648,1081485526,0,-1073297424
5066,-396,16828,-2160,-650,-1327,24,0,-2147483648,1081485526,0,-1073297424
5230,64,16300,180,-398,-1331,-10,0,-2147483648,1081485526,0,-1073297424
5393,116,16244,-256,-447,6,-92,0,-1073741824,1081485547,0,-1073300144
5557,-56,16336,-104,-196,73,-80,0,-1073741824,1081485547,0,-1073300144
5720,-148,16056,-624,45,-1706,63,0,-1073741824,1081485547,0,-1073300144
5883,-1184,17320,1056,-1181,-9023,467,0,-2147483648,1081485381,0,-1073278864
6047,144,18632,-1208,-19455,-13386,-7410,0,-2147483648,1081485381,0,-1073278864
6209,-1008,13180,4300,-3171,-4019,14142,0,-2147483648,1081485381,0,-1073278864
6372,3772,15880,4584,4932,3372,-7082,0,-2147483648,1081485462,0,-1073289232
6536,-3300,13504,-600,651,-310,-4183,0,-2147483648,1081485462,0,-1073289232
6699,-84,15588,2872,-5409,-6715,3715,0,-2147483648,1081485462,0,-1073289232
6863,3756,13328,2724,-3010,-2074,1263,0,-2147483648,1081485462,0,-1073289232
7026,1668,15268,5100,-7072,-5330,899,0,-1610612736,1081485349,0,-1073274784
7189,524,15792,9888,-2900,1619,2438,0,-1610612736,1081485349,0,-1073274784
7351,4456,14616,6196,500,518,-489,0,-1610612736,1081485349,0,-1073274784
7515,4856,14368,6460,591,301,-894,0,1610612736,1081483412,0,-1073026816
7678,5272,15352,6912,2888,2702,-1779,0,1610612736,1081483412,0,-1073026816
7842,3348,15044,5880,-820,1084,-506,0,1610612736,1081483412,0,-1073026816
8005,2824,14752,6068,-771,-495,226,0,1610612736,1081483574,0,-1073047552
8169,4276,15296,5864,-1431,2007,-2672,0,1610612736,1081483574,0,-1073047552
8332,-880,18428,5660,4944,5493,-19010,0,1610612736,1081483574,0,-1073047552
8494,-6704,13196,3280,32767,20420,-1832,0,1610612736,1081483672,0,-1073060096
8658,1120,15404,-5616,1937,-5,14,0,1610612736,1081483672,0,-1073060096
8821,472,32767,-684,15772,19442,-5730,0,1610612736,1081483672,0,-1073060096
8984,-2748,26800,-2840,-20500,3960,-16385,1,1073741824,1081483800,0,-1073076464
9148,-4612,-18592,-5864,1624,527,15003,1,1073741824,1081483800,0,-1073076464
9311,-13164,-17256,-6164,1000,-6143,-5345,1,1073741824,1081483800,0,-1073076464
9474,2744,10796,10264,2791,-15808,-1057,1,1610612736,1081483851,0,-1073083008
9637,-760,16768,11468,-20592,-6521,3414,1,1610612736,1081483851,0,-1073083008
9801,1280,14496,6944,-885,-2251,-3926,1,1610612736,1081483851,0,-1073083008
9964,1176,16808,6324,3735,3883,1363,1,0,1081484056,0,-1073109200
10128,548,15696,5200,-51,-246,-303,1,0,1081484056,0,-1073109200
10291,792,14744,7208,-1379,-2736,-841,1,0,1081484056,0,-1073109200
10455,1480,14848,6316,-3187,-537,-1826,1,0,1081484056,0,-1073109200
10617,860,14944,7268,-223,415,-23,1,1610612736,1081483981,0,-1073099648
10781,1344,15348,6232,8584,6165,-135,1,1610612736,1081483981,0,-1073099648
10944,-80,16548,2700,6586,11561,-4667,1,1610612736,1081483981,0,-1073099648
11108,-1280,19244,852,6691,4215,3764,1,0,1081484105,0,-1073115472
11271,276,16492,656,-279,1644,785,1,0,1081484105,0,-1073115472
11435,456,15316,112,140,4789,-382,1,0,1081484105,0,-1073115472
11598,132,16224,0,-510,465,-116,1,1610612736,1081484143,0,-1073120384
11760,24,16252,200,-406,58,-119,1,1610612736,1081484143,0,-1073120384
11923,0,16328,96,-200,73,-96,1,1610612736,1081484143,0,-1073120384
12087,344,16036,-220,-489,-992,-125,1,-2147483648,1081484324,0,-1073143568
12250,712,16552,380,-372,-854,-101,1,-2147483648,1081484324,0,-1073143568
12414,716,16380,-216,-243,-1422,32,1,-2147483648,1081484324,0,-1073143568
12577,-1564,17048,1052,-3373,-4184,-2177,1,-1610612736,1081484309,0,-1073141664
12741,-396,16948,2364,-6696,-3344,-2852,1,-1610612736,1081484309,0,-1073141664
12915,-1396,12424,4984,1105,-8779,5588,1,-1610612736,1081484309,0,-1073141664
13079,-1388,13760,2756,18293,17234,-433,1,1610612736,1081484322,0,-1073143296
13245,2488,16652,4236,-2821,-5606,-1384,1,1610612736,1081484322,0,-1073143296
13409,-752,15928,9672,-2533,-260,947,1,1610612736,1081484322,0,-1073143296
13575,2540,15664,5896,-2243,1634,1609,1,-536870912,1081484281,0,-1073138112
13739,2352,14652,5692,-2157,285,-1109,1,-536870912,1081484281,0,-1073138112
13905,1880,14648,7252,149,1422,841,1,-536870912,1081484281,0,-1073138112
14068,2536,15348,7204,-610,439,-271,1,536870912,1081484644,0,-1073184480
14234,2884,14504,7508,580,-38,-910,1,536870912,1081484644,0,-1073184480
14397,2256,14804,6848,-34,-15,-786,1,536870912,1081484644,0,-1073184480
14563,2332,14728,7716,-294,23,-113,1,536870912,1081484644,0,-1073184480
14727,2764,15300,7232,417,303,-316,1,1073741824,1081485556,0,-1073301232
14893,3064,15020,5432,1158,1768,-1190,1,1073741824,1081485556,0,-1073301232
15057,1792,15340,6132,1613,639,-1068,1,1073741824,1081485556,0,-1073301232
15222,0,15608,4212,3214,437,-185,1,-1610612736,1081485609,0,-1073308064
15385,-176,16656,2288,1990,518,-93,1,-1610612736,1081485609,0,-1073308064
15551,-616,15456,824,1285,530,-41,1,-1610612736,1081485609,0,-1073308064
15715,392,16664,-144,456,-87,-229,1,0,1081485243,0,-1073261136
15881,440,16296,284,-212,-418,-67,1,0,1081485243,0,-1073261136
16045,-40,16068,456,-409,182,-68,1,0,1081485243,0,-1073261136
16209,-600,16768,-708,-2477,-4273,-2,1,536870912,1081485098,0,-1073242592
16372,108,16372,828,468,67,100,1,536870912,1081485098,0,-1073242592
16536,36,16384,-36,-330,41,-118,1,536870912,1081485098,0,-1073242592

The datalogging file:

#include <Arduino.h>
#include <SD.h>

File forzaFlightData;
const char filename[] = "forzaDataLog.txt";
extern int pressure;
extern int temp;
extern int altitude;
char buffer[128];
bool hasRun = false;
void SDInit()
{
    SD.begin(BUILTIN_SDCARD);
    if (!SD.begin(BUILTIN_SDCARD))
    {
        Serial.print("The SD card failed to initalize");
        while (1)
            ;
    }
    Serial.println("The SD card has been initalized.");
    if (SD.exists(filename))
    {
        SD.remove(filename);
    }
    forzaFlightData = SD.open(filename, FILE_WRITE);
    if (!hasRun)
    {
        if (forzaFlightData)
        {

            forzaFlightData.println("Time, X_ACC, Y_ACC, Z_ACC, X_GYRO, Y_GYRO, Z_GYRO, System_State, altitude, altitudeAGL, Pressure, Temp");
        }
        hasRun = true;
    }
    forzaFlightData.close();
}

void dataLog()
{
    forzaFlightData = SD.open(filename, FILE_WRITE);
    if (forzaFlightData)
    {
        sprintf(buffer, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", millis(), ax, ay, az, gx, gy, gz, systemState, BMPAltitudeUpdate(), BMPAltitudeUpdate() - 357.4, BMPPressureUpdate(), BMPTempUpdate());
        forzaFlightData.println(buffer);
        // Serial.println(millis()); // just a lil debugging :)
        delay(100);
    }
    forzaFlightData.close();
}
void stopDataLog()
{
    forzaFlightData.close();
}

the BMP280 file:

#include <Arduino.h>
#include <Adafruit_BMP280.h>
#include <SPI.h>
#include <Wire.h>
#define BMP_SCK (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS (10)
Adafruit_BMP280 bmp(BMP_CS);
extern int systemState;
bool hitApogee;
extern int altitudeAGL;
void BMPInit()
{
    if (!bmp.begin())
    {
        Serial.print("Connection To Barometer Failed...");
    }
    Serial.println("Succesfully connected to BMP280...");
    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);
}

void printVals_BARO_x10()
{
    for (int i = 0; i < 10; i++)
    {
        Serial.println(bmp.readAltitude(1017.0));
        delay(500);
    }
}

float BMPPressureUpdate()
{
    return bmp.readPressure();
}
float BMPTempUpdate()
{
    return bmp.readTemperature();
}
float BMPAltitudeUpdate()
{
    return bmp.readAltitude(1017.0); // Adjust to local forecast!!
}
bool detectApogee()
{
    int currentAltitude = 0;
    if (systemState == 1)
    {
        int lastAltitude = currentAltitude;
        currentAltitude = BMPAltitudeUpdate();
        if (currentAltitude > lastAltitude)
        {
            return false;
        }
        else
        {
            systemState++;
            hitApogee = true;
            return true;
        }
    }
}
void detectLanding()
{
    if (systemState == 3 && hitApogee && altitudeAGL < 5)
    {
        systemState++;
    }
}

And the main.cpp file:

#include <Arduino.h>
#include <IMU6050.h>
#include <BARO280.h>
#include <GNC.h>
#include <State_Indication.h>
#include <System.h>
#include <pyro.h>
#include <datalog.h>
#include <SITL.h>
#include <TVC.h>
int systemState = 0;
extern int altitudeAGL = 0;
extern int altitude = 0;
extern int temp = 0;
extern int pressure = 0;
void setup()
{
    Serial.begin(9600);
    mpuInit();
    BMPInit();
    SDInit();
    TVCInitalize();
}

void loop()
{
    timeKeeper();
    State_Indication();
    navUpdate();
    dataLog();
    TVCCenter();

    if (systemState == 0) // Ground idle
    {
        allPyrosLow();
        detectLiftoff();
    }
    if (systemState == 1) // Ascent
    {
        detectApogee();
    }
    if (systemState == 2) // Ballistic descent
    {
        detectChutes();
    }
    if (systemState == 3) // Under chutes
    {
        detectLanding();
    }
    delay(50);
}

The insanely big values probably result from the print() method deciding that some variable should be converted as "long".

Big hint: these are always the 9th, 10th, and 12th numbers on a line, and the 11th is always zero.

Please explain how sprintf() deals with float values, in this line:

sprintf(buffer, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", millis(), ax, ay, az, gx, gy, gz, systemState, BMPAltitudeUpdate(), BMPAltitudeUpdate() - 357.4, BMPPressureUpdate(), BMPTempUpdate());

BTW: what program is printing out the data, posted as "the file"?

%f not %d for floats

Except floats are not enabled for Arduino sprintf(). Use dtostrf() to convert the float to a string then %s for the placeholder in sprintf() format string.

I did not know that... thanks.

sprintf "%f" is enabled for some MCUs that can be programmed using the Arduino IDE, but the OP forgot to mention whatever it is they are using.

Turns out, this problem was caused by my poor understanding of the sprintf function.

Please explain how sprintf() deals with float values, in this line:

The answer is, it doesn't. That was the problem. I needed to use %f.
For anyone wondering, I'm using a Teensy 4.1 microcontroller from PJRC.
Thanks for the help!

Fortunately, the Teensy series is one in which sprintf("%f") is supported.

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