Larger millis value than expected when writing to sd

Hi,

I’m using the sparkfun 9DOF stick to caputure motion on my arduino. I’m then writing this information to a HiLetgo sd micro reader. As you can see near the bottom of the code, I’m writing a number of different values per row. The first value is the time, however, the time value is much larger than the actual amount of time that has passed. When I use the millis function simply in the arduino pc app terminal, the print value is as expected? Using the same function on the sd is not?

Here is an example output to the text file (see large values in first column. Should be ~3 seconds between each row):

T Roll Heading Pitch Gx Gy Gz Ax Ay Az Mx My Mz
43123.11 8.58 -32.05 -3.57 7.39 0.62 0.52 0.32 0.76 0.00 0.00 0.00
371823.17 -67.03 -31.86 0.20 0.58 0.38 0.51 0.33 0.76 -0.06 0.23 0.07
700624.23 -67.34 -31.84 0.28 0.60 0.11 0.52 0.34 0.76 -0.06 0.23 0.07
1029323.36 -67.55 -32.00 0.24 0.64 0.46 0.52 0.33 0.76 -0.06 0.23 0.07
1358123.46 -66.30 -32.11 0.34 0.52 0.33 0.52 0.33 0.76 -0.06 0.23 0.08
1686823.42 -68.16 -31.80 0.20 0.74 0.34 0.51 0.33 0.76 -0.05 0.23 0.07
2015623.35 -68.14 -31.75 0.14 0.67 0.34 0.51 0.33 0.76 -0.05 0.23 0.07
2344923.19 -66.15 -31.66 0.45 0.63 0.17 0.52 0.33 0.77 -0.06 0.23 0.07
2673623.25 -67.57 -31.87 0.18 0.87 0.30 0.52 0.33 0.76 -0.06 0.23 0.08

Here is my code, thank you in advance!

#include <Wire.h>
#include <SPI.h>
#include <SparkFunLSM9DS1.h>
#include <SD.h>

File file;
unsigned long time;

LSM9DS1 imu;

#define PRINT_CALCULATED
//#define PRINT_RAW
#define PRINT_SPEED 250 // 250 ms between prints
static unsigned long lastPrint = 0; // Keep track of print time

#define DECLINATION -8.58 // Declination (degrees) in Boulder, CO.

void printGyro();
void printAccel();
void printMag();
void printAttitude(float ax, float ay, float az, float mx, float my, float mz);

void setup(){
Serial.begin(115200);

//-------------

Serial.println(“Initialising SD Card…”);

if(!SD.begin(4)) {
Serial.println(“Initialising SD Failed…”);
return;
}
Serial.println(“Initialising Successful!”);
Serial.println(“Creating File…”);

file = SD.open(“testlog.txt”, FILE_WRITE);

file.print("T ");
file.print("Roll ");
file.print("Heading ");
file.print("Pitch ");

file.print("Gx ");
file.print("Gy ");
file.print("Gz ");

file.print("Ax ");
file.print("Ay ");
file.print("Az ");

file.print("Mx ");
file.print("My ");
file.println("Mz ");

file.close();

Serial.println(“File Created!”);

//--------------

Wire.begin();

if (imu.begin() == false) // with no arguments, this uses default addresses (AG:0x6B, M:0x1E) and i2c port (Wire).
{

while (1);
}
}

void loop()
{
// Update the sensor values whenever new data is available
if ( imu.gyroAvailable() )
{
// To read from the gyroscope, first call the
// readGyro() function. When it exits, it’ll update the
// gx, gy, and gz variables with the most current data.
imu.readGyro();
}
if ( imu.accelAvailable() )
{
// To read from the accelerometer, first call the
// readAccel() function. When it exits, it’ll update the
// ax, ay, and az variables with the most current data.
imu.readAccel();
}
if ( imu.magAvailable() )
{
// To read from the magnetometer, first call the
// readMag() function. When it exits, it’ll update the
// mx, my, and mz variables with the most current data.
imu.readMag();
}

if ((lastPrint + PRINT_SPEED) < millis())
{
printGyro(); // Print “G: gx, gy, gz”
printAccel(); // Print “A: ax, ay, az”
printMag(); // Print “M: mx, my, mz”
// Print the heading and orientation for fun!
// Call print attitude. The LSM9DS1’s mag x and y
// axes are opposite to the accelerometer, so my, mx are
// substituted for each other.
printAttitude(imu.ax, imu.ay, imu.az,
-imu.my, -imu.mx, imu.mz);
Serial.println();

lastPrint = millis(); // Update lastPrint time
}
}

