Hi, I would like to ask you for elementary question.
I am using GPS Neo-6M module, Arduino Nano + SD card module.
And I am trying to get data from GPS, and write them in some specific format to the file on SD card.
I tried GPS and SD card separately (GPS was getting data and writing to serial monitor, program was writing some data to SD card). Now when I tried to glue it together...... It doesn't work.
What doesn't work?
- I am not getting all data from GPS module anymore.
- I am not able to set up how often I want to read data from GPS and save them. (I know that I cannot use Delay)
How often to receive data?
- I want to be able decide how often I want to collect data about position and how often to save them depending of what I get from GPS, for example:
- if my speed will be < 1 km/h => I want to ask GPS module for data after 60 sec.
- If my speed is bigger than 1 km/h I want to ask for data every 30 seconds.
- if my speed is bigger than 50 km/h I want receive and save data every 5 seconds
Thank you for your help of advice. Connecting more devices is something new for me and communication between devices is something what I didn't handle well
#include <TinyGPS++.h> // Include TinyGPS++ library
#include <SoftwareSerial.h> // Include software serial library
#include <SPI.h>
#include <SD.h>
int PositiveLED = 8;
int ErrorLED = 7;
TinyGPSPlus gps;
File myFile;
#define S_RX 5 // Define software serial RX pin
#define S_TX 4 // Define software serial TX pin
SoftwareSerial SoftSerial(S_RX, S_TX); // Configure SoftSerial library
String LatitudeString;
String LongitudeString;
String AltitudeString;
String SpeedString;
String TimeString;
String DateString;
String SatellitesString;
void setup(void)
{
pinMode(8, OUTPUT); // diode
pinMode(7, OUTPUT); // error diode
Serial.begin(9600);
SoftSerial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10))
{
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
}
void loop()
{
while (SoftSerial.available() > 0)
{
if (gps.encode(SoftSerial.read()))
{
String Date = "";
if (gps.date.isValid())
Date = "Date: " + String(gps.date.year()) + "-" + String(gps.date.month()) + "-" + String(gps.date.day());
String Time = "";
if (gps.time.isValid())
Time = "Time (GMT): " + String(gps.time.hour()) + ":" + gps.time.minute() + ":" + gps.time.second();
String Speed = "";
if (gps.speed.isValid())
Speed = "Speed: " + leadSpaceFourPlusDecimals(gps.speed.kmph(),2) + " km/h";
String Precision = "";
if (gps.hdop.isValid())
Precision = "Prec.: " + leadZeroSpecificDigits(gps.hdop.value(),3);
String LatLog = "";
if (gps.location.isValid())
LatLog = "Latitude: " + leadSpaceFourPlusDecimals(gps.location.lat(),6) +";" + " Longitude: " + leadSpaceFourPlusDecimals(gps.location.lng(),6);
String Message = Date + ";" + Time + "; " + LatLog + "; " + Speed + "; " + Precision;
Serial.println(Message);
addLineToFile("Gps.txt", Message);
}
}
}
void addLineToFile(char filename[], String line)
{
Serial.println("Try to open file");
myFile = SD.open(filename, FILE_WRITE);
if (myFile)
{
digitalWrite(8,HIGH); // positive signal of writing
Serial.println("File open!");
myFile.println(line);
Serial.println("Wrote: "+line);
digitalWrite(8,LOW);
}
else
{
digitalWrite(7,HIGH); // Negative signal of writing
Serial.println("Cannot open file");
digitalWrite(7,LOW);
}
myFile.close();
Serial.println("File closed.");
}
String leadZeroSpecificDigits(int input, int digits)
{
String result = "";
if(input < 10)
result = "00";
if(input >= 10 && input < 100)
result = "0";
result = result + input;
return result;
}
String leadZeroTwoDigits(int input)
{
String result = "";
if(input < 10)
result = "0";
result = result + input;
return result;
}
String leadSpaceFourPlusDecimals(double input, int decimal)
{
String result = "";
if(input >= 0)
result = " ";
if(input < 100 || input > -100)
result = result + " ";
if(input < 10 || input > -10)
result = result + " ";
result = result + String(input, decimal);
return result;
}