Isolating latitude and logitude values from GPS data

Hello,

I am using the following code to read the NMEA data from an EM-406 GPS unit. I would like to isolate the latitude and longitude values and place them in the appropriate variables. I thought I could create a variable called lat and that would be the 2 value of the switch, case part of the code but I couldn't seem to get it to work.

Many thanks if anyone has any clues.

#include <SoftwareSerial.h>

#include <ctype.h>
#include <string.h>

#define bit9600Delay 84
#define halfBit9600Delay 42
#define bit4800Delay 188
#define halfBit4800Delay 94
softSerial(6,7);

//byte rx = 6;  //pin 6 to recieve data purple wire from EM-406
//byte tx = 7;  //pin 7 to transmit data orange wire from EM-406
int ledPin = 13;
int indices[13];
int cont=0;
 int bien=0;
 int conta=0;
 int byteGPS=-1;
char linea[300] = "";
 char comandoGPR[7] = "$GPRMC";
byte SWval;  //variable allocated to store recived data in
int i=0;
char messageline[80] =" ";
int lat;
int time;

void setup(){
  
  pinMode(rx, INPUT);
  pinMode(tx, OUTPUT);
  digitalWrite(tx, HIGH);
  digitalWrite(13, HIGH);  //turn on debugging LED
  
 
  Serial.begin(9600);
}

char SWread()
{
  byte val = 0;
  while (digitalRead(rx)); //wait for startbit
  if (digitalRead(rx) == LOW) {
    delayMicroseconds (halfBit4800Delay);
    for (int offset = 0; offset < 8; offset++)
    {
      delayMicroseconds(bit4800Delay);
      val |= digitalRead(rx) << offset; //wait for stop bit + extra
     
    }
  delayMicroseconds(bit4800Delay);
  delayMicroseconds(bit4800Delay);
  return val;
  
  //Serial.print(SWread(),BYTE);
  }
}

  void loop()
{
 digitalWrite(ledPin, HIGH);
   byteGPS=SWread();     // Read a byte of the serial port
   if (byteGPS == -1) {           // See if the port is empty yet
     delay(100); 
   } else {
     linea[conta]=byteGPS;        // If there is serial port data, it is put in the buffer
     conta++;                      
     Serial.print(byteGPS, BYTE); 
     if (byteGPS==13){            // If the received byte is = to 13, end of transmission
       digitalWrite(ledPin, LOW); 
       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++;
           }
           }
           
            
           
       Serial.println("");      // ... and write to the serial port
       Serial.println("");
       Serial.println("---------------");
         for (int i=0;i<12;i++){
           switch(i){
             case 0 :Serial.print("Time in UTC (HhMmSs): ");break;
             case 1 :Serial.print("Status (A=OK,V=KO): ");break;
             case 2 :Serial.print("Latitude: ");break;
             case 3 :Serial.print("Direction (N/S): ");break;
             case 4 :Serial.print("Longitude: ");break;
             case 5 :Serial.print("Direction (E/W): ");break;
             case 6 :Serial.print("Velocity in knots: ");break;
             case 7 :Serial.print("Heading in degrees: ");break;
             case 8 :Serial.print("Date UTC (DdMmAa): ");break;
             case 9 :Serial.print("Magnetic degrees: ");break;
             case 10 :Serial.print("(E/W): ");break;
             case 11 :Serial.print("Mode: ");break;
             case 12 :Serial.print("Checksum: ");break;
           }
           for (int j=indices[i];j<(indices[i+1]-1);j++){
             Serial.print(linea[j+1]); 
             //lat=indices[2];
           }
          // for (int j=indices[i];j<(indices[i+1]-1);j++){
          //   Serial.print(linea[4]+1); 
          //  lat = (linea[4]+1);
          //   Serial.print(lat, BYTE);
             //lat=indices[2];
          // }
           Serial.println("");
         }
         //Serial.println(indices[3]);
         Serial.println("---------------");
         delay(2000);
       }
       conta=0;                    // Reset the buffer
       for (int i=0;i<300;i++){    //  
         linea[i]=' ';  
//delay(2000);         
       }                 
     }
   }
 }

Replace:

        for (int i=0;i<12;i++){
           switch(i){
             case 0 :Serial.print("Time in UTC (HhMmSs): ");break;
             case 1 :Serial.print("Status (A=OK,V=KO): ");break;
             case 2 :Serial.print("Latitude: ");break;
             case 3 :Serial.print("Direction (N/S): ");break;
             case 4 :Serial.print("Longitude: ");break;
             case 5 :Serial.print("Direction (E/W): ");break;
             case 6 :Serial.print("Velocity in knots: ");break;
             case 7 :Serial.print("Heading in degrees: ");break;
             case 8 :Serial.print("Date UTC (DdMmAa): ");break;
             case 9 :Serial.print("Magnetic degrees: ");break;
             case 10 :Serial.print("(E/W): ");break;
             case 11 :Serial.print("Mode: ");break;
             case 12 :Serial.print("Checksum: ");break;
           }
           for (int j=indices[i];j<(indices[i+1]-1);j++){
             Serial.print(linea[j+1]); 
             //lat=indices[2];
           }

With something like:

    String lat = "", long = "";

    for (int j=indices[2];j<(indices[3]-1);j++){
         lat += linea[j+1]); 

     for (int j=indices[4];j<(indices[5]-1);j++){
         long += linea[j+1]);

Thankyou for the suggestion John.

I tried it but it didn't seem to work. However at this stage I have a very limited amount of knowledge and experience so I will keep working on it. But your efforts have no doubt put me on the right track and I will persist...

Many thanks again.

I tried it but it didn't seem to work.

Of course it worked. It just didn't do what you expected. So, you need to tell us what you expected, and what it actually did.

There is a TinyGPS library that will do all the parsing for you, and a SoftwareSerial library that will do a better job of reading the serial data.