After amonth of intensive working with arduino i manged to get several devices working togther ... my project is mesuring distance between 2 arduinos using gps and xbee in both sides ... but sence im new arduino user it wil be great if u can help me to reduce and enhance my code...
here is my receiver code /// the transmitter is sending float variables with packet markers {} ()
#include <NewSoftSerial.h>
#include <TinyGPS.h>
#include<math.h>
#define RXPIN 2
#define TXPIN 3
#define GPSBAUD 4800
TinyGPS gps;
boolean started = false;
boolean ended = false;
char inData[24]; // Size as appropriate
byte index = 0;
float calc_dist(float flat1, float flon1, float flat2, float flon2);// function for calc. distance given lat&lon for 2 points on earth
float getlat2();// getting lat from another arduino
float getlon2();//getting lon from another arduino
#define SOP '{'// start of packet
#define EOP '}'//end of packet
NewSoftSerial uart_gps(RXPIN, TXPIN);
float getgpslat(TinyGPS &gps);
float getgpslon(TinyGPS &gps);
void setup()
{
Serial.begin(9600);// for xbee
uart_gps.begin(GPSBAUD); // setup sketch for data output speed of GPS module im using em406a
}
void loop()
{
float lat1,lat2,lon1,lon2,d;
while(uart_gps.available()>0) // While there is data on the RX pin...
{
int c = uart_gps.read(); // load the data into a variable...
if(gps.encode(c)) // if there is a new valid sentence...
{
lat1=getgpslat(gps);//getting my lat
lat2=getlat2();// getting another arduino lat
lon1=getgpslon(gps);
lon2=getlon2();
delay(5000);
d= calc_dist(lat1,lon1,lat2,lon2);
Serial.print(d,5);
}
}
}
float getgpslat(TinyGPS &gps)
{
float latitude, longitude;
gps.f_get_position(&latitude, &longitude);
return latitude;
}
float getgpslon(TinyGPS &gps)
{
float latitude, longitude;
gps.f_get_position(&latitude, &longitude);
return longitude;
}
float getlat2()
{
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
started = true;
index = 0;
inData[index] = '\0';
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 24-1) // Array size
{
inData[index++] = inChar;
inData[index] = '\0';
}
}
}
if(started && ended)
{
float lat1= atof(inData);
return lat1;
}
}
float getlon2()
{
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == '(')
{
started = true;
index = 0;
inData[index] = '\0';
}
else if(inChar == ')')
{
ended = true;
break;
}
else
{
if(index < 24-1) // Array size
{
inData[index++] = inChar;
inData[index] = '\0';
}
}
}
if(started && ended)
{
float lat2= atof(inData);
return lat2;
}
}
float calc_dist(float flat1, float flon1, float flat2, float flon2)
{
float dist_calc=0;
float dist_calc2=0;
float diflat=0;
float diflon=0;
diflat=radians(flat2-flat1);
flat1=radians(flat1);
flat2=radians(flat2);
diflon=radians((flon2)-(flon1));
dist_calc = (sin(diflat/2.0)*sin(diflat/2.0));
dist_calc2= cos(flat1);
dist_calc2*=cos(flat2);
dist_calc2*=sin(diflon/2.0);
dist_calc2*=sin(diflon/2.0);
dist_calc +=dist_calc2;
dist_calc=(2*atan2(sqrt(dist_calc),sqrt(1.0-dist_calc)));
dist_calc*=6371000.0; //Converting to meters
// Serial.println(dist_calc);
return dist_calc;
}