I have spent the last 4 days trying to find a solution myself. So rest assured, I am coming here to ask at the first instance. I also realise it is something very trivial but I am not able to spot it and therefore need help.
Ok basically my code receives serial data (GPS info) from another arduino. I am trying to split it into meaningful LAT and LONG and further down to degrees and minutes and then want to manipulate it arithmatically. Well thats the crux of it. Here is the code....
VARIABLE DEFINITION
char inChar;
String MASTERLATITUDEDEG,MASTERLATITUDEMIN,MASTERLATITUDE; //Strings to hold LAT / LONG data
String MASTERLONGITUDEDEG,MASTERLONGITUDEMIN,MASTERLONGITUDE;
String inData; // Buffer to store incoming commands from serial port
// Process message when new line character is recieved
if (inChar == '\n')
{
Serial.print("Data Received: ");
Serial.print(inData);
// Now there is an incomatibility between the two GPS data i.e FMR and OBGPS that needs to be corrected
// DATA Received from FMR 76.1726
// DATA Received by OBGPS 76.287811
// therefore (FMR data minutes *100)/60 = OBGPS data minutes
startMasterRxLAT = inData.indexOf('#'); // Marks the position where latitude info starts
startMasterRxLONG = inData.indexOf('@'); // Marks the position where logitude info starts
MASTERLATITUDEDEG= inData.substring(startMasterRxLAT+1,startMasterRxLAT+3);
MASTERLATITUDEMIN= inData.substring(startMasterRxLAT+3,startMasterRxLAT+8);
Serial.print("Master Lat Minutes Received: ");
Serial.println(MASTERLATITUDEMIN);
// Convert that to integer to manipulate data
int FMRLATMIN=(MASTERLATITUDEMIN.toInt());
Serial.print("Master Lat Minutes Converted: ");
Serial.println(FMRLATMIN);
OUTPUT
Data Received: Lat,#09.5773,Long@76.1727
Master Lat Minutes Received: .5773 <== I am able to strip the minutes down to this here
Master Lat Minutes Converted: 0 <== Its this toInt() portion shown in green above that I am not able to get I am hoping to manipulate
Take a look at "Serial Input Basics" from the Useful Links page. It shows you how to accumulate an array of characters (a string, not the String class) from Serial, while watching for special markers.
To accumulate the latitude, your sketch would start saving chars after a '#' is received and end after the '@' is received. That would also start saving the longitude, which would end after you receive a '\n'.
BTW, there is a Degrees-Minutes-Seconds feature in my NeoGPS library that you could use. If you convert the latitude "09.5773" to a long integer 95773000, you could use the DMS class to get the pieces:
float lat = atof( latChars ); // floating-point value 9.5773 from characters "09.5773"
int32_t latL = lat * 10000000.0; // scaled up to integer value 95773000
DMS dms;
dms.From( latL ); // converts 95773000 to separate pieces
Serial.print( dms.degrees ); // display the integer number of minutes
Serial.print( '\'' );
Serial.print( dms.minutes ); // display the integer number of minutes
Serial.print( '"' );
Serial.print( dms.seconds_whole ); // display the integer number of seconds
Serial.println( dms.NS() ); // display N or S for this latitude
NeoGPS also has range/distance, bearing and offset functions for locations, if you need those kind of calculations.
And if you want increased accuracy, the other Arduino could use NeoGPS to handle the GPS device and send the high-accuracy lat and long to this Arduino (e.g., up to 10 digits like 123.1234567). Other libraries use floating-point lat and long, which limits the accuracy to 7 digits (e.g. 123.1234).
NeoGPS is available from the Arduino IDE menu Sketch -> Include Library -> Manage Libraries.
Why does one substring start at the same place the other one ends?
Have you actually tried print() these substrings?
By the way, by convention, all capital letter names are constants. Constants do not appear on the left of an equal sign, except when being assigned the initial value.
septillion:
First, you're not using (the preffered) strings but Strings.
Second, please edit the post to include code tags. Makes everything a lot more readable. See How to use this forum.
And why are those variable names in unreadable and screamy all-caps?
And what is the format of the strings you receive?
Okay point noted about the uppercase and lower case for the variable names. I have correct that now.
If we could ignore that just for one second and revert back to my specific question, the output I receive is
Data Received: Lat,#09.5773,Long@76.1727
Master Lat Minutes Received: .5773 <== I am able to strip the minutes down to this here
Master Lat Minutes Converted: 0 <== Its this toInt() portion shown in green above that I am not able to get I am hoping to manipulate
So from the 09.5773, I am able to strip it down to .5773.
Why then is it that the following lines of code do not work?
int fmrlatmin=(masterlatitudemin.toInt());
Serial.print("Master Lat Minutes Converted: ");
Serial.println(fmrlatmin);
Also I am not sure I understand your comment "First, you're not using (the preffered) strings but Strings."
Master Lat Minutes Received: .5773 <== I am able to strip the minutes down to this here
Master Lat Minutes Converted: 0 <== Its this toInt() portion shown in green above that I am not able to get I am
Are you surprised? Integers are whole numbers and do not have fractions. You can convert to a float and store in a float variable (will probably work) or adjust the startposition in the substring method.