Hi all,
Im currently building a gps logger. Im having trouble with writing the gps data to the SD card. The code always has a problem writing/opening to the file. I have used the datalogger example in the Arduino IDE and it works all fine. After the line: File dataFile = SD.open("log.txt", FILE_WRITE); the program will turn on the red led pin 6, instead of writing any data to the SD card.
Could anybody suggest why my code is not working?
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <SD.h>
//3(rx) and 4(tx). for gps.
TinyGPS gps;
SoftwareSerial ss(3, 4);
Sd2Card card;
SdVolume volume;
SdFile root;
const int chipSelect = 10;
unsigned long time, duration;
void setup()
{
duration=300000;//5 minutes
Serial.begin(115200);
ss.begin(9600);
pinMode(6, OUTPUT); //red led
pinMode(7, OUTPUT); //green led
if (!card.init(SPI_QUARTER_SPEED, chipSelect)) {
Serial.println("initialization failed.");
digitalWrite(6, HIGH);//turn led red
return;
}
else {
Serial.println("Wiring is correct and a card is present.");
digitalWrite(7, HIGH);//led green 3 seconds
delay(3000);
digitalWrite(7, LOW);
}
}
void loop()
{
time=millis();
if(time<duration){///&&//switch active
bool newData = false;
newData=gpsSetup(newData);
if (newData)
{
long lat, lon;
unsigned long age,time,date,speed,alt;
gps.get_position(&lat, &lon, &age);
gps.get_datetime(&date,&time);
speed=gps.speed();
alt=gps.altitude();
/*
Serial.print("LAT=");
Serial.print(lat);
Serial.print(" LON=");
Serial.print(lon);
Serial.print(" AGE=");
Serial.print(age);
Serial.println();*/
File dataFile = SD.open("log.txt", FILE_WRITE);
if (dataFile){
dataFile.print("test");
//dataFile.print(" ");
//dataFile.println(lon);
dataFile.close();
digitalWrite(7, HIGH); //quick flash of led to tell if data has been written?
delay(50);
digitalWrite(7, LOW);
}
else {
digitalWrite(6, HIGH);//error opening file
}
//quick flash of led to tell if data has been written?
}}
else{
}
/////////function definitons////////////
bool gpsSetup(bool newData){
for (int i=0;i<1000;i++){
while (ss.available()){
char c = ss.read();
if (gps.encode(c)) // Did a new valid sentence come in?
newData = true;
}
}
return newData;
}
Thanks, help much appreciated.
edit: forgot SD.begin