Writing in a txt.file onto an SD-Card leads to defective numbers[SOLVED]

Hi fam

I am data logging from a sensor. I write the sensor data onto an SD-Card using SPI. My issue is that some of the numbers are damaged. This occurs maybe 10-15 times upon 3000 samples. I cannot explain why this happens. Below I posted some examples where the third number is the defective one. I use the SD library to write onto the card with .open, .println and . close

-0.4892
0.5565
0.8269
2401
1.2721
1.1211

0.5345
0.1765
-2679
-0.5853
-0.7064

0.6463
0.7371
0.800.8517
0.8543
0.8601

-0.4764
0.6519
1.06761.3823
1.5134
1.6836

timkeweb:
I use the SD library to write onto the card with .open, .println and . close

That sounds reasonable, but who would know without the code? I rather suspect that SD is entirely innocent, and the text you're writing is the problem.

Here is the code. Maybe it helps

// put your setup code here, to run once:
  Serial.begin(9600);  
  //Call .begin() to configure the IMUs
  if( System.begin() != 0 )
  {
    Serial.println("Device error");
  }
  else  
  {
      Serial.println("Device OK!");
  }


//******************************************************************SD-Card part******************************************************************************************//
//************************************************************************************************************************************************************************//

    while (!Serial) {
  }
  Serial.print("Initializing SD card...");

  if (!SD.begin(CS_PIN)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
 
  
  myFileAcc = SD.open("test.txt", FILE_WRITE);
  Serial.println("card initialized.");

    
}


void loop()
{
    currentMillis = micros();
  
    //setting a constant sampling time. used to calculate velocity and position
    if (currentMillis - previousMillis >= PERIOD)
    {
         previousMillis = currentMillis;

     //to make sure, the sensor values are kept within the array. Size of the arrays are determined here as 
        well
    if (i == 10) i = 0;


//***********************************************************************Calculations*************************************************************************************//
//************************************************************************************************************************************************************************//
        
        //getting acceleration values
        ax = System.readFloatAccelX()-*pxValue;
        ay = System.readFloatAccelY()-*pyValue;
    
        //Calculation of the acceleration. Correction of bias included. Internal noise gets flaten out
        if(ax < maxMinBuffer[1]+bufferZone && ax > maxMinBuffer[2]-bufferZone)
        {
      
            accelBuffer[i] = 0;
    
        }
        else
        { 
   
            accelBuffer[i] = System.realValue(ax,ay, ExPos, ExNeg, EyPos, EyNeg);
           
        }
        
        if(accelBuffer[i] == 0)
        {
            *pAccZeroCounter++;
        }
        else
        {
            *pAccZeroCounter = 0;
        }
     
//************************************************************************************************************************************************************************//
//************************************************************************************************************************************************************************//

    
    

//***********************************************************************Writing on SDcard********************************************************************************//
//************************************************************************************************************************************************************************//
    
    if (myFileAcc) 
    {
        myFileAcc.println(accelBuffer[i],4);

    } 
    else 
    {
        Serial.println("error opening txt-File");
    }
    
  
//************************************************************************************************************************************************************************//
//************************************************************************************************************************************************************************//

    i++;
    runtime++;
    
    if (runtime == 3000)
    {
      myFileAcc.close();
      Serial.println("Done!");
        
    for(;;){      
    }
    }
  }
}


//here is the functions called above

//Should take care if the fact that the sensor axis is not perfectly aligned with the moving axis
//It takes x and y values into account... simple pythagoras to calculate the expected value
float Grove6a::realValue(float ax, float ay, float ExP, float ExN, float EyP, float EyN)
{
  float value;

  if ( ax > 0 && ay > 0)
  {
    value = sqrt(pow(ax/ExP,2) + pow(ay/EyP,2));
  }
  else if ( ax > 0 && ay < 0)
  {
    value = sqrt(pow(ax/ExP,2) + pow(ay/EyN,2));
  }
  else if ( ax < 0 && ay > 0)
  {
    value = -1 * (sqrt(pow(ax/ExN,2) + pow(ay/EyP,2)));
  }
  else if (ax < 0 && ay < 0)
  {
    value = -1 * (sqrt(pow(ax/ExN,2) + pow(ay/EyN,2)));
  }

 
  return value;
}



int16_t Grove6a::readAccelX( void )
{
  int16_t output;
  status_t errorLevel = readRegister16( &output, LSM6DS3_ACC_GYRO_OUTX_L_XL );

  return output;
}
float Grove6a::readFloatAccelX( void )
{
  float output = calcAccel(readAccelX());
  return output;
}


float Grove6a::calcAccel( int16_t input )
{
  float output = (float)input * 0.061 * (settings.accelRange >> 1) / 1000;
  return output;
}

Additionally the program jumps out of the loop function back into the setup function. Operating voltage is stable, confuses me as well :(.