Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Example code for connecting a Parallax GPS module to the Arduino
- Igor Gonzalez Martin. 05-04-2007
- English translation by djmatic 19-05-2007
- Listen for the $GPRMC string and extract the GPS location data from this.
- Display the result in the Arduino's serial monitor.
- */
- #include <string.h>
- #include <ctype.h>
- int ledPin1= 10; // LED test pin
- int ledPin2= 9; //LED warning limit
- int ledPin3= 8; //LED warning over limit
- int byteGPS=-1;
- char linea[300] = "";
- char comandoGPR[7] = "$GPRMC";
- int cont=0;
- int bien=0;
- int conta=0;
- int indices[13];
- int lat = 0;
- void setup()
- {
- pinMode(ledPin1,OUTPUT); // Initialize LED pin
- pinMode(ledPin2, OUTPUT);
- pinMode(ledPin3, OUTPUT);
- Serial.begin(4800);
- for (int i=0;i<300;i++){ // Initialize a buffer for received data
- linea[i]=' ';
- }
- }
- void loop()
- {
- digitalWrite(ledPin1, HIGH);
- byteGPS=Serial.read(); // Read a byte of the serial port
- if (byteGPS == -1) { // See if the port is empty yet
- delay(100);
- } else {
- // note: there is a potential buffer overflow here!
- linea[conta]=byteGPS; // If there is serial port data, it is put in the buffer
- conta++;
- Serial.write(byteGPS);
- if (byteGPS==13){ // If the received byte is = to 13, end of transmission
- // note: the actual end of transmission is <CR><LF> (i.e. 0x13 0x10)
- digitalWrite(ledPin1, LOW);
- cont=0;
- bien=0;
- // The following for loop starts at 1, because this code is clowny and the first byte is the <LF> (0x10) from the previous transmission.
- 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
- // note: again, there is a potential buffer overflow here!
- 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]);
- }
- Serial.println("");
- //********** Checking Latitude limit ******************
- if(i==2)//i=2 indicate latitude
- {
- //convert latitude data to int
- int k = indices[i];
- lat = (linea[k+1]-48)*1000 + (linea[k+2]-48)*100 + (linea[k+3]-48)*10 +(linea[k+4]-48);
- if(lat > 55)
- {
- //Led on when latitude value larger than 0055
- digitalWrite(ledPin2, HIGH);
- }
- else
- {
- digitalWrite(ledPin2, LOW);
- }
- }
- //***************************************************
- }
- Serial.print("Latitude: ");
- Serial.println(lat);
- Serial.println("---------------");
- }
- conta=0; // Reset the buffer
- for (int i=0;i<300;i++){ //
- linea[i]=' ';
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment