Polling gps and sending sentences through bluetooth

I am working on a project that involves parsing a gps and sending the nmea sentences to a smart phone or laptop via bluetooth. I have the gps parsing and writing to a sd card in a separate program. Also in another program the gps data is being read from the sd card and transmitted via bluetooth. When I try to integrate the bluetooth and the gps code into one file it doesn’t function correctly. For instance sometimes the gps will poll when the code is executed and the bluetooth will not or vice versa. But as of now I haven’t been able to get them to both work concurrently. Here is some of the code below any suggestions or help will be appreciated.

#include<SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

SoftwareSerial mySerial(2,3);
SoftwareSerial BlueTooth(8,9); // RX, TX
int ledpin = 13; // led on D13 will blink on/off
int BT; // the data given from Computer
File GPSlog;
String sentence; //
String in;
#define LED 7 // status LED for SD operations

const int chipSelect = 10;

void setup() {

pinMode(10,OUTPUT); // Per SD library notes, pin 10 must be set to output
pinMode(LED,OUTPUT);
mySerial.begin(4800);
Serial.begin(9600);
BlueTooth.begin(9600);
BlueTooth.println(“Bluetooth On Please Press 0 or 1 to Blink LED…”);
pinMode(ledpin,OUTPUT);

Serial.print(“Initializing SD card…”);

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
// don’t do anything more:
return;
}
Serial.println(“card initialized.”);
GPSlog = SD.open(“GPS.log”,O_CREAT|O_WRITE);

}

void loop() {

//GPSlog = SD.open(“GPS.log”,O_CREAT|O_WRITE);
//
// if(!GPSlog) { // test if file can be opened
// digitalWrite(LED,LOW); // Turn off status LED if file open fails
// return;
// }
//else digitalWrite(LED,HIGH); // Turn on status LED if file open is
//GPSlog.close();

if (mySerial.available())
{

char c = mySerial.read();

if(c == ‘\n’)
{
parseCommand(sentence);
sentence = “”;
}
else
{
sentence += c;
}

}

if (BlueTooth.available()){
BT=BlueTooth.read();

if(BT==‘1’) {
GPSlog.close();
delay(200);
BlueTooth.println(“im here”);
//delay(200);
BlueTooth.println(“next step”);
GPSlog = SD.open(“GPS.log”);
delay(200);
if(GPSlog){
while (GPSlog.available()){
BlueTooth.write(GPSlog.read());
}
GPSlog.close();
delay(200);
}
else{
BlueTooth.println(“Error opening and reading file”);
}

}

}
}

/////////////// Just part of the function************************

