Turning Sketch into a Function. Please Help.

Hey guys,
I’m still a little new to programming the arduino, and I need just a little bit of help. I have this sketch that takes serial gps data and parses out the longitude, latitude, and the direction of each.
What I want to do is take this whole sketch and turn it into a single function that outputs just outputs those 4 things, so that i can then just serial.print those afterwards.

Here’s the code:

int ledPin = 13;                  // LED test pin
int rxPin = 0;                    // RX PIN 
int txPin = 1;                    // TX TX
int byteGPS=-1;
int count=0;
int good=0;
int counta=0;
int index[13];
int x, y;                         //  For Longitude and Latitude arrays

char line[300] = "";
char commandGPR[7] = "$GPRMC";
char longitude[9];        //  Longitude added by me
char latitude[9];         //  Latitude added by me
char lon;                 //  Longitude Direction (N/S) added by me
char lat;                 //  Latitude Direction (E/W) added by me

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
    line[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 {
    line[counta]=byteGPS;        // If there is serial port data, it is put in the buffer
    counta++;                      
    Serial.print(byteGPS, BYTE); // Prints out raw GPS data
    if (byteGPS==13){            // If the received byte is = to 13, end of transmission
      digitalWrite(ledPin, LOW); 
      count=0;
      good=0;
      for (int i=1;i<7;i++){     // Verifies if the received command starts with $GPR
        if (line[i]==commandGPR[i-1]){
          good++;
        }
      }
      if(good==6){               // If yes, continue and process the data
        for (int i=0;i<300;i++){
          if (line[i]==','){     // check for the position of the  "," separator
            index[count]=i;
            count++;
          }
          if (line[i]=='*'){     // ... and the "*"
            index[12]=i;
            count++;
          }
        }
        Serial.println("");      // ... and write to the serial port
        Serial.println("");
        Serial.println("---------------");
        for (int i=0;i<12;i++){
          for (int j=index[i];j<(index[i+1]-1);j++){
           if (i == 2){                       //  Latitude
              Serial.print("Latitude = ");    
              for (int j=index[i];j<(index[i+1]-1);j++){ 
                latitude[y] = line[j+1];      
                Serial.print(latitude[y]); }  //  Output to PC
                break;                        
            }
            if (i == 3){                      //  Latitude Direction
              lat = line[j+1];                
              Serial.print("Direction = ");   
              Serial.print(lat);              //  Output to PC
              break;                          
            }
            if (i == 4){                      //  Longitude
              Serial.print("Longitude = ");   
              for (int j=index[i];j<(index[i+1]-1);j++){
                longitude[x] = line[j+1];
                Serial.print(longitude[x]); } //  Output to PC
                break;  //
            }
            if (i == 5){                      //  Longitude Direction
              lon = line[j+1];                
              Serial.print("Direction = ");   
              Serial.print(lon);              //  Output to PC
              break;                          
            }
          }
          Serial.println("");
        }
        Serial.println("---------------");
       }
      counta=0;                    // Reset the buffer
      for (int i=0;i<300;i++){      
        line[i]=' ';             
      }                 
    }
  }
}

If you can just show me how to turn this into a single function that i can call on demand i would be very grateful.

Thanks,
Cory

It already is in a single function - the function is called "loop".
Just rename "loop", strip out the serial prints, and your results are in the global variables "latitude", "longitude" etc.

Oh!! Duh! Thanks :slight_smile:

~Cory