I have now twisted my brain over this code all day, and finally I understand that I'm just not skilled enough to figure this out. The code below is put together from various sources I found here on Arduino.cc. I know it's rather chaotic and it's probably quite obviously that I don't understand all that is going on. Anyway; The code read an NMEA string from a GPS receiver connected by using software UART on pin 2 and 4. The received data is then passed on to the serial port.
The problem is that not all the data in the string send to the serial port seems to be updated, with other words; Some of the GPS data keeps repeating itself (like latitude and longitude) while other data (like the date and time) updates aright. I have tried to connect the GPS receiver directly to my laptop and monitor the output through HyperTerminal, and there all data is updated for each sentence received from the GPS.
void loop()
{
if( state == DATA_PENDING )
{
state = IDLE;
// Serial.print(SwUartRXData);
if (SwUartRXData == -1) { // See if the port is empty yet
delay(100);
} else {
linea[conta]=SwUartRXData; // If there is serial port data, it is put in the buffer
conta++;
//printByte(byteGPS);
if (SwUartRXData==13){ // If the received byte is = to 13, end of transmission
cont=0;
bien=0;
for (int i=1;i<7;i++){ // Verifies if the received command starts with $GPR
if (linea[i]==comandoGPR[i-1]){
bien++;
}
}
if(bien==6){ // If yes, continue and process the data
for (int i=0;i<300;i++){
if (linea[i]==','){ // check for the position of the "," separator
indices[cont]=i;
cont++;
}
if (linea[i]=='*'){ // ... and the "*"
indices[12]=i;
cont++;
}
}
if (teller > trackint) {
//Serial.print("RFU");
//Serial.print(";");
Serial.print(unitID);
Serial.print(";");
for (int j=indices[3];j<(indices[3+1]-1);j++){ // N/S
Serial.print(linea[j+1]);
}
Serial.print(";");
for (int j=indices[2];j<(indices[2+1]-1);j++){ // Latitude
Serial.print(linea[j+1]);
}
Serial.print(";");
for (int j=indices[5];j<(indices[5+1]-1);j++){ // E/W
Serial.print(linea[j+1]);
}
Serial.print(";");
for (int j=indices[4];j<(indices[4+1]-1);j++){ // Longitude
Serial.print(linea[j+1]);
}
Serial.print(";"); // UTC Time
for (int j=indices[0];j<(indices[0+1]-1);j++){
Serial.print(linea[j+1]);
}
Serial.print(";"); // UTC date
for (int j=indices[8];j<(indices[8+1]-1);j++){
Serial.print(linea[j+1]);
}
Serial.print(";"); // Speed in knots
for (int j=indices[6];j<(indices[6+1]-1);j++){
Serial.print(linea[j+1]);
}
Serial.print(";"); // Heading in degrees
for (int j=indices[7];j<(indices[7+1]-1);j++){
Serial.print(linea[j+1]);
}
Serial.print(";"); // Valid GPS fix A=yes, V=no
for (int j=indices[1];j<(indices[1+1]-1);j++){
Serial.print(linea[j+1]);
}
Serial.println("");
teller = 0;
} else {
teller = teller + 1;
}
}
conta=0; // Reset the buffer
for (int i=0;i<300;i++){ //
linea[i]=' ';
}
}
}
// End
}
}
Some of the GPS data keeps repeating itself (like latitude and longitude) while other data (like the date and time) updates aright
Pardon me if this is a dumb question, but wouldn't you expect the latitude and longitude to repeat itself, more or less, unless you were moving? I haven't dissected the code, but it's surprising that date and time work, but not lat/long.
Good point, but believe me; I have tried to move around with the GPS receiver as well, but with no luck :).
When I connect the GPS directly to my computer, I see the position drift a little, even though I'm standing still, so no matter what I would expect the latitude and longitude to change a little.
My first instinct is that this is unnecessarily complicated. It looks like you have a complete implementation of SoftwareSerial in your sketch. Why not just #include SoftwareSerial.h, or better yet, AFSoftSerial.h (third party software serial library)?
Can you share some sample of the output serial stream your program generates and identify which parts seems suspicious to you?