GPS Assigning string data to variables

Hello Everybody,

The following code displays the data absolutely perfect.

 #include <string.h>
 #include <ctype.h>
 int ledPin = 13;                  // LED test pin
 int rxPin = 0;                    // RX PIN 
 int txPin = 1;                    // TX TX
 int byteGPS=-1;
 char linea[300] = "";
 char comandoGPR[7] = "$GPRMC";
 int cont=0;
 int bien=0;
 int conta=0;
 int indices[13];
 
 
 void setup() {
   pinMode(ledPin, OUTPUT);       // Initialize LED pin
   pinMode(rxPin, INPUT);
   pinMode(txPin, OUTPUT);
   Serial.begin(4800);
   for (int i=0;i<300;i++){       // Initialize a buffer for received data
     linea[i]=' ';
   }   
 }


 void loop() {
   digitalWrite(ledPin, HIGH);
   byteGPS=Serial.read();         // 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 (DdMmYy): ");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]); 
           }
           Serial.println("");
         }
         Serial.println("---------------");
       }
       conta=0;                    // Reset the buffer
       for (int i=0;i<300;i++){    //  
         linea[i]=' ';             
       }                 
     }
   }
 }

However, I want everything to be assigned to a variable. For example, the numerical value in "Time in UTC(HhMmSs): 154655". I need that to be assigned to a variable and "Latitude: 4428.1874" numerical value to be assigned to another variable. Pleaaassseee try to help me out.

 Time in UTC (HhMmSs): 154655 
 Status (A=OK,V=KO): A 
 Latitude: 4428.1874 
 Direction (N/S): N 
 Longitude: 00440.5185 
 Direction (E/W): W 
 Speed in Knots: 000.7 
 Heading in degrees: 000.0 
 Date in UTC (DdMmYy): 050407 
 Magnetic variation:  
 Variation (E/W):  
 Mode: A

So, what's the problem? You've got the character data that you are printing out. Do you need help copying that character data to another array? Do you need help converting the character data to numbers? The atoi and atof functions were written for just this purpose.

Well, what I would like to do is take that numeric 154655 value that is being displayed as the UTC time in HhMsSs and then declare a variable, therefore:

double UTC;

and then assign the numeric value to that variable, thus:

UTC = 154655;

I need to do the same for the Latitude, Longitude, and UTC date.

Thanks

You don't have a numeric value to assign to UTC. You have an array of characters that contains the string that represents the UTC value.

You need to copy those characters into another character array. Then, NULL terminate that array. Then, call atoi or atof on the NULL-terminated array, to get a numeric value. Then, assign that value to UTC.

Yes, I kinda understand that's what i have to do. but i'm not a pro at programming so could you help me code that or at least get me started. thanks.