The program crashes when i put in my code the function to save data in a SD

I am using an arduino Uno R3 board and three sensors: MPU6050, GPS-NEO6MV2 and a Micro SD reader. It works all properly , but when I put the function to save the data into the sd card it crashes or it start to loop the setup that doesn't make any sense. It's my first arduino project so i am a beginner, if anybody can help me I would be so greatfull.

This is the code:


#include <Wire.h>
#include <MPU6050_tockn.h>
#include <SD.h>

#include <SoftwareSerial.h>
#include <TinyGPS++.h>

MPU6050 mpu6050(Wire);
SoftwareSerial gpsSerial(4, 3);

TinyGPSPlus gps;
File dataFile;  // Oggetto per il file su SD

void setup() {
  Serial.begin(9600);
  Wire.begin();
  gpsSerial.begin(9600);
  delay(1000);
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);

  // Inizializza la scheda SD
  if (SD.begin(10)) {
    Serial.println("Scheda SD inizializzata con successo.");
  } else {
    Serial.println("Errore nell'inizializzazione della scheda SD.");
  }


  Serial.println("inizio");
  delay(2000);
}


void loop() {

  mpu6050.update();

  int i;
  int j;

  float VM_angle_Y;
  float angle_Y;
  float temp;
  int hour;
  int minute;
  int second;
  int year;
  int month;
  int day;
  float VM_temp;
  float acc_X;
  float acc_Y;
  float VM_acc_X;
  float VM_acc_Y;
  static unsigned long t1 = 0;
  static unsigned long t2 = 0;
  static unsigned long t3 = 0;
  float latitudine;
  float longitudine;
  float speedKmph;
  float VM_speedKmph;


  if (millis() - t1 >= 100) {
    t1 = millis();
    for (i = 0; i < 100; i++) {
      temp = mpu6050.getTemp();
      VM_temp += temp;
      angle_Y = mpu6050.getAngleY();
      VM_angle_Y += angle_Y;
      acc_X = mpu6050.getAccX();
      VM_acc_X += acc_X;
      acc_Y = mpu6050.getAccY();
      VM_acc_Y += acc_Y;
    }
    VM_temp /= 100;
    VM_acc_X /= 100;
    VM_acc_Y /= 100;
    VM_angle_Y /= 100;
  }

  while (gpsSerial.available() > 0) {
    if (gps.encode(gpsSerial.read())) {
      if (gps.location.isValid()) {
        for (j = 0; j < 10; j++) {
          speedKmph = gps.speed.kmph();
          VM_speedKmph += speedKmph;
        }
        VM_speedKmph /= 10;

        latitudine = gps.location.lat();
        longitudine = gps.location.lng();

        hour = gps.time.hour() + 2;
        minute = gps.time.minute();
        second = gps.time.second();
        year = gps.date.year();
        month = gps.date.month();
        day = gps.date.day();
      }
    }
  }












  if (millis() - t3 >= 500) {
    t3 = millis();
    Serial.print(day);
    Serial.print("/");
    Serial.print(month);
    Serial.print("/");
    Serial.print(year);
    Serial.print("            | ");

    if (hour >= 10) {
      Serial.print(hour);
    } else if (hour >= 1 && hour <= 9) {
      Serial.print("0");
      Serial.print(hour);
    } else {
      Serial.print("00");
    }
    Serial.print(":");
    if (minute >= 10) {
      Serial.print(minute);
    } else if (minute >= 1 && minute <= 9) {
      Serial.print("0");
      Serial.print(minute);
    } else {
      Serial.print("00");
    }

    Serial.print(":");

    if (second >= 10) {
      Serial.print(second);
    } else if (second >= 1 && second <= 9) {
      Serial.print("0");
      Serial.print(second);
    } else {
      Serial.print("00");
    }


    Serial.print("            | ");

    Serial.print(VM_temp, 1);
    Serial.print("            | ");
    Serial.print(VM_acc_X, 1);
    if (VM_acc_X > 0) {
      Serial.print("            | ");
    } else {
      Serial.print("           | ");
    }
    Serial.print(VM_acc_Y, 1);
    if (VM_acc_Y > 0) {
      Serial.print("            | ");
    } else {
      Serial.print("           | ");
    }


    if (latitudine >= 0 && latitudine < 10) {
      Serial.print(latitudine, 6);
      Serial.print(" N");
      Serial.print("            | ");
    } else if (latitudine >= 10) {
      Serial.print(latitudine, 6);
      Serial.print(" N");
      Serial.print("           | ");
    } else if (latitudine < 0 && latitudine > -10) {
      Serial.print(-latitudine, 6);
      Serial.print(" S");
      Serial.print("            | ");
    } else {
      Serial.print(-latitudine, 6);
      Serial.print(" S");
      Serial.print("           | ");
    }



    if (longitudine >= 0 && longitudine < 10) {
      Serial.print(longitudine, 6);
      Serial.print(" E");
      Serial.print("            | ");
    } else if (longitudine >= 10) {
      Serial.print(longitudine, 6);
      Serial.print(" E");
      Serial.print("           | ");
    } else if (longitudine < 0 && longitudine > -10) {
      Serial.print(-longitudine, 6);
      Serial.print(" O");
      Serial.print("            | ");
    } else {
      Serial.print(-longitudine, 6);
      Serial.print(" O");
      Serial.print("           | ");
    }




    if (VM_speedKmph >= 0 && VM_speedKmph < 10) {
      Serial.print(VM_speedKmph, 1);
      Serial.print(" km/h");
      Serial.print("            | ");
    } else if (VM_speedKmph >= 10 && VM_speedKmph < 100) {
      Serial.print(VM_speedKmph, 1);
      Serial.print(" km/h");
      Serial.print("           | ");
    } else {
      Serial.print(VM_speedKmph, 1);
      Serial.print(" km/h");
      Serial.print("           | ");
    }






    if (VM_angle_Y <= -2 && VM_angle_Y >= -90) {

      VM_angle_Y *= -1;
      Serial.print("Angolo di piega SX: ");
      Serial.print(VM_angle_Y, 2);
      Serial.print("°");
      Serial.println();

    } else if (VM_angle_Y >= 2 && VM_angle_Y <= 90) {

      Serial.print("Angolo di piega DX: ");
      Serial.print(VM_angle_Y, 2);
      Serial.print("°");
      Serial.println();

    } else {
      Serial.println("Flat");
    }
  }


  dataFile = SD.open("gpsdata.csv", FILE_WRITE);
  if (dataFile) {
    dataFile.print(latitudine, 6);
    dataFile.print(",");
    dataFile.print(longitudine, 6);
    dataFile.print(",");
    dataFile.print(day);
    dataFile.print("/");
    dataFile.print(month);
    dataFile.print("/");
    dataFile.print(year);
    dataFile.print(",");
    dataFile.print(hour);
    dataFile.print(":");


    if (minute >= 10) {
      dataFile.print(minute);
    } else if (minute >= 1 && minute <= 9) {
      dataFile.print("0");
      dataFile.print(minute);
    } else {
      dataFile.print("00");
    }

    dataFile.print(":");
    if (second >= 10) {
      dataFile.print(second);
    } else if (second >= 1 && second <= 9) {
      dataFile.print("0");
      dataFile.print(second);
    } else {
      dataFile.print("00");
    }
    dataFile.println();
    dataFile.close();
  }
}