void parseCommand(String in){

//DateTime now = rtc.now();

String Header; // set header to “”
String Latitude;
String Latth; // Lattitude Hemisphere
String Longitude;
String LongH; //Longtitude Hemisphere
String Ts; // Time stamp
String Status; // validity A or V invalid
String Cs; // Checksum
String Sk; // Speed in knots
String Tc; // True course
String Ds; // Date Stamp
String Vari; // Variation
String MagVari; // Magnetic Variation
//char endMarker = ‘\n’;

int head = in.indexOf(’$’); // set starting index for header
int endHeader = in.indexOf(","); // end of header
Header = in.substring(head+1, endHeader); // get the string that is between $ and comma
if(Header == “GPRMC”){ // output header of packet
Serial.print("Packet Header = ");
//Serial.println(Header);

int timestamp = in.indexOf(",", endHeader+1);
Ts = in.substring(endHeader+1, timestamp);
int st = in.indexOf(",", timestamp+1);
Status = in.substring(timestamp+1, st);
int Lat = in.indexOf(",", st+1);
Latitude= in.substring(st+1, Lat);
int lh = in.indexOf(",", Lat+1);
Latth = in.substring(Lat+1, lh);
int Lon = in.indexOf(",", lh+1);
Longitude = in.substring(lh+1, Lon);
int loh = in.indexOf(",", Lon+1);
LongH = in.substring(Lon+1, loh);

SoftwareSerial mySerial(2,3);
SoftwareSerial BlueTooth(8,9); // RX, TX

Whenever you find yourself needing two software serial instances, that is a sure sign that you need a Mega or a Due.

String Header; // set header to ""
String Latitude;
String Latth; // Lattitude Hemisphere
String Longitude;
String LongH;  //Longtitude Hemisphere
String Ts; // Time stamp
String Status; // validity A or V invalid
String Cs; // Checksum
String Sk; // Speed in knots
String Tc; // True course
String Ds; // Date Stamp
String Vari; // Variation
String MagVari; // Magnetic Variation

You got the special Uno with 2 terabytes of memory? If not, do NOT use the String class.

Here is some of the code below any suggestions or help will be appreciated.

Post ALL of your code, properly, after reading the stickies at the top of the forum - the ones you were supposed to read BEFORE you blundered in here.

Lol no I only have the 2048 bytes one. But even when I use the tiny gps library I get the same results of the Bluetooth and gps not working simultaneously. I used two software serials for testing so that I could see what is happening in the monitor. Also sorry about the code I will post the rest in a few.

HERE IS ALL THE CODE BELOW

#include<SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

SoftwareSerial mySerial(2,3);
SoftwareSerial BlueTooth(8,9); // RX, TX
int ledpin = 13; // led on D13 will blink on/off
int BT; // the data given from Computer
File GPSlog;
String sentence; // Stores all values
String in;
#define LED 7 // status LED for SD operations

const int chipSelect = 10;

void setup() {

pinMode(10,OUTPUT); // Per SD library notes, pin 10 must be set to output
pinMode(LED,OUTPUT);
mySerial.begin(4800);
Serial.begin(9600);
BlueTooth.begin(9600);
BlueTooth.println(“Bluetooth On Please Press 0 or 1 to Blink LED…”);
pinMode(ledpin,OUTPUT);

Serial.print(“Initializing SD card…”);

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
// don’t do anything more:
return;
}
Serial.println(“card initialized.”);
GPSlog = SD.open(“GPS.log”,O_CREAT|O_WRITE);

}

void loop() {

if (mySerial.available())
{

char c = mySerial.read();

if(c == ‘\n’)
{
parseCommand(sentence);
sentence = “”;
}
else
{
sentence += c;
}

}

if (BlueTooth.available()){
BT=BlueTooth.read();

if(BT==‘1’) {
GPSlog.close();
delay(200);
BlueTooth.println(“im here”);
//delay(200);
BlueTooth.println(“next step”);
GPSlog = SD.open(“GPS.log”);
delay(200);
if(GPSlog){
while (GPSlog.available()){
BlueTooth.write(GPSlog.read());
}
GPSlog.close();
delay(200);
}
else{
BlueTooth.println(“Error opening and reading file”);
}

}

}

}

void parseCommand(String in){

//DateTime now = rtc.now();

String Header; // set header to “”
String Latitude;
String Latth; // Lattitude Hemisphere
String Longitude;
String LongH; //Longtitude Hemisphere
String Ts; // Time stamp
String Status; // validity A or V invalid
String Cs; // Checksum
String Sk; // Speed in knots
String Tc; // True course
String Ds; // Date Stamp
String Vari; // Variation
String MagVari; // Magnetic Variation
//char endMarker = ‘\n’;

int head = in.indexOf(’$’); // set starting index for header
int endHeader = in.indexOf(","); // end of header
Header = in.substring(head+1, endHeader); // get the string that is between $ and comma
if(Header == “GPRMC”){ // output header of packet
Serial.print("Packet Header = ");
//Serial.println(Header);

int timestamp = in.indexOf(",", endHeader+1);
Ts = in.substring(endHeader+1, timestamp);
int st = in.indexOf(",", timestamp+1);
Status = in.substring(timestamp+1, st);
int Lat = in.indexOf(",", st+1);
Latitude= in.substring(st+1, Lat);
int lh = in.indexOf(",", Lat+1);
Latth = in.substring(Lat+1, lh);
int Lon = in.indexOf(",", lh+1);
Longitude = in.substring(lh+1, Lon);
int loh = in.indexOf(",", Lon+1);
LongH = in.substring(Lon+1, loh);
int knots = in.indexOf(",", loh+1);
Sk = in.substring(loh+1, knots);
int course = in.indexOf(",", knots+1);
Tc = in.substring(knots+1,course);
int date = in.indexOf(",", course+1);
Ds = in.substring(course+1, date);
int mv = in.indexOf(",", date+1);
MagVari = in.substring(date+1,mv);
int check = in.indexOf(",", mv+1);
Cs = in.substring(mv+1, check);

//Accel();

//GPSlog.print(now.year(), DEC); GPSlog.print(’/’); GPSlog.print(now.month(), DEC); GPSlog.print(’/’);
//GPSlog.print(now.day(), DEC); GPSlog.print(" ("); GPSlog.print(daysOfTheWeek[now.dayOfTheWeek()]); GPSlog.print(") “);
//GPSlog.print(now.hour(), DEC); GPSlog.print(’:’); GPSlog.print(now.minute(), DEC); GPSlog.print(’:’);GPSlog.print(now.second(), DEC);
//GPSlog.print(” , ");

// formula to convert decimal decimal to decimal degree
float f = atof(Latitude.c_str());
int firsttwodigits = ((int)f)/100;
float nexttwodigits = f -(float)(firsttwodigits*100);
float theFinalAnswer = (float)(firsttwodigits + nexttwodigits/60.0);
if(Latth == “S”){
theFinalAnswer = (-1)*theFinalAnswer;
}

GPSlog.print(theFinalAnswer,8);
GPSlog.print(" , “);
GPSlog.print(Latth);
GPSlog.print(” , ");

// formula to convert decimal decimal to decimal degree
float degWhole2=atof(Longitude.c_str());
int b = ((int)degWhole2)/100;
float degDec2 = degWhole2 -(float)(b*100);
float theFinalAnswer2 = (float)(b + degDec2/60.0);
if(LongH == “W”){
theFinalAnswer2 = (-1)*theFinalAnswer2;
}
GPSlog.print(theFinalAnswer2,8);
GPSlog.print(" , “);
GPSlog.print(LongH);
GPSlog.print(” , “);
GPSlog.println(Sk);
//GPSlog.print(“http://maps.google.com/maps?q=”);
//GPSlog.print(theFinalAnswer,8);
//GPSlog.print(”,+");
//GPSlog.println(theFinalAnswer2,8);
//GPSlog.print(“r\n”);
GPSlog.flush();

}
}

But even when I use the tiny gps library I get the same results of the Bluetooth and gps not working simultaneously.

What library you use to process the data from the GPS is irrelevant.

The issue is that only one instance of SoftwareSerial can listen at a time. If the Arduino is listening to the GPS is can NOT be listening to the bluetooth device.

If it is listening to the bluetooth device, it can NOT be listening to the GPS.

See reply #1 again.

Ok I switched the GPS to serial pin 0 and 1 and now they are both running simultaneously but when I checked the file in the SD card all I seen was zeros for the latitude and longitude but the orientation was correct. Does it have something to do with using a delay?

OK, you changed the code, and now it does something sort of right, but not exactly. And, you want us to guess what the problem is.

OK. I guess that your code still has problems.

What did I win?

OP, Please read the post:

How to use this forum - please read.