FahmiG

ARDUINO + GPS = INDICATOR LIMIT AREA

Oct 10th, 2014
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  /*
  2.  Example code for connecting a Parallax GPS module to the Arduino
  3.  Igor Gonzalez Martin. 05-04-2007
  4.  English translation by djmatic 19-05-2007
  5.  Listen for the $GPRMC string and extract the GPS location data from this.
  6.  Display the result in the Arduino's serial monitor.
  7.  */
  8.  #include <string.h>
  9.  #include <ctype.h>
  10.  int ledPin1= 10; // LED test pin
  11.  int ledPin2= 9; //LED warning limit
  12.  int ledPin3= 8; //LED warning over limit
  13.  int byteGPS=-1;
  14.  char linea[300] = "";
  15.  char comandoGPR[7] = "$GPRMC";
  16.  int cont=0;
  17.  int bien=0;
  18.  int conta=0;
  19.  int indices[13];
  20.  
  21.  int lat = 0;
  22.  void setup()
  23.  {
  24.    pinMode(ledPin1,OUTPUT);       // Initialize LED pin
  25.    pinMode(ledPin2, OUTPUT);
  26.    pinMode(ledPin3, OUTPUT);
  27.    Serial.begin(4800);
  28.    for (int i=0;i<300;i++){       // Initialize a buffer for received data
  29.      linea[i]=' ';
  30.    }  
  31.  
  32.    
  33.  }
  34.  void loop()
  35.  {
  36.    digitalWrite(ledPin1, HIGH);
  37.    byteGPS=Serial.read();         // Read a byte of the serial port
  38.    if (byteGPS == -1) {           // See if the port is empty yet
  39.      delay(100);
  40.    } else {
  41.      // note: there is a potential buffer overflow here!
  42.      linea[conta]=byteGPS;        // If there is serial port data, it is put in the buffer
  43.      conta++;                      
  44.      Serial.write(byteGPS);
  45.      if (byteGPS==13){            // If the received byte is = to 13, end of transmission
  46.        // note: the actual end of transmission is <CR><LF> (i.e. 0x13 0x10)
  47.        digitalWrite(ledPin1, LOW);
  48.        cont=0;
  49.        bien=0;
  50.        // The following for loop starts at 1, because this code is clowny and the first byte is the <LF> (0x10) from the previous transmission.
  51.        for (int i=1;i<7;i++){     // Verifies if the received command starts with $GPR
  52.          if (linea[i]==comandoGPR[i-1]){
  53.            bien++;
  54.          }
  55.        }
  56.        if(bien==6){               // If yes, continue and process the data
  57.          for (int i=0;i<300;i++){
  58.            if (linea[i]==','){    // check for the position of the  "," separator
  59.              // note: again, there is a potential buffer overflow here!
  60.              indices[cont]=i;
  61.              cont++;
  62.            }
  63.            if (linea[i]=='*'){    // ... and the "*"
  64.              indices[12]=i;
  65.              cont++;
  66.            }
  67.          }
  68.          Serial.println("");      // ... and write to the serial port
  69.          Serial.println("");
  70.          Serial.println("---------------");
  71.          for (int i=0;i<12;i++)
  72.          {
  73.                switch(i)
  74.                {
  75.                    case 0 :Serial.print("Time in UTC (HhMmSs): ");break;
  76.                    case 1 :Serial.print("Status (A=OK,V=KO): ");break;
  77.                    case 2 :Serial.print("Latitude: ");break;
  78.                    case 3 :Serial.print("Direction (N/S): ");break;
  79.                    case 4 :Serial.print("Longitude: ");break;
  80.                    case 5 :Serial.print("Direction (E/W): ");break;
  81.                    case 6 :Serial.print("Velocity in knots: ");break;
  82.                    case 7 :Serial.print("Heading in degrees: ");break;
  83.                    case 8 :Serial.print("Date UTC (DdMmAa): ");break;
  84.                    case 9 :Serial.print("Magnetic degrees: ");break;
  85.                    case 10 :Serial.print("(E/W): ");break;
  86.                    case 11 :Serial.print("Mode: ");break;
  87.                    case 12 :Serial.print("Checksum: ");break;
  88.                }
  89.                for (int j=indices[i];j<(indices[i+1]-1);j++)
  90.                {
  91.                    Serial.print(linea[j+1]);
  92.                }
  93.            
  94.                Serial.println("");
  95.                
  96.                //********** Checking Latitude limit ******************  
  97.                if(i==2)//i=2 indicate latitude
  98.                {    
  99.                       //convert latitude data to int
  100.                       int k = indices[i];
  101.                       lat = (linea[k+1]-48)*1000 + (linea[k+2]-48)*100 + (linea[k+3]-48)*10 +(linea[k+4]-48);
  102.                       if(lat > 55)
  103.                       {
  104.                               //Led on when latitude value larger than 0055
  105.                               digitalWrite(ledPin2, HIGH);
  106.                       }
  107.                       else
  108.                       {
  109.                               digitalWrite(ledPin2, LOW);    
  110.                       }              
  111.                  }
  112.                  //***************************************************
  113.          }
  114.  
  115.          Serial.print("Latitude: ");
  116.          Serial.println(lat);
  117.          Serial.println("---------------");
  118.        }
  119.        conta=0;                    // Reset the buffer
  120.        for (int i=0;i<300;i++){    //  
  121.          linea[i]=' ';            
  122.        }                
  123.      }
  124.    }
  125.  }
Advertisement
Add Comment
Please, Sign In to add comment