you are just too low in memory, the uno is struggling. (don't you get a warning when you compile?)

try replacing all the Serial.print("xxxx: "); by Serial.print(F("xxxx: ")); as well as the dataFile.print("xxx"); by dataFile.print(F("xxx"));

➜ that will put all the text in flash memory, that might be enough to free up some memory to accommodate the 512 bytes buffer required by the SD card (your UNO only has 2KB of SRAM) and the rest of your code

1 Like

what is this:

    Serial.print("           | ");
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <MPU6050_tockn.h>


TinyGPSPlus gps;
MPU6050 mpu6050(Wire);
SoftwareSerial gpsSerial(4, 3);  // Imposta i pin RX (4) e TX (3) per la comunicazione GPS
File dataFile;

void setup() {
  delay(2000);
  Serial.begin(9600);
  gpsSerial.begin(9600);
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);

  // Inizializza la scheda SD
  if (SD.begin(10)) {
    Serial.println("Scheda SD inizializzata con successo.");
  } else {
    Serial.println("Errore nell'inizializzazione della scheda SD.");
  }

  dataFile = SD.open("gpsdata.csv", FILE_WRITE);  // Apre il file CSV
  if (dataFile) {
    dataFile.println("Latitudine,Longitudine,Data,Time");  // Intestazione del file CSV
    dataFile.close();
  }
}

