GPS SD Card Arduino Mega Projekt

I want to get location from 3 different GPS modules which placed 1m apart from each other. Also 1 16gb SDHC microSD card with SD card shield. All of them are assembled on arduino mega 2560.
Some of my components will arrive in 4 days. So I just wanted to know will this code work for the following circuit diagram.
Please feel free to correct the circuit diagram as well.
I am open for discussions, Thank You.

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

File myFile;
int GPSBaud = 9600;
uint8_t csPin = 53;
String filename = "GPS_LOG";
char gps1[4] = "GPS1", gps2[4] = "GPS2", gps3[4] = "GPS3";
String Latitude, Longitude, Altitude, day, month, year, hour, minute, second, Date, Time, Data, senosrNo;
TinyGPSPlus gps;
bool flagSD = true; 
int count = 0;
String updatedFileName;


void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; 
  }
  Serial.print("Initializing SD card..."); 

  
  if(!SD.begin(csPin)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  Serial.println("Creating GPS_LOG.csv...");

  while(flagSD){
    if (SD.exists(filename+".csv")){
      count = count+1;
      filename = filename + String(count);
    }else{
      flagSD = false;
    }
  }
  updatedFileName = filename+".csv";
  myFile = SD.open(updatedFileName,FILE_WRITE);
  if(myFile){
    myFile.println( "Latitude, Longitude, Altitude, Date and Time \r\n");
    myFile.close();
  }else{
    Serial.println();
  }

  Serial1.begin(GPSBaud);
  Serial2.begin(GPSBaud);
  Serial3.begin(GPSBaud);
}

void loop() { 
  while(Serial1.available())
    if (gps.encode(Serial1.read())){
      obtain_data(updatedFileName, gps1);
    }
  while(Serial2.available())
  if (gps.encode(Serial2.read())){
    obtain_data(updatedFileName, gps2);
  }
  while(Serial3.available())
  if (gps.encode(Serial3.read())){
    obtain_data(updatedFileName, gps3);
  }

}

void obtain_data(String filename, char sensor)
{
  if (gps.location.isValid())
  {
    Latitude = gps.location.lat();
    Longitude = gps.location.lng();
    Altitude = gps.altitude.meters();
  }
  else
  {
    Serial.println("Location is not available");
  }
  
  if (gps.date.isValid())
  {
    month = gps.date.month();
    day = gps.date.day();
    year = gps.date.year();
    Date = day + "-" + month + "-" + year;
     Serial.println(Date);
  }
  else
  {
    Serial.print("Date Not Available");
  }
 
  Serial.print("Time: ");
  if (gps.time.isValid())
  {
    if (gps.time.hour() < 10) ;
    hour = gps.time.hour();
    if (gps.time.minute() < 10);
    minute = gps.time.minute();
    if (gps.time.second() < 10) ;
    second = gps.time.second();
    Time = hour + ":" + minute + ":" + second;
  Serial.println(Time);
  }
  else
  {
    Serial.print("Time Not Available");
  }
  
  Data = Date + "," + Time + "," + Latitude + "," + Longitude + "," + Altitude;
  Serial.print("Save data for " + String(sensor) + ": ");
  Serial.println(Data);
  myFile = SD.open(filename, FILE_WRITE);

  if (myFile) {
    Serial.print("GPS logging to "+ filename + "...");
    myFile.println(Data);
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening " + filename);
  } 
 Serial.println();
 //delay(10000);
}

And why are you doing that ?

You have one GPS connected to pins 10,11 ?

The Mega has 3 spare hardware serial ports.

I know one GPS is absolutely Okay but my project supervisor wants to try getting location with 3 GPS. So decision is not mine.

So GPS3 can be connected to RX3 TX3. right?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.