HC-SR04, GPS, and SD card reader with Arduino MEGA

Finally finished school project to make a device to take distance measurement, take gps fix, then upload this data onto an SD card! Doesn’t look pretty but it works!

#include <NewPing.h> 
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <SPI.h> 
SoftwareSerial mySerial(11,10); 
Adafruit_GPS GPS(&mySerial); 

char c; 
String NMEA1;
String NMEA2;

#define TRIGGER_PIN 31
#define ECHO_PIN 33
#define MAX_DISTANCE 200 
#define pingSpeed 2000 
#define chipSelect 53 
unsigned long pingTimer1, pingTimer2, pingTimer3, pingTimer4; 
NewPing sonar(31,33,65); 

File myFile; 

void setup() {
  Serial.begin(115200); 
  pingTimer1 = millis() + pingSpeed; 
  pingTimer2 = pingTimer1 + (pingSpeed/2);
  pingTimer3= pingTimer2 + pingSpeed;
  pingTimer4= pingTimer3 + pingSpeed; 
  GPS.begin(9600); 
  GPS.sendCommand("$PGCMD,33,0*6D");
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);

  if(!SD.begin(53)) {
    Serial.println(F("not beginning"));
    return;
  }
  Serial.println(F("its working")); 
  
  
 
  if(! SD.remove("ultrad.txt")){
    Serial.println(F("ultra not working"));
    
  }
  
  if(!SD.remove("gps.txt")){
  Serial.println(F("gps not working"));
  return;
  }
}

void loop() {

 
  Ping();
  delay(1000);
  GPSdata(); 
  delay(1000);
  SDCARD2(); 
  delay(1000);
}

void Ping(){
 
 Serial.print(F("Ping: ")); 
 Serial.print(sonar.ping_cm()); 
 Serial.println(F("cm"));

  if((sonar.ping_cm()<= 65) && (sonar.ping_cm()>0)){
    
   
  myFile = SD.open("ultrad.txt", FILE_WRITE); 
  myFile.print(sonar.ping_cm());
  myFile.print(","); 
  myFile.print("cm");
  myFile.print(",");
  myFile.print(GPS.hour, DEC);myFile.print(':'); 
  myFile.print(GPS.minute, DEC);
  myFile.print(",");  
  myFile.print(GPS.month, DEC); 
  myFile.print("/"); 
  myFile.print(GPS.day, DEC);
  myFile.print("/"); 
  myFile.println(GPS.year, DEC); 
  myFile.close(); 
  }


}

void GPSdata() { 
   
  clearGPS();
  while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
    c=GPS.read(); 
  }
  GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
  NMEA1=GPS.lastNMEA();

  while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
    c=GPS.read(); 
  }
  GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
  NMEA2=GPS.lastNMEA();

  Serial.print(GPS.latitude); 
  Serial.print(F(",")); 
  Serial.println(GPS.longitude);
  Serial.print(GPS.hour, DEC);Serial.print(':'); 
  Serial.println(GPS.minute, DEC);

 
 
}

void clearGPS() {

   while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
    c=GPS.read(); 
  }
  GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
  while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
    c=GPS.read(); 
  }
  GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
   while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
    c=GPS.read(); 
}
}

 
void SDCARD2(){  
  myFile = SD.open("gps.txt", FILE_WRITE); 
  myFile.print(GPS.latitude);
  myFile.print(","); 
  myFile.print(GPS.longitude);
  myFile.print(","); 
  myFile.print(GPS.hour, DEC);myFile.print(':'); 
  myFile.print(GPS.minute, DEC);  
  myFile.print(",");  
  myFile.print(GPS.month, DEC); 
  myFile.print("/"); 
  myFile.print(GPS.day, DEC);
  myFile.print("/"); 
  myFile.println(GPS.year, DEC); 
  myFile.close();
}