I have some problems with my GPS Logger. U use Arduino Uno, NEO - 6M, SD card. Problem is in function void loop. I enter inside, but stuck on the "while loop", don't even get inside this loop. Here is code I use:
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <SPI.h>
#include <SD.h>
#define SD_CS 4
#define FILE_BASE_NAME "Trac"
TinyGPS gps;
SoftwareSerial serial(10, 11);
File myFile;
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[] = FILE_BASE_NAME "00.csv";
void setup(){
Serial.begin(9600);
serial.begin(9600);
Serial.println("SD card initialization...");
if(!SD.begin(SD_CS))
{
Serial.println("Failed! Check card");
//while(1);
}
Serial.println("Initialization done");
while (SD.exists(fileName)){
if (fileName[BASE_NAME_SIZE + 1] != '9')
{
fileName[BASE_NAME_SIZE + 1]++;
}
else if (fileName[BASE_NAME_SIZE] != '9')
{
fileName[BASE_NAME_SIZE + 1] = '0';
fileName[BASE_NAME_SIZE]++;
}
else{
Serial.println("Can't create file name");
//return;
}
}
myFile = SD.open(fileName, FILE_WRITE);
if (!myFile) {
Serial.println("open failed");
//return;
}
Serial.print("opened: ");
Serial.println(fileName);
}
void loop(){
while (serial.available())
{
int c = serial.read();
if (gps.encode(c))
{
long lat, lon;
unsigned long fix_age;
gps.get_position(&lat, &lon, &fix_age);
Serial.print("Latitude:");Serial.println(lat);
Serial.print("Longitude:");Serial.println(lon);
myFile = SD.open(fileName, FILE_WRITE);
myFile.print(lat);
myFile.print(", ");
myFile.print(lon);
myFile.println("");
myFile.close();
}
}
}
If I use this code, it gives me the lat ad lon:
#include <SoftwareSerial.h>
#include <TinyGPS.h>
TinyGPS gps;
SoftwareSerial nss(10, 11);
void setup(){
nss.begin(9600);
Serial.begin(9600);
}
void loop()
{
while (nss.available())
{
int c = nss.read();
if (gps.encode(c))
{
long lat, lon;
unsigned long fix_age;
gps.get_position(&lat, &lon, &fix_age);
Serial.print("Szerokosc:");Serial.println(lat);
Serial.print("Dlugosc:");Serial.println(lon);
}
}
}
-
you need to describe the observed results .... you made no mention if anything actually prints in the serial monitorjsotola– jsotola2018年09月09日 23:36:11 +00:00Commented Sep 9, 2018 at 23:36
-
Yes sorry, the observed results: -code with GPS only is print location on serial monitor, so GPS works fine -code with GPS and SD card only show inforation about file create and stopMaciej– Maciej2018年09月10日 07:49:50 +00:00Commented Sep 10, 2018 at 7:49
1 Answer 1
SD uses SPI. On the Uno, SPI is on pins 10-13. You are also using pins 10 and 11 for software serial. That won't work. You need to use pins other than 10 and 11 for software serial.
-
Owww that was so simple, and stupid mistake for me. You saved my life! Thanks so much. First I changed the pins of serial to 8 and 9, it's not work, but when I change to 5 and 6 it works perfectly. Once again thanks! :)Maciej– Maciej2018年09月10日 07:51:47 +00:00Commented Sep 10, 2018 at 7:51