String to Integer when contains a character

Hello All,

Thank you for reading this.. you are dealing with a beginner....

PROBLEM DESCRIPTION

I am trying to write some code to be able to extra data from a string and convert them into integers...
From the serial stream, the arduino will be receiving serially data of the type:

s-12.3,p-23.4,g12.3,

My code should be able to:

  1. Detect the string part starting with the character p (in the above example p23.4,)
  2. Get the integer between the character p and coma
  3. Finally convert it into an integer n so the resulting value is -23

I have put together the code below but im not quite there...

My goal is to seek your help to rewrite this so that the string gets augmented only when we get the "p"

something like this...

if ((mostSignificantDigit) == 'p'){
readString += c; // but we should find a way to remove the character "c" from the string and keep the incoming digits
}

CODE:

String readString;
String formatstring;
int n;

void setup() {
Serial.begin(9600); // initialize communication to the display
}

void loop() {
// STARTING READING THE STRING FROM HERE:

while (Serial.available()) {
delay(10); //small delay to allow input buffer to fill
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') { //
break;
} //breaks out of capture loop to print readstring
if (c != 'p') { // removing the string identifier from the string so that it contains only digits
readString += c;
}
} //makes the string readString

if (readString.length() >0) {
char mostSignificantDigit = readString.charAt(0);

if ((mostSignificantDigit) == 'p'){

}

char carray[10];
readString.toCharArray(carray, sizeof(carray));
n = atoi(carray);
Serial.println(n);

readString=""; //clears variable for new input
}
}

How to use this forum
take a look at point 7. :slight_smile:

  1. Get the integer between the character p and coma

The integer -23.4?

    if (c != 'p') { // removing the string identifier from the string so that it contains only digits
      readString += c;
    }

If you get anything but a 'p', store it in the String.

    char mostSignificantDigit = readString.charAt(0);

    if ((mostSignificantDigit) == 'p'){

Since you didn't store the p, this seems unlikely to ever be true.

Ditch the String class. Use a NULL terminated char array, and strtok() and atoi() (or atof()).

Hi PaulS,

Thank you for writing me back... Not really sure or what you mean ..could you please be kind enough to alter the code as suggested so that i can learn it? Again thank you so much for the help, patience and looking into this.

Paul wants you to look into not using the String class. He wants you to learn to use the C functions of strtok() and atoi() instead. I agree.

Hi All,

I managed to fix my own code by playing with the string... i attache the code here for future reference...

I'll look at the above commendation later but BIG THANK YOU for the answers...

/*
This program should be able to read string of the type

  1. p12.3,
  2. p-12.5

ignore strings with any other starting character like

f55,7,
g-23.6,

Expected output

  1. for case of "p12.3," n = 12
  2. for case of "p-12.5," n = -12
 */


String readString;
String formatstring;
int n;

void setup() {
  Serial.begin(9600); // initialize communication to the display
}

void loop() {
  // STARTING READING THE STRING FROM HERE:

  while (Serial.available()) {
    delay(10);  //small delay to allow input buffer to fill
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c;
    if (c == ',') {         // 
      break;
    }  //breaks out of capture loop to print readstring

  } //makes the string readString  

  if (readString.length() >0) {
    char mostSignificantDigit = readString.charAt(0);

    if ((mostSignificantDigit) == 'p'){

      formatstring = readString;

      readString.setCharAt(0,' ' );

      readString.trim();

      char carray[10];
      readString.toCharArray(carray, sizeof(carray));
      n = atoi(carray); 
      Serial.println(n);


    }



    readString=""; //clears variable for new input
  }
}