Sketch Unifying

Hi guys, i need a urgent help about my final project. Two sketches should unify and two seperated gps info should be seen(One from remote and other is own). Firts code shows gps location info received via xbee from another arduino.It works successfully .The second sketch (worked good also) shows own location info acquired via gps module and connected to arduino. I tried some methods like “.listen” for multidevice or multi serial connection but somehow it didnt work.If you could reply and help id be so pleased.From now thanks for your reply and help.Regards,Faruk

SKETCH FOR RECEIVING GPS INFOS FROM ANOTHER ARDUINO VIA XBEE

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
long t1;
String st, lon_val, lat_val;
int val;

//08.08.2014--17:48 
static const int RXPin = 9, TXPin = 8;
static const uint32_t GPSBaud = 4800;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(9, 8);

///////////////////////
void setup()
{
   pinMode(13, OUTPUT);
   Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
   
   //ss.begin(GPSBaud);
   t1 = millis();
}
 
void parseData()
{
   if(st.indexOf(" ")>=0)
   {
     lon_val = st.substring(0,(st.indexOf(" ")));
     lat_val = st.substring(st.indexOf(" ")+1);
   }
   else
   {
     lon_val = st;
     val = 0;
   }
}
 
void loop()
{
  static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  //GPS alam?yoruz
  //printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  //printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);

  if (Serial.available())
  {
     st = String("");
     while(Serial.available())
     {
        st = st + char(Serial.read());
        delay(1);
     }
 
     parseData();
     //Serial.print(st);
     //Serial.print("\t");
     Serial.print(lon_val);
     Serial.print("\t");
     Serial.println(lat_val);
     t1 = millis();
  }
  if(lon_val.indexOf("LED1ON")>=0)
  {
    if ((millis() - t1)<(val*1000))
       digitalWrite(13,HIGH);
    else if((millis() - t1) >(val*1000))
       digitalWrite(13,LOW);
  }
 else
    digitalWrite(13, LOW);
 }
 
 static void printFloat(float val, bool valid, int len, int prec)
{
  if (!valid)
  {
  while (len-- > 1)
  Serial.print('*');
  Serial.print(' ');
  }
  else
  {
  Serial.print(val, prec);
  int vi = abs((int)val);
  int flen = prec + (val < 0.0 ? 2 : 1); // . and -
  flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  for (int i=flen; i<len; ++i)
  Serial.print(' ');
  }
  smartDelay(0);
}
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
  while (ss.available())
  gps.encode(ss.read());
  } while (millis() - start < ms);
}

SKETCH FOR GETTING GPS INFOS.

#include <SoftwareSerial.h>

// Constants
#define txPin 8      //tx pin in GPS connection
#define rxPin 9      //rx pin in GPS connection

// Set up the GPS serial port
SoftwareSerial gps = SoftwareSerial(rxPin, txPin);


// Variables
byte byteGPS = 0;  
int i = 0;
int state = 0;
char dataGPG[100]="";
char *pch;
char *GGA[15];
int sat = 0;

void setup()
{
//setup for Serial Port
Serial.begin(9600);
Serial.flush(); 

//setup for GPS Serial Port  
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
gps.begin(4800);
gps.flush();

//setup satellites signal
pinMode(13, OUTPUT);
digitalWrite(13, LOW);     // Turn off the led until a satellite signal
}


void loop()
{
gps.flush();
Serial.flush();

Serial.println("Waiting GPS data...");

// Prepare all for reading GPS Serial Port
memset(dataGPG, 0, sizeof(dataGPG));    // Remove previous readings
byteGPS = 0;                            // Remove data
byteGPS = gps.read();                   // Read the byte that is in the GPS serial port
delay(1000);

// Find the desired string
while(byteGPS != '

)
{
byteGPS = gps.read();
}

// Save the string in an array
i=1;
dataGPG[0] = ’


;

while(byteGPS != '*' )
{
byteGPS = gps.read();
dataGPG[i]=byteGPS; 
i++; 
}

dataGPG[i]= '\0';
string();                                 // Call to the function that manipulates our string

}

/*
This function will allow us to identify the data we need to get the longitude, latitude ...
*/

void string()
{
i=0;
memset(GGA, 0, sizeof(GGA));          // Remove previous readings

pch = strtok (dataGPG,",");

// Analyze the saved interval in pch to see if it the needed string
if (strcmp(pch,"$GPGGA")==0)
{
while (pch != NULL)
{
pch = strtok (NULL, ",");
GGA[i]=pch;    
i++;
} 

plot(GGA,"$GPGGA");         // Call to the function that is going to display the data
}
}

