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.
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]).
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()