Depicting Lat & Lon in Serial

Hi all,

I have gps coordinates that are being sent to my Arduino over serial (tx, rx), they look like this:

41.805103,-87.997513

In my code, I want to know the latitude and longitude. I have a for loop to do this but I get a class error every time and I don’t know how to fix this. I’m trying to get the String roverLat be connected to the latitude in the serial monitor. Unfortunately I’m not an expert with Stringtoints and that type of stuff so I couldn’t figure this out. :wink:

Here is my code:

#include <SoftwareSerial.h>
// software serial #1: RX = digital pin 10, TX = digital pin 11
SoftwareSerial portOne(10, 11);


void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // Start each software serial port
  portOne.begin(9600);
  
}

void loop() {
  portOne.listen();
 
  while (portOne.available() > 0) {
   String message = portOne.read();
    Serial.write(message);
    //message.toInt();
    for(int i = 0; i < 7; i++){
      String roverLat = message.indexOf(i);
    }
  } 
}

So you want a String that contains everything up to the ','? Maybe String roverLat = portOne.readStringUntil(','); would get you that. For the other part you could use String roverLon = portOne.readStringUntil('\n'); Look at Serial - Arduino Reference for other ways of reading from a serial port.

The information in the serial input basics thread may be of interest. It will allow reliable serial communication without using the problematic String class and is non-blocking. There is an example of receiving and parsing data from a string (null terminated character string [small s]).

Ok;

I'm still getting the same error of

"conversion from 'int' to 'String' is ambiguous"

Best to avoid Strings. They tend to make Arduinos crash.

Instead, use C-strings (zero terminated character arrays) and the efficient built in functions strcpy(), strcmp(), strtok(), etc.

But how would that avoid the error?

JackD5:
I’m still getting the same error of
“conversion from ‘int’ to ‘String’ is ambiguous”

It means that you have your data types so mixed up that the compiler can’t figure out what you intended. If you look at the full message you can see the full explanation:

Arduino: 1.8.2 (Mac OS X), Board: "Arduino/Genuino Uno"
/Users/john/Documents/Arduino/sketch_jun03a/sketch_jun03a.ino: In function 'void loop()':
sketch_jun03a:21: error: conversion from 'int' to 'String' is ambiguous
     String message = portOne.read();
                                   ^
/Users/john/Documents/Arduino/sketch_jun03a/sketch_jun03a.ino:21:35: note: candidates are:
In file included from /Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.18/cores/arduino/Arduino.h:231:0,
                 from sketch/sketch_jun03a.ino.cpp:1:
/Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.18/cores/arduino/WString.h:61:2: note: String::String(const __FlashStringHelper*) <near match>
  String(const __FlashStringHelper *str);
  ^
/Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.18/cores/arduino/WString.h:61:2: note:   no known conversion for argument 1 from 'int' to 'const __FlashStringHelper*'
/Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.18/cores/arduino/WString.h:59:2: note: String::String(const char*) <near match>
  String(const char *cstr = "");
  ^
/Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.18/cores/arduino/WString.h:59:2: note:   no known conversion for argument 1 from 'int' to 'const char*'

portOne.read() returns an ‘int’. You are trying to store that ‘int’ into a 'String" and the compiler doesn’t know of a way to store an ‘int’ in a ‘String’. It is fairly clear from context that you want to read more than one character into a buffer. The .read() function won’t do that. You need something like .readStringUntil()

Ok, I changed the line to

String   roverLat = portOne.readStringUntil(',');

Now I get an error from the String line:

for(int i = 0; i < 7; i++){
     String phoneLat = roverLat.indexOf[i];    <----
    }
  }

The error is :

invalid types '<unresolved overloaded function type>[int]' for array subscript

I don’t understand how to make it compile…
Should it be roverlat.charAt(i); I’m not sure what goes with what… :frowning:

The reference page for String is: String() - Arduino Reference

I suspect what you want to set phoneLat to the first seven characters of roverLat. Look at the reference for String.substring.