void loop() {

  static unsigned long t1 = 0;
  double lat = 0.0;  // Valore di default per latitudine
  double lon = 0.0;  // Valore di default per longitudine
  int hour = 0;      // Valore di default per l'ora
  int minute = 0;    // Valore di default per i minuti
  int second = 0;    // Valore di default per i secondi
  int year = 0;      // Valore di default per l'anno
  int month = 0;     // Valore di default per il mese
  int day = 0;       // Valore di default per il giorno


  while (gpsSerial.available() > 0) {
    if (gps.encode(gpsSerial.read())) {
      if (gps.location.isValid()) {
        if (millis() - t1 >= 1000) {

          t1 = millis();


          lat = gps.location.lat();
          lon = gps.location.lng();

          hour = gps.time.hour() + 2;
          minute = gps.time.minute();
          second = gps.time.second();
          year = gps.date.year();
          month = gps.date.month();
          day = gps.date.day();


          // Stampa le coordinate su Arduino
          Serial.print("Latitudine: ");
          Serial.println(lat, 6);
          Serial.print("Longitudine: ");
          Serial.println(lon, 6);
          Serial.print("Data: ");
          Serial.print(day);
          Serial.print("/");
          Serial.print(month);
          Serial.print("/");
          Serial.print(year);
          Serial.print(" ");

          Serial.print("Ora locale: ");
          if (hour >= 10) {
            Serial.print(hour);
          } else if (hour >= 1 && hour <= 9) {
            Serial.print("0");
            Serial.print(hour);
          } else {
            Serial.print("00");
          }
          Serial.print(":");
          if (minute >= 10) {
            Serial.print(minute);
          } else if (minute >= 1 && minute <= 9) {
            Serial.print("0");
            Serial.print(minute);
          } else {
            Serial.print("00");
          }




          Serial.print(":");
          if (second >= 10) {
            Serial.println(second);
          } else if (second >= 1 && second <= 9) {
            Serial.print("0");
            Serial.println(second);
          } else {
            Serial.println("00");
          }
        }
        // Salva le coordinate sul file CSV
        dataFile = SD.open("gpsdata.csv", FILE_WRITE);
        if (dataFile) {
          dataFile.print(lat, 6);
          dataFile.print(",");
          dataFile.print(lon, 6);
          dataFile.print(",");
          dataFile.print(day);
          dataFile.print("/");
          dataFile.print(month);
          dataFile.print("/");
          dataFile.print(year);
          dataFile.print(",");
          dataFile.print(hour);
          dataFile.print(":");


          if (minute >= 10) {
            dataFile.print(minute);
          } else if (minute >= 1 && minute <= 9) {
            dataFile.print("0");
            dataFile.print(minute);
          } else {
            dataFile.print("00");
          }

          dataFile.print(":");
          if (second >= 10) {
            dataFile.print(second);
          } else if (second >= 1 && second <= 9) {
            dataFile.print("0");
            dataFile.print(second);
          } else {
            dataFile.print("00");
          }
          dataFile.println();
          dataFile.close();
        }
      }
    }
  }
}

Why this works without any problems?

It's just to make some space beetween the values

where you need so much space?

when you compile that you are at 89% of the available RAM is statically allocated memory.

when memory is so busy, just an extra byte can get you in trouble.

#include <Wire.h>
#include <MPU6050_tockn.h>
#include <SD.h>

#include <SoftwareSerial.h>
#include <TinyGPS++.h>

MPU6050 mpu6050(Wire);
SoftwareSerial gpsSerial(4, 3);

TinyGPSPlus gps;
File dataFile;  // Oggetto per il file su SD

void setup() {
  Serial.begin(9600);
  Wire.begin();
  gpsSerial.begin(9600);
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);

  // Inizializza la scheda SD
  if (!SD.begin(10))    Serial.println("Err SD");
  delay(3000);

}


