millies() doesn't work on sdcard datalogger

Hello,

I got small datalogger in my project. But I have problem with millis() function. While usinge Serial Print it works fine, but when I want save my measurments on sd card I see only forst time log and later arduino lefts only free space. What Can I do.

Yours,
Elav

PS: Now I see that funcion pronts time sometimes but mainly I see free space

Here it's what I mean. Difference in programs is only in datalogginf on sd card

The function millis() will return an unsigned long value always.

So if you convert it and get only blanks (or tabs or nothing),
you don't have a problem with millis(), but with your code (that you did not post).

"serial print loop":

void loop() {
  digitalWrite (LED, HIGH);
  czas = millis();
  double temp1 = termopara1.readThermocouple(CELSIUS);
  double temp2 = termopara2.readThermocouple(CELSIUS);
  double temp3 = termopara3.readThermocouple(CELSIUS);
  double temp4 = termopara4.readThermocouple(CELSIUS);
  double temp5 = termopara5.readThermocouple(CELSIUS);
  double temp6 = termopara6.readThermocouple(CELSIUS);

  Serial.print(czas);
  Serial.print("\t");

  if (temp1==10000) {
    Serial.print("ERR");
    Serial.print("\t");
  } else {
    Serial.print(temp1);
    Serial.print("\t");
  }
  if (temp2==10000) {
    Serial.print("ERR");
    Serial.print("\t");
  } else {
    Serial.print(temp2);
    Serial.print("\t");
  }
  if (temp3==10000) {
    Serial.print("ERR");
    Serial.print("\t");
  } else {
    Serial.print(temp3);
    Serial.print("\t");
  }
  if (temp4==10000) {
    Serial.print("ERR");
    Serial.print("\t");
  } else {
    Serial.print(temp4);
    Serial.print("\t");
  }
  if (temp5==10000) {
    Serial.print("ERR");
    Serial.print("\t");
  } else {
    Serial.print(temp5);
    Serial.print("\t");
  }
  if (temp6==10000) {
    Serial.print("ERR");
    Serial.print("\t");
  } else {
    Serial.print(temp6);
    Serial.print("\t");
  }
    
  Serial.println();
  digitalWrite (LED, LOW);
  delay (20);

}

"datalogger loop code":

void loop() {
  digitalWrite (LED, HIGH);
  czas = millis();
  
  SPI.end();
  pinMode(SO,INPUT);  
  pinMode(myCLK,OUTPUT);
  
  double temp1 = termopara1.readThermocouple(CELSIUS);
  double temp2 = termopara2.readThermocouple(CELSIUS);
  double temp3 = termopara3.readThermocouple(CELSIUS);
  double temp4 = termopara4.readThermocouple(CELSIUS);
  double temp5 = termopara5.readThermocouple(CELSIUS);
  double temp6 = termopara6.readThermocouple(CELSIUS);

  SPI.begin();
  datalog = SD.open("datalog.txt", FILE_WRITE);
  
  if (datalog) {
    datalog.print(czas);
    datalog.print("\t");

    if (temp1==10000) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp1);
      datalog.print("\t");
    }
    
    if (temp2==10000) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp2);
      datalog.print("\t");
    }
    
    if (temp3==10000) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp3);
      datalog.print("\t");
    }
    
    if (temp4==10000) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp4);
      datalog.print("\t");
    }
    
    if (temp5==10000) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp5);
      datalog.print("\t");
    }
    
    if (temp6==10000) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp6);
      datalog.print("\t");
    }
    
    datalog.println();
    
  } 
  else {
    Serial.println("Nie mozna otworzyc pliku datalog.txt");
  }
  
  datalog.close();  
  digitalWrite(sdPIN, LOW);
  digitalWrite (LED, LOW);
  delay (40);

}

I am suspicious of

 datalog.println();

I do not think that the tab after the temp6 field is useful, and I would change the code to put the new line with the last datalog.print statement. See if this arrangement gets consistent printing of the millis() field.

void loop() {
  digitalWrite (LED, HIGH);
  czas = millis();
  
  SPI.end();
  pinMode(SO,INPUT);  
  pinMode(myCLK,OUTPUT);
  
  double temp1 = termopara1.readThermocouple(CELSIUS);
  double temp2 = termopara2.readThermocouple(CELSIUS);
  double temp3 = termopara3.readThermocouple(CELSIUS);
  double temp4 = termopara4.readThermocouple(CELSIUS);
  double temp5 = termopara5.readThermocouple(CELSIUS);
  double temp6 = termopara6.readThermocouple(CELSIUS);

  SPI.begin();
  datalog = SD.open("datalog.txt", FILE_WRITE);
  
  if (datalog) {
    datalog.print(czas);
    datalog.print("\t");

    if (temp1==10000) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp1);
      datalog.print("\t");
    }
    
    if (temp2==10000) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp2);
      datalog.print("\t");
    }
    
    if (temp3==10000) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp3);
      datalog.print("\t");
    }
    
    if (temp4==10000) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp4);
      datalog.print("\t");
    }
    
    if (temp5==10000) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp5);
      datalog.print("\t");
    }
    
    if (temp6==10000) {
      datalog.println("ERR");//add new line and remove last tab
     //datalog.print("\t");
    } else {
      datalog.println(temp6);//add new line and remove last tab
      //datalog.print("\t");
    }
    
    //datalog.println();
    
  } 
  else {
    Serial.println("Nie mozna otworzyc pliku datalog.txt");
  }
  
  datalog.close();  
  digitalWrite(sdPIN, LOW);
  digitalWrite (LED, LOW);
  delay (40);

}

Still having issues ;/

EDIT: I checked my code without SPI.end, begin, etc. And in that case my issue doesnt exists. But I have to use it, any ideas? Some delays or something?

EDIT2: Now I'm 100% sure that it's problem of SPI.end and SPI.begin, sometimes datalogging suspend almost at start

elaveiro:
"datalogger loop code":

void loop() {

datalog.close(); 
  digitalWrite(sdPIN, LOW);
  digitalWrite (LED, LOW);
  delay (40);

}

Why do you drive sdPIN LOW after datalog.close()? sdPIN should be HIGH to 'de-select' the SDcard. with it low, manipulations of the SCK, MOSI while sdPin is low will 'confuse' the SDcard.

Chuck