So i should just add that to my code? If i am not mistaken you've declared the dataString as a string of all the different strings that were being printed, right?
before the void setup:
char longBuffer [20]; // may need resizing
char latBuffer [20]; // may need resizing
char dataString [80];
In the loop fn:
dtostrf (latBuffer, gps.location.lat(), 6); // may need to put this in a conditional in case the fix isn't valid
dtostrf (longBuffer, gps.location.lng(), 6);
sprintf (dataString, "%s,%s, @ %02d:%02d:%02d", latBuffer, longBuffer, hours, minutes, seconds);
Then the
dataFile.println(dataString)
should write it to the file on the SD.
After Compiling,
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
/*
GPS
Rx from GPS to pin 2
Tx from GPS to pin 3
SD
MOSI - pin 11
MISO - pin 12
CLK - pin 13
CS - pin 4
*/
static const int RXPin = 3, TXPin = 2;
static const uint32_t GPSBaud = 9600;
//from forum
char longBuffer [20]; // may need resizing
char latBuffer [20]; // may need resizing
char dataString [80];
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
//SD
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
ss.begin(GPSBaud);
///Serial.println(F("FullExample.ino"));
//Serial.println(F("An extensive example of many interesting TinyGPS++ features"));
//Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
//Serial.println(F("by Mikal Hart"));
Serial.println();
Serial.println(F("Latitude Longitude Date Time Date"));
Serial.println(F("(deg) (deg) (UTC+0) Age"));
Serial.println(F("---------------------------------------------------------------------------------------------------------------------------------------"));
}
void loop()
{
dtostrf (latBuffer, gps.location.lat(), 6); // may need to put this in a conditional in case the fix isn't valid
dtostrf (longBuffer, gps.location.lng(), 6);
sprintf (dataString, "%s,%s, @ %02d:%02d:%02d", latBuffer, longBuffer);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
//dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
//printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
//printInt(gps.hdop.value(), gps.hdop.isValid(), 5);
printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
//printInt(gps.location.age(), gps.location.isValid(), 5);
printDateTime(gps.date, gps.time);
//printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
//printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
//printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
//printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.value()) : "*** ", 6);
unsigned long distanceKmToLondon =
(unsigned long)TinyGPSPlus::distanceBetween(
gps.location.lat(),
gps.location.lng(),
LONDON_LAT,
LONDON_LON) / 1000;
//printInt(distanceKmToLondon, gps.location.isValid(), 9);
double courseToLondon =
TinyGPSPlus::courseTo(
gps.location.lat(),
gps.location.lng(),
LONDON_LAT,
LONDON_LON);
//printFloat(courseToLondon, gps.location.isValid(), 7, 2);
const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon);
// printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6);
//printInt(gps.charsProcessed(), true, 6);
//printInt(gps.sentencesWithFix(), true, 10);
//printInt(gps.failedChecksum(), true, 9);
Serial.println();
smartDelay(1000);
if (millis() > 5000 && gps.charsProcessed() < 10)
Serial.println(F("No GPS data received: check wiring"));
}
// This custom version of delay() ensures that the gps object
// is being "fed".
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
static void printFloat(float val, bool valid, int len, int prec)
{
if (!valid)
{
while (len-- > 1)
Serial.print('*');
Serial.print(' ');
}
else
{
Serial.print(val, prec);
int vi = abs((int)val);
int flen = prec + (val < 0.0 ? 2 : 1); // . and -
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i = flen; i < len; ++i)
Serial.print(' ');
}
smartDelay(0);
}
static void printInt(unsigned long val, bool valid, int len)
{
char sz[32] = "*****************";
if (valid)
sprintf(sz, "%ld", val);
sz[len] = 0;
for (int i = strlen(sz); i < len; ++i)
sz[i] = ' ';
if (len > 0)
sz[len - 1] = ' ';
Serial.print(sz);
smartDelay(0);
}
static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
if (!d.isValid())
{
Serial.print(F("********** "));
}
else
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
Serial.print(sz);
}
if (!t.isValid())
{
Serial.print(F("******** "));
}
else
{
char sz[32];
sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
Serial.print(sz);
}
printInt(d.age(), d.isValid(), 5);
smartDelay(0);
}
static void printStr(const char *str, int len)
{
int slen = strlen(str);
for (int i = 0; i < len; ++i)
Serial.print(i < slen ? str[i] : ' ');
smartDelay(0);
}
i have this :S
Arduino: 1.5.8 (Windows 7), Board: "Arduino Uno"
Build options changed, rebuilding all
GPD_SD_trial.ino: In function 'void loop()':
GPD_SD_trial.ino:69:44: error: cannot convert 'char*' to 'double' for argument '1' to 'char* dtostrf(double, signed char, unsigned char, char*)'
GPD_SD_trial.ino:70:45: error: cannot convert 'char*' to 'double' for argument '1' to 'char* dtostrf(double, signed char, unsigned char, char*)'
Error compiling.