I created a smaller sketch so that i could isolate the cause of the problem. I believe that the problem is related to multiple modes of communication interfering which each other. I don't know enough about these modes to figure out what is wrong. Here is the small test sketch:
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <stdio.h>
//Add the SdFat Libraries
#include <SdFat.h>
#include <SdFatUtil.h>
#include <ctype.h>
// ==================================
#define I2C_Address 0x27
#define I2C_NumChar 20
#define I2C_NumLines 4
#define LED 9
#define BUTTON 7
#define SDCARD 10
#define PRINT_DATA 0
#define DONT_PRINT_DATA 1
#define error(s) sd.errorHalt_P(PSTR(s))
static const int RXPin = 2, TXPin = 3;
static const uint32_t GPSBaud = 9600;
// 4 line, 20 character/line LCD Display
LiquidCrystal_I2C lcd(I2C_Address,I2C_NumChar,I2C_NumLines);
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
// MicroSD Card
//Create the variables to be used by SdFat Library
char fileName[] = "GPS01.txt";//Create an array that contains the name of our file.
SdFat sd;
SdFile myFile;
const uint8_t chipSelect = 8; // CS = 8 on Sparkfun Card
// Global variables
bool haveData=true;
bool firstPass = true;
float homeLat, homeLong, currentLat, currentLong;
unsigned long last = 0UL;
char Latitude[20],Longitude[20],CurrentTime[20],Status[22],CurrentShortTime[20],LatShort[20],LongShort[20];
int lastDistance=0;
unsigned long dist;
static int lastChecksum=0,currentChecksum=0;
void setup()
{
bool result;
int err;
Serial.begin(9600);
ss.begin(GPSBaud);
Serial.println("Software Serial has been initialized");
Serial.println(F("SD Test.ino"));
Serial.println(F("Testing saving data to SD Card"));
Serial.println(F("by Evan Westermann"));
Serial.println();
pinMode(LED,OUTPUT);
pinMode(BUTTON,INPUT);
pinMode(SDCARD, OUTPUT);
pinMode(chipSelect, OUTPUT);
Serial.println("Pin Modes have been set");
Serial.println("Attempting to initialize SD Card");
err = sd.begin(chipSelect, SPI_HALF_SPEED);
if (!err)
sd.errorHalt("Unable to initialize SD Card.");
else
Serial.println("SD Card Initialized successfully");
digitalWrite(chipSelect,HIGH);
Serial.println("Attempting to Open file on SD Card");
err = myFile.open(fileName, O_RDWR | O_CREAT | O_APPEND);
if (!err)
sd.errorHalt("Opening file for write failed");
else
{
Serial.println("Open successful");
myFile.close();
}
Serial.println("Attempting to initialize LCD Display");
lcd.init();
lcd.backlight();
Serial.println("Initialization of LCD Display successful");
}
// This custom version of delay() ensures that the gps object
// is being "fed".
static void smartDelay(unsigned long ms)
{
bool homeSet=false;
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
// if(digitalRead(BUTTON) == HIGH && !homeSet)
// {
// digitalWrite(LED,HIGH);
// homeLat = currentLat;
// homeLong = currentLong;
// Serial.println("Home Position set");
// homeSet = true;
// }
} while (millis() - start < ms);
// digitalWrite(LED,LOW);
}
void SPrintLatitude()
{
currentLat = gps.location.lat();
// Location parameters have 5 decimal places
int dec = (float)(currentLat - (int)currentLat)*100000;
int num = currentLat;
sprintf(LatShort,"%4d.%5d",num,dec);
sprintf(Latitude,"Lat: %s St%02d",LatShort,gps.GetSatellites());
// smartDelay(0);
}
void SPrintLongitude()
{
currentLong = gps.location.lng();
// Location parameters have 5 decimal places
int dec = abs((float)(currentLong - (int)currentLong)*100000);
int num = currentLong;
sprintf(LongShort,"%4d.%05d",num,dec);
sprintf(Longitude,"Long: %s",LongShort);
// smartDelay(0);
}
void SPrintTime(TinyGPSTime &t)
{
int hours;
if (!t.isValid())
sprintf(CurrentTime,"Time: %02d:%02d:%02d ",0,0,0);
else
{
// -4 hours to get to EST
hours = t.hour() - 4;
if(hours < 0)
hours += 24;
sprintf(CurrentTime, "Time: %02d:%02d:%02d ", hours, t.minute(), t.second());
sprintf(CurrentShortTime, "%02d:%02d:%02d ", hours, t.minute(), t.second());
}
// smartDelay(0);
}
void loop()
{
char message[] = "This is a test string";
int err;
Serial.println("In Loop");
// SPrintLatitude();
// SPrintLongitude();
// SPrintTime(gps.time);
// attempt to write data to file
Serial.println("Attempting to Open file on SD Card");
err = myFile.open(fileName, O_RDWR | O_CREAT | O_APPEND);
if (!err)
sd.errorHalt("Opening file for write failed");
else
Serial.println("Open successful");
Serial.println("Writing to file...");
myFile.println(message);
// close the file:
myFile.close();
Serial.println("done.");
smartDelay(1000);
Serial.println("In loop, after SmartDelay");
}
Without the call to SmartDelay(1000) at the end, this works fine and the Serial Monitor shows:
Software Serial has been initialized
SD Test.ino
Testing saving data to SD Card
by Evan Westermann
Pin Modes have been set
Attempting to initialize SD Card
SD Card Initialized successfully
Attempting to Open file on SD Card
Open successful
Attempting to initialize LCD Display
Initialization of LCD Display successful
In Loop
Attempting to Open file on SD Card
Open successful
Writing to file...
done.
In Loop
Attempting to Open file on SD Card
Open successful
Writing to file...
done.
In Loop
Attempting to Open file on SD Card
Open successful
Writing to file...
done.
In Loop
Attempting to Open file on SD Card
Open successful
Writing to file...
done.
In Loop
Attempting to Open file on SD Card
Open successful
Writing to file...
done.
In Loop
Attempting to Open file on SD Card
Open successful
Writing to file...
done.
With the call, the program hangs trying to open the file:
Software Serial has been initialized
SD Test.ino
Testing saving data to SD Card
by Evan Westermann
Pin Modes have been set
Attempting to initialize SD Card
SD Card Initialized successfully
Attempting to Open file on SD Card
The routine SmartDelay is used to obtain GPS data from the device, and use SoftwareSerial for communications and is attached to Arduino pins 2 and 3. Without the SDCard, the GPS device works fine, so i assume the communications are getting garbled, but i don't know how to fix it.