This is the first tme using Arduino and I don't have any experience with coding but
I put 2 seperate codes together to try and make it work. So i got the code so far that my gps works together with my SD card.
however my code doesn't loop properly so i dont have constant updates for my gps.
/* TEST
*/
#include <SD.h>
#include <SPI.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
File myFile;
int pinCS = 10; // Pin 10 on Arduino Uno
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
void setup() {
Serial.begin(9600);
pinMode(pinCS, OUTPUT);
ss.begin(GPSBaud);
// SD Card Initialization
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
// Create/Open file
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.println("Writing to file...");
// Write to file
myFile.println("Testing text 1, 2 ,3...");
myFile.close(); // close the file
Serial.println("Done.");
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}
// Reading the file
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("Read:");
// Reading the whole file
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
}
else {
Serial.println("error opening test.txt");
}
}
void loop() {
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.println("Writing to file...");
// Write to file
myFile.println("Testing text 1, 2 ,3...");
while (ss.available() > 0){
gps.encode(ss.read());
if (gps.location.isUpdated()){
Serial.println("Writing to file...");
// Write to fileif (gps.location.isUpdated()){
myFile.println("Latitude= ");
myFile.println(gps.location.lat(), 6);
myFile.println(" Longitude= ");
myFile.println(gps.location.lng(), 6);
myFile.close(); // close the file
Serial.println("Done.");
}
}
}
}