/*
This function organize the gps data received for printing in the serial monitor.
*/

void plot(char **GGAPrint, char *trama)
{
state = atoi(GGAPrint[5]);
sat = atoi(GGAPrint[6]);

if(trama=="$GPGGA" && state==1)
{
digitalWrite(13, HIGH);            // Then there are satellites, the LED switch ON

Serial.println("");
Serial.println("----------------------------------------------");
Serial.print("UTC Hour -> ");
Serial.println(GGAPrint[0]);
Serial.print("Latitude -> ");
Serial.print(GGAPrint[1]);
Serial.println(GGAPrint[2]);
Serial.print("Longitude -> ");
Serial.print(GGAPrint[3]);
Serial.println(GGAPrint[4]);
Serial.print("GPS quality: 0=null; 1=GPS fixed -> ");
Serial.println(GGAPrint[5]);
Serial.print("Number of satellites -> ");
Serial.println(sat);
Serial.print("Horizontal Dilution of Precision -> ");
Serial.println(GGAPrint[7]);
Serial.print("Antenna altitude -> ");
Serial.print(GGAPrint[8]);
Serial.println(GGAPrint[9]);
Serial.print("Geoid Separation -> ");
Serial.print(GGAPrint[10]);
Serial.println(GGAPrint[11]);
Serial.println("----------------------------------------------");
Serial.println("");

}

else                                // If no satellite connection
{
digitalWrite(13, LOW);                                              // Turn off the LED
Serial.println("");
Serial.println("-----------------------------");
Serial.print("|--- Satellites Used -->");
Serial.print(sat);
Serial.println(" |");
Serial.println("|----Waiting location----|");
Serial.println("-----------------------------");
Serial.println("");
}
}

Two sketches should unify

Why?

I tried some methods like “.listen” for multidevice or multi serial connection but somehow it didnt work.

Then, somehow, you did it wrong.

This demo about merging codes may help.

...R

PaulS:

Two sketches should unify

Why?

I tried some methods like ".listen" for multidevice or multi serial connection but somehow it didnt work.

Then, somehow, you did it

PaulS:

Two sketches should unify

Why?

Because i'll use two rc car ,first is leader and second is follower. Locations of two rc cars should be processed for following.

I tried some methods like ".listen" for multidevice or multi serial connection but somehow it didnt work.

Then, somehow, you did it wrong.

Yes i did it wrong ,sorry for my sentence

If you could help,id be so pleased.If you wish other details i can explain.I'm using Arduino Uno, Cooking hacks gps module.I look forward to your reply.

GPS Module for Arduino, Raspberry Pi and Intel Galileo

Regards

If you wish other details i can explain.

Good. You tried something. You didn't show us what you tried.

Whatever you tried did not work, for some definition of work. You did not explain what the code actually did, or did not do. You did not explain what you expected it to do, or not do.

You now have the opportunity to correct those oversights.

PaulS:

If you wish other details i can explain.

Good. You tried something. You didn’t show us what you tried.

Whatever you tried did not work, for some definition of work. You did not explain what the code actually did, or did not do. You did not explain what you expected it to do, or not do.

You now have the opportunity to correct those oversights.

I’m sending the code unified,but i was unsuccessfull.Unfortunately serial monitor was empty. First code receives only longtitude, and latitude values from another arduino via xbee.It parses and shows seperately long. and latitud eof other arduino. Second code shows gps parameters (long,lat,number of satellites,horizontal dilution of precision, hour, etc.) of own arduino and receives this parameters from connected gps module(Tx=8,Rx=9).I look forward to your reply.

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
long t1;
String st, lon_val, lat_val;
int val;
void string();
//08.08.2014--17:48
static const int RXPin = 9, TXPin = 8;
static const uint32_t GPSBaud = 4800;
//gps de?i?kenleri
byte byteGPS = 0;
int i = 0;
int state = 0;
char dataGPG[100]="";
char *pch;
char *GGA[15];
int sat = 0;
//
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(9, 8);
SoftwareSerial xbee(0, 1);
///////////////////////
void setup()
{
  pinMode(13, OUTPUT);
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  while(!Serial)
  {
    ;
  }

  ss.begin(9600);
  xbee.begin(9600);
  ss.flush();
  t1 = millis();

  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}
