Hello,
I'm looking for a little guidance on what might I'm doing wrong when I try to write strings to an SD card.
I'm aware that strings are not necessarily the most efficient way to store information, and I'm trying to learn how to convert the data types to character arrays. I'd request that people's replies be without some of the disparagement I've seen on other forum questions regarding strings, and perhaps some advice on casting floats, ints etc as character arrays (or anything else more appropriate) could be substituted.
I have a SD card writer that I'm communicating with an Arduino Uno, with
// ** GND
// ** Vcc 3.3 V
// ** CS - pin 4
// ** MOSI - pin 11
// ** CLK - pin 13
// ** MISO - pin 12
I'm attempting to write GPS, tiltmeter and magnetometer data to the card
I can write the data I need to both the Serial Monitor with
Serial.println(gpsString);
Serial.println(tiltString);
Serial.println(magString);
I've built the strings just by concatenating floats and ints casts as strings. An example output of what I see on the serial monitor (without trying to write to the SD card) is:
20:5:24:14:20:28.0 sxxx.xxxxxxxxx:xxx.xxxxxxxxx
-5 -79 17
1614 6509 -1
(the s and x are actual numbers that I've removed. They are my lat and long)
However, the call I'm using to write to the SD card seems to be corrupting the strings somehow. This call:
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(gpsString); //note that GPS use UTC time.
dataFile.println(tiltString);
dataFile.println(magString);
dataFile.close();
}
or even just
File dataFile = SD.open("datalog.txt", FILE_WRITE);
dataFile.close();
Will produce return the lines:
20:5:24:14:16:19.0
-4 -81 18
1618 6507 -1
i.e. without the lat and long values written, though the date and time are - even though I've cast them all as strings - i. e.
String gpsString = String(fix.dateTime.year) + ":" + String(fix.dateTime.month) + ":" + String(fix.dateTime.date)+":" ;
gpsString += String(fix.dateTime.hours) + ":" + String(fix.dateTime.minutes) + ":" +String(fix.dateTime.seconds);
gpsString += "." + String(fix.dateTime_ms())+" ";
gpsString += String(fix.latitude(),9) + ":" + String(fix.longitude(),9)+" ";
When I try to write to the SD card, the data saved to the cars has the same content as above (i.e. , GPS data including date time without lat, long, Tiltmeter and magnetometer are all saved. The only data that is absent is the lat and long.
I'm unsure, but I suspect I'm not using strings or variable types properly - and if that is the case, i'm not sure how to solve the problem - e.g. converting float or int variables into character arrays. Either way, does anyone have any thoughts on my problem?