 }
 
 void writeToFile(char* data) {
+  File file = SD.open("data.log", FILE_WRITE);
   if (file) {
     file.println(data);
+    file.close();
+  } else {
+    Serial.println("Error to open/create file");
   }
 }
 

  // or the SD library functions will not work. 
  pinMode(10, OUTPUT);

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // 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 = SD.open("data.log", FILE_WRITE);

  // I2C Slave configuration
  Wire.begin(I2C_ADDRESS);                // join i2c bus with address #4

    attachInterrupt(1, speed_counter, FALLING);

    saveValues(engine_rpm, wheel_rpm, gear(ratio));
    writeToFile("test");
  }
  
}
