Need help Adding a timestamp using mills into x,y,z accelerometer .

I really could use some help with this code. I am trying to add a timestamp under the section " // read three sensors and append to the string:" after the data string+= . I am having difficulties figuring out the code to add a timestamp to the x,y,z data.

BME211.Final.ino (1.35 KB)

Please post your code in code tags.

    if (analogPin < 3) { 
      dataString += ",";  millis()
    }

That won’t even compile.

millis() returns a value. It is pointless to call it when you don’t care what the value is.

@PaulS I know im just very confused trying to add the timestamp after the loop is done iterating, I just put that there as a baseline because I have no clue how to add a timestamp.

I really could use some help with this code. I am trying to add a timestamp under the section " // read three sensors and append to the string:" after the data string+= . I am having difficulties figuring out the code to add a timestamp to the x,y,z data.

Here is my code:

#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");
  }
}

It seems like you seem to already know how to append to a String, but then decided to try to invent your own syntax when it came to the timestamp. Spend a little time studying how you appended the sensor readings to dataString. Now spend some time considering why you aren't doing the same with the timestamp.

Personally, I would do all this without the use of String at all.

You have another answer in the new thread that you started.

I really have no clue about the code, this is from an SD card reader tutorial from Arduino, we are supposed to modify it to add the timestamp. This is my first time using Arduino, its for a MATLAB programming class.