Problems storing angles calculated from MPU6050 output to SD card

Hi there!

I am trying log data from a MPU6050. The sketch calculates angles from the MPU reading (sketch is from Matthew's Arduino Page). The sketch works really perfect, however I have problems to store the angles (compAngleX and compAngleY) to a SD card on an Ethernet shield. It goes all the way done until the message "Error writing to file !". And checking the data.csv….no data on it….
Does anyone have any idea what I am doing wrong or how I could make this working?

Cheers!

//
#include "SD.h"
#include"SPI.h"
#include<Wire.h>
//
const int MPU_addr=0x68;
double AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
uint32_t timer; 
double compAngleX, compAngleY; 
#define degconvert 57.2957786 

String dataString =""; 
File data;
//
//
void setup()
{
// Set up MPU 6050:
  Wire.begin();
  #if ARDUINO >= 157
  Wire.setClock(400000UL); 
  #else
    TWBR = ((F_CPU / 400000UL) - 16) / 2; 
  #endif

  
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  
  Wire.write(0);     
  Wire.endTransmission(true);
  
// Open serial communications
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(4, OUTPUT);
//
// see if the card is present and can be initialized:
if (!SD.begin(4)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
  //setup starting angle
  //1) collect the data
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);  
  AcX=Wire.read()<<8|Wire.read();     
  AcY=Wire.read()<<8|Wire.read();  
  AcZ=Wire.read()<<8|Wire.read();  
  Tmp=Wire.read()<<8|Wire.read();  
  GyX=Wire.read()<<8|Wire.read();  
  GyY=Wire.read()<<8|Wire.read();  
  GyZ=Wire.read()<<8|Wire.read();  


  double roll = atan2(AcY, AcZ)*degconvert;
  double pitch = atan2(-AcX, AcZ)*degconvert;

 
  double gyroXangle = roll;
  double gyroYangle = pitch;
  double compAngleX = roll;
  double compAngleY = pitch;

  //start a timer
  timer = micros();
}
//
void loop(){

  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);  
  AcX=Wire.read()<<8|Wire.read();       
  AcY=Wire.read()<<8|Wire.read();  
  AcZ=Wire.read()<<8|Wire.read();  
  Tmp=Wire.read()<<8|Wire.read();  
  GyX=Wire.read()<<8|Wire.read();  
  GyY=Wire.read()<<8|Wire.read();  
  GyZ=Wire.read()<<8|Wire.read();  
  
  double dt = (double)(micros() - timer) / 1000000; 
  timer = micros(); 

  double roll = atan2(AcY, AcZ)*degconvert;
  double pitch = atan2(-AcX, AcZ)*degconvert;

  double gyroXrate = GyX/131.0;
  double gyroYrate = GyY/131.0;

  compAngleX = 0.99 * (compAngleX + gyroXrate * dt) + 0.01 * roll; 
  compAngleY = 0.99 * (compAngleY + gyroYrate * dt) + 0.01 * pitch; 

dataString = String(compAngleX) + "," + String(compAngleY); // convert to CSV
saveData(); // save to SD card
delay(60000); // delay before next write to SD Card, adjust as required
}

//
void saveData(){
if(SD.exists("data.csv")){ // check the card is still there
// now append new data file
Serial.println("Creating data.csv...");

if (data){
File data = SD.open("data.csv", FILE_WRITE);
data.println(dataString);
data.close(); // close the file
}
}
else{
Serial.println("Error writing to file !");
}
}

You will see "Error writing to file !" if this check fails to find the data file:

  if (SD.exists("data.csv"))

Is the file actually there?