void loop() {
  const char Spaces[] = {"           | "};
  mpu6050.update();
  float VM_angle_Y;
  uint16_t VM_temp;
  float VM_acc_X;
  float VM_acc_Y;
  static unsigned long t1 = 0;
  static unsigned long t2 = 0;
  static unsigned long t3 = 0;
  float latitudine;
  float longitudine;
  uint16_t VM_speedKmph;


  if (millis() - t1 >= 100) {
    t1 += 100;
    for (i = 0; i < 100; i++) {
      VM_temp += mpu6050.getTemp() * 10;
      VM_angle_Y += mpu6050.getAngleY();
      VM_acc_X += mpu6050.getAccX();
      VM_acc_Y += mpu6050.getAccY();
    }
    VM_temp /= 100;
    VM_acc_X /= 100;
    VM_acc_Y /= 100;
    VM_angle_Y /= 100;
  }
  char bufer[30];

  while (gpsSerial.available() > 0) {
    if (gps.encode(gpsSerial.read())) {
      if (gps.location.isValid()) {
        for (j = 0; j < 10; j++) VM_speedKmph += gps.speed.kmph() * 10;
        VM_speedKmph /= 10;

        latitudine = gps.location.lat();
        longitudine = gps.location.lng();

        sprintf(bufer, "%02d/%02d/%02d\t%2d:%02d:%02d", gps.date.day(), gps.date.month(), gps.date.year(), gps.time.hour() + 2, gps.time.minute(), gps.time.second(););
      }
    }
  }
  Serial.print(bufer);
    Serial.print(Spaces);

  if (millis() - t3 >= 500) {
    t3 += 500;

    Serial.print(VM_temp / 10);
    Serial.print('.');
    Serial.print(VM_temp % 10);
    Serial.print(Spaces);
    Serial.print(VM_acc_X, 1);
    Serial.print(Spaces);

    Serial.print(VM_acc_Y, 1);
    Serial.print(Spaces);

    if (latitudine >= 0 ) {
      Serial.print(latitudine, 6);
      Serial.print(" N");
    }
    else {
      Serial.print(-latitudine, 6);
      Serial.print(" S");
    }
    Serial.print(Spaces);

    if (longitudine >= 0 ) {
      Serial.print(longitudine, 6);
      Serial.print(" E");
    }
    else {
      Serial.print(-longitudine, 6);
      Serial.print(" O");
    }
    Serial.print(Spaces);

    Serial.print(VM_speedKmph / 10);
    Serial.print('.');
    Serial.print(VM_speedKmph % 10);
    Serial.print(" km/h");
    Serial.print(Spaces);

    if (VM_angle_Y <= -2 && VM_angle_Y >= -90) {

      VM_angle_Y *= -1;
      Serial.print("Angolo di piega SX: ");
      Serial.print(VM_angle_Y, 2);
      Serial.print("°");
      Serial.println();

    } else if (VM_angle_Y >= 2 && VM_angle_Y <= 90) {

      Serial.print("Angolo di piega DX: ");
      Serial.print(VM_angle_Y, 2);
      Serial.print("°");
      Serial.println();

    } else {
      Serial.println("Flat");
    }
  }

  dataFile = SD.open("gpsdata.csv", FILE_WRITE);
  if (dataFile) {
    dataFile.print(latitudine, 6);
    dataFile.print(",");
    dataFile.print(longitudine, 6);
    dataFile.print(",");
    dataFile.println(bufer);
    dataFile.close();
  }
}

defining once

and using

    Serial.print(Spaces);

won't save memory compared to

    Serial.print("           | ");

even if you have 1000 of these prints. the string in between the double quotes is a string literal, a const char *, the compiler knows it can't change, so if it spots the same string literal elsewhere, it will reuse it.

basically if you compile

void setup() {
  Serial.begin(115200);
  Serial.print("           | ");
}

void loop() {}

or

void setup() {
  Serial.begin(115200);
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
  Serial.print("           | ");
}

void loop() {}

they will use up the same amount of RAM (and just a bit more flash to accommodate for the the call to print)

1 Like

cool, i knew i learn today something new

the compiler is very smart, if it notices you reuse the end of another string literal, it will use a pointer in the middle of the other string

const char * text1 = "Hello from France";
const char * text2 = "France";

➜ the compiler will notice 'F' 'r' 'a' 'n' 'c' 'e' '\0' are shared bits of memory and so text2 will point within text1 so text2 will not use extra memory for its content (just for the pointer)

1 Like

good to know.

does it work for integers?

    VM_temp /= 100;
    VM_acc_X /= 100;
    VM_acc_Y /= 100;
    VM_angle_Y /= 100;

I think is a problem beetween the sd activity and the mpu6050 because in this code when I add the mpu6050 library and the mpu6050.calcGyroOffsets(true) it doesn't work anymore.`

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <MPU6050_tockn.h>


