0

im sending string data via serial with following format ex. :45:60 or such as ":120:57".i got the data shown in Serial.println (secondValue) Serial.println (thirdValue); but i cant convert it to int

String myString;
char c;
int Index1,Index2,Index3, azi;
String secondValue, thirdValue;
void setup(){
Serial.begin(9600);}
void loop(){
while (Serial.available()>0)
{
delay(10);
c = Serial.read();
 myString += c;
}
 if (myString.length()>0)
{
Index1 = myString.indexOf(':');
Index2 = myString.indexOf(':', Index1+1);
Index3 = myString.indexOf(':', Index2+1);
secondValue = myString.substring(Index1+1, Index2);
thirdValue = myString.substring(Index2+1, Index3);
Serial.println(secondValue);
Serial.println(thirdValue);
myString="";
}
delay(1000);
}
asked Dec 15, 2018 at 12:49
4

1 Answer 1

1

All numbers are integers? If yes, you can use toInt() function.

int secondValueInt, thirdValueInt;
secondValueInt = secondValueInt.toInt();
thirdValueInt = thirdValue.toInt();
answered Dec 15, 2018 at 14:28
1
  • thx a lot bro, you are a life saver (Y) Commented Dec 15, 2018 at 15:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.