Using mills() to add timestamp to x,y,z data in 3 axis accelerometer.

I am trying to add a timestamp that when the loop iterates it adds the timestamp by using mills()

Here is my code: (any help that you can give me would be very much appreciated)

Also I know that under the void loop I have the millis() function randomly written, I am just unsure how to add it into the program

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

const int chipSelect = 53;
int timej = 0;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
}

void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 3) { 
      dataString += ",";  millis()
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);

    
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

Why not just write the data to the file as binary?
Much faster.

You have the (incorrect) call to millis() in the for loop - do you need a stamp for every reading?
They're only going to be about 100us apart, so there's a good chance they'll all have the same millis() value anyway.

Yes the professor wants us to have a timestamp for every reading to write the proper code to read all three analog signals from the accelerometer and save the data in the SD card via the SD Card reader provided.

Ok, so you know that millis() returns an unsigned long value, and analogRead returns an int.

What's the problem?

The problem is I have no clue how to add the timestamp to the readings when the loop iterates. what would be the code to do this, is what im looking for.

Very similar problem and very identical IP address here

AWOL:
Very similar problem and very identical IP address here

I thought that the query sounded familiar

Yes. Correct it is the same person, I could not figure out my old login/email so I made a new account. I apologize for reposting this question/thread multiple times, it will not happen again, I have secured a new account now.

So, you know how to turn an analogue reading in a variable (an integer value) into part of a String.

All you have to do is put the value of millis() into a variable, and append it to your String.

If I were doing it, I'd write the values as binary, and convert it to ASCII when read back .
I certainly would not use Strings.

Maybe something you should discuss with your instructor/prof.

Thank you for the help, I still have no clue what do though because I don't understand the code as it was just adapted from the tutorials from Arduino.

The Arduino IDE is jam-packed with worked examples.

Try working through a few, and see if you can relate them to your code.

Failing that, complain that the teaching staff appear to be failing in their jobs.

Thank you very much. I kinda understand what you were saying when you said the following:

"So, you know how to turn an analogue reading in a variable (an integer value) into part of a String.
All you have to do is put the value of millis() into a variable, and append it to your String."

but I do not know how to implement that in the code / what the problem is.

Do you understand these two lines of code?

    int sensor = analogRead(analogPin);
    dataString += String(sensor);

Not really to be completely honest.

In that case, reply #10 is the way to go.