TinyGPSPlus gps;
MPU6050 mpu6050(Wire);
SoftwareSerial gpsSerial(4, 3);  // Imposta i pin RX (4) e TX (3) per la comunicazione GPS
File dataFile;

void setup() {
  delay(2000);
  Serial.begin(9600);
  gpsSerial.begin(9600);
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);

  // Inizializza la scheda SD
  if (SD.begin(10)) {
    Serial.println("Scheda SD inizializzata con successo.");
  } else {
    Serial.println("Errore nell'inizializzazione della scheda SD.");
  }

  dataFile = SD.open("gpsdata.csv", FILE_WRITE);  // Apre il file CSV
  if (dataFile) {
    dataFile.println("Latitudine,Longitudine,Data,Time");  // Intestazione del file CSV
    dataFile.close();
  }
}

void loop() {

  static unsigned long t1 = 0;
  double lat = 0.0;  // Valore di default per latitudine
  double lon = 0.0;  // Valore di default per longitudine
  int hour = 0;      // Valore di default per l'ora
  int minute = 0;    // Valore di default per i minuti
  int second = 0;    // Valore di default per i secondi
  int year = 0;      // Valore di default per l'anno
  int month = 0;     // Valore di default per il mese
  int day = 0;       // Valore di default per il giorno


  while (gpsSerial.available() > 0) {
    if (gps.encode(gpsSerial.read())) {
      if (gps.location.isValid()) {
        if (millis() - t1 >= 1000) {

          t1 = millis();


          lat = gps.location.lat();
          lon = gps.location.lng();

          hour = gps.time.hour() + 2;
          minute = gps.time.minute();
          second = gps.time.second();
          year = gps.date.year();
          month = gps.date.month();
          day = gps.date.day();


          // Stampa le coordinate su Arduino
          Serial.print("Latitudine: ");
          Serial.println(lat, 6);
          Serial.print("Longitudine: ");
          Serial.println(lon, 6);
          Serial.print("Data: ");
          Serial.print(day);
          Serial.print("/");
          Serial.print(month);
          Serial.print("/");
          Serial.print(year);
          Serial.print(" ");

          Serial.print("Ora locale: ");
          if (hour >= 10) {
            Serial.print(hour);
          } else if (hour >= 1 && hour <= 9) {
            Serial.print("0");
            Serial.print(hour);
          } else {
            Serial.print("00");
          }
          Serial.print(":");
          if (minute >= 10) {
            Serial.print(minute);
          } else if (minute >= 1 && minute <= 9) {
            Serial.print("0");
            Serial.print(minute);
          } else {
            Serial.print("00");
          }




          Serial.print(":");
          if (second >= 10) {
            Serial.println(second);
          } else if (second >= 1 && second <= 9) {
            Serial.print("0");
            Serial.println(second);
          } else {
            Serial.println("00");
          }
        }
        // Salva le coordinate sul file CSV
        dataFile = SD.open("gpsdata.csv", FILE_WRITE);
        if (dataFile) {
          dataFile.print(lat, 6);
          dataFile.print(",");
          dataFile.print(lon, 6);
          dataFile.print(",");
          dataFile.print(day);
          dataFile.print("/");
          dataFile.print(month);
          dataFile.print("/");
          dataFile.print(year);
          dataFile.print(",");
          dataFile.print(hour);
          dataFile.print(":");


          if (minute >= 10) {
            dataFile.print(minute);
          } else if (minute >= 1 && minute <= 9) {
            dataFile.print("0");
            dataFile.print(minute);
          } else {
            dataFile.print("00");
          }

          dataFile.print(":");
          if (second >= 10) {
            dataFile.print(second);
          } else if (second >= 1 && second <= 9) {
            dataFile.print("0");
            dataFile.print(second);
          } else {
            dataFile.print("00");
          }
          dataFile.println();
          dataFile.close();
        }
      }
    }
  }
}

it does not (but with const the optimizer will just inject the right byte into assembly)

There might be timing conflicts where things would get slow or you would lose GPS data on software serial because the buffer would fill up but that's not your main issue. Memory is. Get a more capable arduino.

so it's not possible to end the project with the arduino uno?

there is no way to optimize the code?

well.... see post 2

  int hour = 0;
  int minute = 0;
  int second = 0;
  int year = 0;
  int month = 0;
  int day = 0;

Only year needs to be an int, the rest can be byte.

