Recently I have been having trouble getting my code to write to a text document on an SD card. The code creates the files that I told it to but it doesn't seem to write any data to the selected file. I am using a Dexter Industries GPS Module on an Arduino Nano. How to Use the Arduino GPS Shield For the First Time For Beginners I did notice that once removing dgps.updategga(); in the code that the problem seems to go away. The commented out section was a test to see if RAM had an effect on it.
Here's the code:
#include "string.h"
#include "ctype.h"
#include "SoftwareSerial.h"
#include "dGPS.h"
#include <SPI.h>
#include <SD.h>
File myFile;
// Software serial TX & RX Pins for the GPS module
// Initiate the software serial connection
int ledPin = 13; // LED test pin
dGPS dgps = dGPS(); // Construct dGPS class
void setup() {
pinMode(8, OUTPUT);
Serial.end(); // Close any previously established connections
Serial.begin(9600); // Serial output back to computer. On.
dgps.init(); // Run initialization routine for dGPS.
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
else
{
Serial.println("initialization done.");
}
delay(1000);
}
void loop() {
dgps.update(0, 0);
Serial.print("Latitude: ");
Serial.print(dgps.Lat(), 6); // Latitude - in DD.MMSSSS format (decimal-degrees format) (D-Degree; M-Minute; S-Second)
Serial.println(" degrees");
Serial.print("Longitude: ");
Serial.print(dgps.Lon(), 6); // Longitude - in DD.MMSSSS format (decimal-degrees format) (D-Degree; M-Minute; S-Second)
Serial.println(" degrees");
/*Serial.print("UTC Date(DDMMYY): ");
Serial.println(dgps.Date()); // UTC date. Date is in format: DDMMYY (D - Day; M - Month; Y-Year)
Serial.print("UTC Time: ");
Serial.println(dgps.Time()); // .Time returns the UTC time (GMT) in HHMMSS, 24 huor format (H-Hour; M-Minute; S-Second)
Serial.print("Status: ");
Serial.println(dgps.Status()); // A - Satellites acquired and a valid signal. V - No sats and not a valid signal.
Serial.print("Velocity: ");
Serial.print(dgps.Vel(), 6); // Velocity, in knots.
Serial.println(" knots");
Serial.print("Heading: ");
Serial.print(dgps.Head(), 6); // Heading, in degrees
Serial.println(" degrees");
Serial.print("Received CheckSum: ");
Serial.println(dgps.Checks()); //Checksum received from packet
Serial.print("Computed Checksum: ");
Serial.println(dgps.Checked(), HEX); //Checksum computed */
dgps.updategga(); //updates the values of Number of Satellites, HDOP and Altitude
Serial.print("HDOP: ");
Serial.println(dgps.Hdop()); // HDOP
Serial.print("Altitude: ");
Serial.print(dgps.Alti()); // Altitude (in meters) above sea-level
Serial.println(" meters above sea level");
Serial.print("Satellites in use: ");
Serial.println(dgps.SatView()); // Number of Satellites in use
Serial.println("");
if (dgps.SatView() > 0) {
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
}
// LONGITUDE RAW CREATION
myFile = SD.open("RAWLONG.txt", FILE_WRITE);
if (myFile) {
myFile.println(dgps.Lon(), 6);
myFile.close();
}
// LATITUDE RAW CREATION
myFile = SD.open("RAWLAT.txt", FILE_WRITE);
if (myFile) {
myFile.println(dgps.Lat(), 6);
myFile.close();
}
// ALTITUDE RAW CREATION
myFile = SD.open("RAWALT.txt", FILE_WRITE);
if (myFile) {
myFile.println(dgps.Alti());
myFile.close();
}
}
I did use the Read-Write SD code program on the Arduino IDE. The SD card worked perfectly and without flaw. It created the file I wanted to create and wrote what i wanted to it. Once I introduced it into this GPS code it started to not work and cause problems.
Correct, everything other than the SD is working perfectly. All Latitude and Longitude readings are correct. The only problem is getting it to write to the text document.
dGPS uses SoftwareSerial, which is scary, especially without mentioning it. SoftwareSerial does nasty things with interrupts and often creates conflicts with other libraries. This will not solve your problem as the method update(x,y) makes use of it as well as updategga().
Is there a way to not use Software Serial with the dGPS module. I know you can change the Software Serial pins in one of the library files but is there a way to not use it all together?
What i mean by 'it seems to go away' is that once i remove updategga() it starts to write to the file that it created. It sometimes doesn't work but after a quick reset it starts to work again.
The library uses SoftwareSerial to communicate with the GPS, they are just serial read functions. You could replace that if you come up with another method of communicating. Or, use a different board with more serial ports (m2560 comes to mind). Or, find a different library that doesn't use SoftwareSerial. What all libraries will have in common though, is the need to communicate. With most GPS sensors I'm aware of, this means via a USB host or TTL serial.
MagicMan:
Is there a way to not use Software Serial with the dGPS module. I know you can change the Software Serial pins in one of the library files but is there a way to not use it all together?
It a serial GPS so it needs to use either software serial or a hardware serial port.
Plug it into say a Mega2560 or Due Arduino and fit a wire from the GPS TX output to a hardware serial input.
Not good design on the PCB really, if the board was about 1mm longer it could have picked up the Serial3 hardware pins on ATmega2560 and Due boards.
To use hardware serial with the GPS, connect the GPS output to the hardware serial input (Pin 0, RXD) after uploading your program.
Your program would use Serial.begin(), Serial.available() and Serial.read() instead of the Software Serial equivalents. However, after glancing through the very poorly written dGPS library, I see that it already uses both software and hardware serial.
So, I strongly recommend that you use a different GPS library, like TinyGPS++ or NeoGPS. One or both should work with the SD card. Consider also using a different GPS module. They are quite cheap these days and you are not stuck with the shield.
Finally, you can take the dGPS shield off of the Arduino, and use it as a stand alone module, making only the three connections that are actually useful: 5V, GND and GPS TXO.
The dGPS module has been taken off the shield. Everything is hooked up correctly. The 2 libraries you stated have not worked with this GPS module. Also when editing the original library to use hardware serial, the program would not communicate with the GPS module.