Hello Guys,
For my project I am using 3 gps modules(Neo6m gps module) with arduino mega 2560 and I want to get measurements from them every 1 second that is 1st gps will give 1st reading after 1 sec 2nd gps will give 2nd reading and then after 1 sec 3rd gps will give 3rd reading. Again 1st gps will give 4th reading 2nd will give 5th and 3rd will give 6th reading with 1 sec interval. But I am getting the readings not in correct sequence also the time shown in the .txt file is not correct either.
Here is my code
#include "SD.h"
#include <SPI.h>
#include <TinyGPS++.h>
TinyGPSPlus gps1,gps2,gps3;
String day, month, year, hour, minute, second, Date, Time, Data;
double Latitude, Longitude, Altitude;
String gps_1 = "GPS1", gps_2 = "GPS2", gps_3 = "GPS3";
String filename = "GPSLOG";
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin(53)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
int count = 0;
bool flag = false;
while(!flag){
if(SD.exists(filename + ".txt")){
count++;
filename = "GPSLOG" + (String)count;
Serial.print(filename);
}else{
flag = true;
}
}
File myFile = SD.open((filename+".txt"), FILE_WRITE);
if (myFile) {
myFile.println( "GPS, Latitude, Longitude, Date and Time \r\n");
myFile.close();
} else {
Serial.println("error opening GPS_data.txt");
}
Serial1.begin(9600);
Serial2.begin(9600);
Serial3.begin(9600);
}
void loop() {
if (Serial1.available() > 0){
if (gps1.encode(Serial1.read())){
obtain_data(gps1,gps_1,filename);
}
}
if (Serial2.available() > 0){
if (gps2.encode(Serial2.read())){
obtain_data(gps2,gps_2, filename);
}
}
if (Serial3.available() > 0){
if (gps3.encode(Serial3.read())){
obtain_data(gps3,gps_3, filename);
}
}
if (millis() > 10000 && gps1.charsProcessed() < 10 && gps2.charsProcessed() < 10 && gps3.charsProcessed() < 10)
{
Serial.println("GPS NOT DETECTED!");
while(true);
}
}
void obtain_data(TinyGPSPlus gps, String sensor, String filename)
{
if (gps.location.isValid())
{
Latitude = gps.location.lat();
Longitude = gps.location.lng();
Altitude = gps.altitude.meters();
//File myFile = SD.open((filename+".txt"), FILE_WRITE);
}
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 = sensor + "," + (String)Latitude + "," + (String)Longitude + "," + (String)Altitude + "," + Date + "," + Time;
Serial.print("Save data: ");
Serial.println(Data);
File myFile = SD.open((filename+".txt"), FILE_WRITE);
if (myFile) {
Serial.print("GPS logging to " + filename+".txt"+"...");
//myFile.print(Data);
myFile.print(sensor);
myFile.print(",");
myFile.print(gps.location.lat(),6);
myFile.print(",");
myFile.print(gps.location.lng(),6);
myFile.print(",");
myFile.print(gps.altitude.meters(),3);
myFile.print(",");
myFile.print(Date+","+Time);
myFile.println("");
myFile.close();
Serial.println("done.");
} else {
Serial.println("error opening GPS_data.txt");
}
Serial.println();
delay(1000);
}
and here is my data in the .txt file
GPS, Latitude, Longitude, Date and Time
GPS3,0.000000,0.000000,0.000,,
GPS2,0.000000,0.000000,0.000,,
GPS1,0.000000,0.000000,0.000,,
GPS3,0.000000,0.000000,0.000,,
GPS1,0.000000,0.000000,0.000,,
GPS2,0.000000,0.000000,0.000,,
GPS3,0.000000,0.000000,0.000,,
GPS3,48.780967,11.475488,0.000,23-3-2023,14:39:50
GPS2,48.781017,11.475526,0.000,23-3-2023,14:39:50
GPS1,48.780933,11.475433,0.000,23-3-2023,14:39:50
GPS3,48.780967,11.475488,0.000,23-3-2023,14:39:50
GPS2,48.781017,11.475526,0.000,23-3-2023,14:39:50
GPS1,48.780933,11.475433,0.000,23-3-2023,14:39:50
GPS1,48.780933,11.475433,0.000,23-3-2023,14:39:50
GPS2,48.781017,11.475526,0.000,23-3-2023,14:39:50
GPS3,48.780975,11.475486,0.000,23-3-2023,14:39:59
GPS2,48.781013,11.475529,0.000,23-3-2023,14:39:59
GPS1,48.780948,11.475444,0.000,23-3-2023,14:39:59
GPS3,48.780975,11.475486,0.000,23-3-2023,14:39:59
GPS2,48.781013,11.475529,0.000,23-3-2023,14:39:59
GPS1,48.780948,11.475444,0.000,23-3-2023,14:39:59
GPS1,48.780948,11.475444,0.000,23-3-2023,14:39:59
GPS3,48.780975,11.475500,0.000,23-3-2023,14:40:7
GPS2,48.781013,11.475537,0.000,23-3-2023,14:40:7
GPS1,48.780952,11.475448,0.000,23-3-2023,14:40:7
GPS3,48.780975,11.475500,0.000,23-3-2023,14:40:7
GPS2,48.781013,11.475537,0.000,23-3-2023,14:40:7
GPS1,48.780952,11.475448,0.000,23-3-2023,14:40:7
GPS3,48.780975,11.475500,0.000,23-3-2023,14:40:7
GPS2,48.781013,11.475537,0.000,23-3-2023,14:40:7
GPS1,48.780952,11.475448,0.000,23-3-2023,14:40:7
GPS3,48.781013,11.475542,0.000,23-3-2023,14:40:17
GPS2,48.781044,11.475566,0.000,23-3-2023,14:40:17
GPS1,48.780990,11.475477,0.000,23-3-2023,14:40:17
GPS3,48.781013,11.475542,0.000,23-3-2023,14:40:17
GPS2,48.781044,11.475566,0.000,23-3-2023,14:40:17
GPS1,48.780990,11.475477,0.000,23-3-2023,14:40:17
GPS2,48.781044,11.475566,0.000,23-3-2023,14:40:17
GPS2,48.781127,11.475605,0.000,23-3-2023,14:40:25
GPS3,48.781089,11.475604,0.000,23-3-2023,14:40:25
GPS1,48.781059,11.475521,0.000,23-3-2023,14:40:25
GPS2,48.781127,11.475605,0.000,23-3-2023,14:40:25
GPS3,48.781089,11.475604,0.000,23-3-2023,14:40:25
GPS1,48.781059,11.475521,0.000,23-3-2023,14:40:25
GPS1,48.781059,11.475521,0.000,23-3-2023,14:40:25
GPS2,48.781127,11.475605,0.000,23-3-2023,14:40:25
GPS3,48.781177,11.475682,0.000,23-3-2023,14:40:34
GPS2,48.781219,11.475651,0.000,23-3-2023,14:40:34
GPS1,48.781139,11.475566,0.000,23-3-2023,14:40:34
GPS3,48.781177,11.475682,0.000,23-3-2023,14:40:34
GPS2,48.781219,11.475651,0.000,23-3-2023,14:40:34
GPS1,48.781139,11.475566,0.000,23-3-2023,14:40:34
GPS3,48.781177,11.475682,0.000,23-3-2023,14:40:34
GPS1,48.781139,11.475566,0.000,23-3-2023,14:40:34
GPS2,48.781219,11.475651,0.000,23-3-2023,14:40:34
GPS3,48.781299,11.475773,0.000,23-3-2023,14:40:44
GPS2,48.781341,11.475717,0.000,23-3-2023,14:40:44
GPS1,48.781257,11.475636,0.000,23-3-2023,14:40:44
GPS3,48.781299,11.475773,0.000,23-3-2023,14:40:44
GPS2,48.781341,11.475717,0.000,23-3-2023,14:40:44
GPS1,48.781257,11.475636,0.000,23-3-2023,14:40:44
GPS3,48.781299,11.475773,0.000,23-3-2023,14:40:44
GPS1,48.781257,11.475636,0.000,23-3-2023,14:40:44
GPS2,48.781341,11.475717,0.000,23-3-2023,14:40:44
GPS3,48.781433,11.475853,0.000,23-3-2023,14:40:54
GPS2,48.781475,11.475785,0.000,23-3-2023,14:40:54
GPS1,48.781372,11.475707,0.000,23-3-2023,14:40:54
GPS3,48.781433,11.475853,0.000,23-3-2023,14:40:54
GPS2,48.781475,11.475785,0.000,23-3-2023,14:40:54
GPS1,48.781372,11.475707,0.000,23-3-2023,14:40:54
GPS3,48.781433,11.475853,0.000,23-3-2023,14:40:54
GPS1,48.781372,11.475707,0.000,23-3-2023,14:40:54
GPS2,48.781475,11.475785,0.000,23-3-2023,14:40:54
GPS3,48.781562,11.475852,0.000,23-3-2023,14:41:4
GPS2,48.781612,11.475845,0.000,23-3-2023,14:41:4
GPS1,48.781509,11.475793,0.000,23-3-2023,14:41:4
GPS3,48.781562,11.475852,0.000,23-3-2023,14:41:4
GPS2,48.781612,11.475845,0.000,23-3-2023,14:41:4
GPS1,48.781509,11.475793,0.000,23-3-2023,14:41:4
GPS2,48.781612,11.475845,0.000,23-3-2023,14:41:4
GPS3,48.781562,11.475852,0.000,23-3-2023,14:41:4
GPS1,48.781509,11.475793,0.000,23-3-2023,14:41:4
GPS3,48.781627,11.475646,0.000,23-3-2023,14:41:14
GPS2,48.781650,11.475724,0.000,23-3-2023,14:41:14
GPS1,48.781585,11.475650,0.000,23-3-2023,14:41:14
GPS3,48.781627,11.475646,0.000,23-3-2023,14:41:14
GPS2,48.781650,11.475724,0.000,23-3-2023,14:41:14
GPS1,48.781585,11.475650,0.000,23-3-2023,14:41:14
GPS3,48.781627,11.475646,0.000,23-3-2023,14:41:14
GPS2,48.781650,11.475724,0.000,23-3-2023,14:41:14
GPS1,48.781585,11.475650,0.000,23-3-2023,14:41:14
GPS3,48.781627,11.475646,0.000,23-3-2023,14:41:14
GPS2,48.781650,11.475724,0.000,23-3-2023,14:41:14
GPS1,48.781585,11.475650,0.000,23-3-2023,14:41:14
GPS3,48.781707,11.475388,0.000,23-3-2023,14:41:27
GPS2,48.781707,11.475461,0.000,23-3-2023,14:41:27
GPS1,48.781681,11.475405,0.000,23-3-2023,14:41:27
GPS3,48.781707,11.475388,0.000,23-3-2023,14:41:27
GPS2,48.781707,11.475461,0.000,23-3-2023,14:41:27
GPS1,48.781681,11.475405,0.000,23-3-2023,14:41:27
GPS3,48.781707,11.475388,0.000,23-3-2023,14:41:27
GPS1,48.781681,11.475405,0.000,23-3-2023,14:41:27
GPS2,48.781707,11.475461,0.000,23-3-2023,14:41:27
GPS3,48.781707,11.475388,0.000,23-3-2023,14:41:27
GPS1,48.781681,11.475405,0.000,23-3-2023,14:41:27
GPS3,48.781730,11.475377,0.000,23-3-2023,14:41:39
GPS2,48.781707,11.475389,0.000,23-3-2023,14:41:39
GPS1,48.781688,11.475375,0.000,23-3-2023,14:41:39
GPS3,48.781730,11.475377,0.000,23-3-2023,14:41:39
GPS2,48.781707,11.475389,0.000,23-3-2023,14:41:39
GPS1,48.781688,11.475375,0.000,23-3-2023,14:41:39
GPS3,48.781730,11.475377,0.000,23-3-2023,14:41:39
GPS1,48.781688,11.475375,0.000,23-3-2023,14:41:39