I have a project on a Arduino Uno. It uses a gps device (GP-635T which uses SoftwareSerial.h and TinyGPS++.h), and LCD display (which uses I2C via the LiquidCrystal_I2C.h library. I also have a Sparkfun MicroSD shield stacked onto the Uno.
Everything worked fine until i try to add the code to save data to the SD card. Now i cannot initialize the card. Here is my Setup routine:
#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[] = "GPS_Teat_01.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;
Serial.begin(9600);
ss.begin(GPSBaud);
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);
pinMode(11, OUTPUT);
pinMode(13, OUTPUT);
pinMode(12, INPUT);
lcd.init();
lcd.backlight();
Serial.println("Attempting to initialize SD Card");
if (!sd.begin(chipSelect, SPI_HALF_SPEED))
sd.initErrorHalt();
Serial.println("SD Card Initialized successfully");
digitalWrite(chipSelect,HIGH);
}
And here is where i try to initialize and write to the SD card
void SaveData(int option)
{
char outData[100];
bool result;
char msg[100];
lastChecksum = currentChecksum;
currentChecksum = gps.failedChecksum();
sprintf(outData,"%s,%s,%s,%05d,%05d,%05d,%05d",
CurrentShortTime,LatShort,LongShort,dist,abs(gps.GetSatellites()),currentChecksum-lastChecksum);
// save data to SD Card
// open the file for write at end like the Native SD library
if (!myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END)) {
sprintf(msg,"Opening file for write failed. Error = %d. Data = %d",sd.card()->errorCode(),
sd.card()->errorData());
sd.errorHalt(msg);
}
// if the file opened okay, write to it:
Serial.print("Writing to test.txt...");
myFile.println(outData);
// close the file:
myFile.close();
Serial.println("done.");
// write out data to Serial Monitor
if(option == PRINT_DATA)
Serial.println(outData);
}
When it starts i get the following error code from myFile.open command:
error: Opening file for write failed. Error = 0. Data = 0
I don't even see an error code 0. If i comment out the section in SaveData() that does the write, the program runs fine. Can someone help me with this, thanks.