void printGyro()
{
// Now we can use the gx, gy, and gz variables as we please.
// Either print them as raw ADC values, or calculated in DPS.
Serial.print(“G: “);
#ifdef PRINT_CALCULATED
// If you want to print calculated values, you can use the
// calcGyro helper function to convert a raw ADC value to
// DPS. Give the function the value that you want to convert.
Serial.print(imu.calcGyro(imu.gx), 2);
Serial.print(”, “);
Serial.print(imu.calcGyro(imu.gy), 2);
Serial.print(”, “);
Serial.print(imu.calcGyro(imu.gz), 2);
Serial.println(” deg/s”);
#elif defined PRINT_RAW
Serial.print(imu.gx);
Serial.print(", “);
Serial.print(imu.gy);
Serial.print(”, ");
Serial.println(imu.gz);
#endif
}

void printAccel()
{
// Now we can use the ax, ay, and az variables as we please.
// Either print them as raw ADC values, or calculated in g’s.
Serial.print(“A: “);
#ifdef PRINT_CALCULATED
// If you want to print calculated values, you can use the
// calcAccel helper function to convert a raw ADC value to
// g’s. Give the function the value that you want to convert.
Serial.print(imu.calcAccel(imu.ax), 2);
Serial.print(”, “);
Serial.print(imu.calcAccel(imu.ay), 2);
Serial.print(”, “);
Serial.print(imu.calcAccel(imu.az), 2);
Serial.println(” g”);
#elif defined PRINT_RAW
Serial.print(imu.ax);
Serial.print(", “);
Serial.print(imu.ay);
Serial.print(”, ");
Serial.println(imu.az);
#endif

}

void printMag()
{
// Now we can use the mx, my, and mz variables as we please.
// Either print them as raw ADC values, or calculated in Gauss.
Serial.print(“M: “);
#ifdef PRINT_CALCULATED
// If you want to print calculated values, you can use the
// calcMag helper function to convert a raw ADC value to
// Gauss. Give the function the value that you want to convert.
Serial.print(imu.calcMag(imu.mx), 2);
Serial.print(”, “);
Serial.print(imu.calcMag(imu.my), 2);
Serial.print(”, “);
Serial.print(imu.calcMag(imu.mz), 2);
Serial.println(” gauss”);
#elif defined PRINT_RAW
Serial.print(imu.mx);
Serial.print(", “);
Serial.print(imu.my);
Serial.print(”, ");
Serial.println(imu.mz);
#endif
}

// Calculate pitch, roll, and heading.
// Pitch/roll calculations take from this app note:
// http://cache.freescale.com/files/sensors/doc/app_note/AN3461.pdf?fpsp=1
// Heading calculations taken from this app note:
// http://www51.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/Magnetic__Literature_Application_notes-documents/AN203_Compass_Heading_Using_Magnetometers.pdf
void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
{
float roll = atan2(ay, az);
float pitch = atan2(-ax, sqrt(ay * ay + az * az));

float heading;
if (my == 0)
heading = (mx < 0) ? PI : 0;
else
heading = atan2(mx, my);

heading -= DECLINATION * PI / 180;

if (heading > PI) heading -= (2 * PI);
else if (heading < -PI) heading += (2 * PI);

// Convert everything from radians to degrees:
heading *= 180.0 / PI;
pitch *= 180.0 / PI;
roll *= 180.0 / PI;

Serial.print("Pitch, Roll: “);
Serial.print(pitch, 2);
Serial.print(”, ");
Serial.println(roll, 2);
Serial.println(time);
Serial.print("Heading: "); Serial.println(heading, 2);

//-------------------

if(SD.exists(“testlog.txt”)) {
Serial.println(“File Exists!”);
file = SD.open(“testlog.txt”, FILE_WRITE);

time = millis();
file.print(time);

file.print(roll, 2);
file.print(" “);
file.print(heading, 2);
file.print(” “);
file.print(pitch, 2);
file.print(” ");

file.print(imu.calcGyro(imu.gx), 2);
file.print(" “);
file.print(imu.calcGyro(imu.gy), 2);
file.print(” “);
file.print(imu.calcGyro(imu.gz), 2);
file.print(” ");

file.print(imu.calcAccel(imu.ax), 2);
file.print(" “);
file.print(imu.calcAccel(imu.ay), 2);
file.print(” “);
file.print(imu.calcAccel(imu.az), 2);
file.print(” ");

file.print(imu.calcMag(imu.mx), 2);
file.print(" “);
file.print(imu.calcMag(imu.my), 2);
file.print(” ");
file.println(imu.calcMag(imu.mz), 2);

file.close();

} else {
Serial.println(“File Does not exist”);
}

delay(3000);
//---------------------

}

Put a space after millis().

conteph:
Here is my code, thank you in advance!

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is far too long for me to study quickly without copying to my text editor.

...R

did you forget to print a space (" ") between the time and roll value?

   file.print(time);
      
    file.print(roll, 2);
    file.print(" ");
    file.print(heading, 2);