Since you only use Serial to send data, and only receive data from the GPS on SoftwareSerial, it is possible to eliminate the SoftwareSerial and instead use Serial to receive from the GPS (there is a 1K Ohm resistor between the Rx pin of the atmega328 and the Tx pin of the atmega16u2 Serial-to-USB chip, so the Tx from the GPS can generally safely override this signal). That will eliminate the 128 bytes of memory needed for the Rx and Tx buffers for SoftwareSerial. Note that there is no connection to the Rx of the GPS module, and doing this will require disconnecting the Tx from the GPS during an upload.

In this code I deleted all the Serial.print(), it just write on SD, but i have the same problem.

#include <Wire.h>
#include <MPU6050_tockn.h>
#include <SD.h>

#include <SoftwareSerial.h>
#include <TinyGPS++.h>

MPU6050 mpu6050(Wire);
SoftwareSerial gpsSerial(4, 3);

TinyGPSPlus gps;
File dataFile;  // Oggetto per il file su SD

void setup() {
  Serial.begin(9600);
  Wire.begin();
  gpsSerial.begin(9600);
  delay(1000);
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);

  // Inizializza la scheda SD
  if (SD.begin(10)) {
    Serial.println("Scheda SD inizializzata con successo.");
  } else {
    Serial.println("Errore nell'inizializzazione della scheda SD.");
  }

  dataFile = SD.open("gpsdata.csv", FILE_WRITE);  // Apre il file CSV
  if (dataFile) {
    dataFile.println("Latitudine,Longitudine,Data,Time");  // Intestazione del file CSV
    dataFile.close();
  }




  Serial.println("inizio");
  delay(2000);
}


void loop() {

  mpu6050.update();

  int i;
  int j;

  float VM_angle_Y;
  float angle_Y;
  float temp;
  int hour;
  int minute;
  int second;
  int year;
  int month;
  int day;
  float VM_temp;
  float acc_X;
  float acc_Y;
  float VM_acc_X;
  float VM_acc_Y;
  static unsigned long t1 = 0;
  static unsigned long t2 = 0;
  static unsigned long t3 = 0;
  float latitudine;
  float longitudine;
  float speedKmph;
  float VM_speedKmph;


  if (millis() - t1 >= 100) {
    t1 = millis();
    for (i = 0; i < 100; i++) {
      temp = mpu6050.getTemp();
      VM_temp += temp;
      angle_Y = mpu6050.getAngleY();
      VM_angle_Y += angle_Y;
      acc_X = mpu6050.getAccX();
      VM_acc_X += acc_X;
      acc_Y = mpu6050.getAccY();
      VM_acc_Y += acc_Y;
    }
    VM_temp /= 100;
    VM_acc_X /= 100;
    VM_acc_Y /= 100;
    VM_angle_Y /= 100;
  }

  while (gpsSerial.available() > 0) {
    if (gps.encode(gpsSerial.read())) {
      if (gps.location.isValid()) {
        for (j = 0; j < 10; j++) {
          speedKmph = gps.speed.kmph();
          VM_speedKmph += speedKmph;
        }
        VM_speedKmph /= 10;

        if (millis() - t2 >= 1000) {

          latitudine = gps.location.lat();
          longitudine = gps.location.lng();

          hour = gps.time.hour() + 2;
          minute = gps.time.minute();
          second = gps.time.second();
          year = gps.date.year();
          month = gps.date.month();
          day = gps.date.day();


          dataFile = SD.open("gpsdata.csv", FILE_WRITE);
          if (dataFile) {
            dataFile.print(latitudine, 6);
            dataFile.print(",");
            dataFile.print(longitudine, 6);
            dataFile.print(",");
            dataFile.print(day);
            dataFile.print("/");
            dataFile.print(month);
            dataFile.print("/");
            dataFile.print(year);
            dataFile.print(",");
            dataFile.print(hour);
            dataFile.print(":");


            if (minute >= 10) {
              dataFile.print(minute);
            } else if (minute >= 1 && minute <= 9) {
              dataFile.print("0");
              dataFile.print(minute);
            } else {
              dataFile.print("00");
            }

            dataFile.print(":");
            if (second >= 10) {
              dataFile.print(second);
            } else if (second >= 1 && second <= 9) {
              dataFile.print("0");
              dataFile.print(second);
            } else {
              dataFile.print("00");
            }
            dataFile.println();
            dataFile.close();
          }
        }
      }
    }
  }
}