void parseData()
{
   if(st.indexOf(" ")>=0)
   {
     lon_val = st.substring(0,(st.indexOf(" ")));
     lat_val = st.substring(st.indexOf(" ")+1);
   }
   else
   {
     lon_val = st;
     val = 0;
   }
}
void loop()
{
  xbee.listen();
  static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  //GPS alam?yoruz
  //printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  //printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  if (Serial.available())
  {
     st = String("");
     while(Serial.available())
     {
        st = st + char(Serial.read());
        delay(1);
     }
     parseData();
     //Serial.print(st);
     //Serial.print("\t");
     Serial.print(lon_val);
     Serial.print("\t");
     Serial.println(lat_val);
     t1 = millis();
  }

  if(lon_val.indexOf("LED1ON")>=0)
  {
    if ((millis() - t1)<(val*1000))
       digitalWrite(13,HIGH);
    else if((millis() - t1) >(val*1000))
       digitalWrite(13,LOW);
  }
  else
    digitalWrite(13, LOW);
  ss.listen();
  ss.flush();
  Serial.flush();
  Serial.println("Waiting GPS data...");
  // Prepare all for reading GPS Serial Port
  memset(dataGPG, 0, sizeof(dataGPG));    // Remove previous readings
  byteGPS = 0;                            // Remove data
  byteGPS = ss.read();                   // Read the byte that is in the GPS serial port
  delay(1000);
  // Find the desired string
  while(byteGPS != '

)
  {
    byteGPS = ss.read();
  }
  // Save the string in an array
  i=1;
  dataGPG[0] = ’


;
  while(byteGPS != '*' )
  {
    byteGPS = ss.read();
    dataGPG[i]=byteGPS;
    i++;
  }
  dataGPG[i]= '\0';
  string();                                 // Call to the function that manipulates our string
}
static void printFloat(float val, bool valid, int len, int prec)
{
  if (!valid)
  {
    while (len-- > 1)
    Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(' ');
  }
  smartDelay(0);
}
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

void string()
{
  i=0;
  memset(GGA, 0, sizeof(GGA));          // Remove previous readings
  pch = strtok (dataGPG,",");
  // Analyze the saved interval in pch to see if it the needed string
  if (strcmp(pch,"$GPGGA")==0)
  {
    while (pch != NULL)
    {
      pch = strtok (NULL, ",");
      GGA[i]=pch;
      i++;
    }
    plot(GGA,"$GPGGA");         // Call to the function that is going to display the data
  }
}
void plot(char **GGAPrint, char *trama)
{
  state = atoi(GGAPrint[5]);
  sat = atoi(GGAPrint[6]);
  if(trama=="$GPGGA" && state==1)
  {
    digitalWrite(13, HIGH);            // Then there are satellites, the LED switch ON
    Serial.println("");
    Serial.println("----------------------------------------------");
    Serial.print("UTC Hour -> ");
    Serial.println(GGAPrint[0]);
    Serial.print("Latitude -> ");
    Serial.print(GGAPrint[1]);
    Serial.println(GGAPrint[2]);
    Serial.print("Longitude -> ");
    Serial.print(GGAPrint[3]);
    Serial.println(GGAPrint[4]);
    Serial.print("GPS quality: 0=null; 1=GPS fixed -> ");
    Serial.println(GGAPrint[5]);
    Serial.print("Number of satellites -> ");
    Serial.println(sat);
    Serial.print("Horizontal Dilution of Precision -> ");
    Serial.println(GGAPrint[7]);
    Serial.print("Antenna altitude -> ");
    Serial.print(GGAPrint[8]);
    Serial.println(GGAPrint[9]);
    Serial.print("Geoid Separation -> ");
    Serial.print(GGAPrint[10]);
    Serial.println(GGAPrint[11]);
    Serial.println("----------------------------------------------");
    Serial.println("");
  }
  else                                // If no satellite connection
  {
    digitalWrite(13, LOW);                                              // Turn off the LED
    Serial.println("");
    Serial.println("-----------------------------");
    Serial.print("|--- Satellites Used -->");
    Serial.print(sat);
    Serial.println(" |");
    Serial.println("|----Waiting location----|");
    Serial.println("-----------------------------");
    Serial.println("");
  }
}

Looks like you didn't bother with the simple system in Reply #2

...R

SoftwareSerial xbee(0, 1);

This instance needs to be renamed:

SoftwareSerial I_Am_An_Idiot(0, 1);

Make sure you rename all references, too.

You can NOT use the hardware serial pins for software serial.

  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

Not possible. You've already, foolishly, tried to use the hardware serial pins, so they are NOT available for Serial.

Make up your mind how you intend to use that pair of pins, and delete the other use.