Convert String From Serial Data to Numerical Value

Hello Everyone,
I've been able to read and select a string of data to display on my terminal using:

for (int i=6;i<11;i=i+1) {
  Serial.print(sensorstring[i]);
      }
Serial.println(" Liters/Minute");
//

My goal is to convert this value to a 'number' so I can run an algorithm on it. I tried to use

Serial.parseInt()

but am unable to figure out the syntax and the Arduino Reference site does not have any examples.

Any insight is appreciated.

  int value = atoi(sensorstring);

-br

That "string" is an array so by using atoi(sensorstring) it will be converted to an integer.

Thanks for that, I tried:

      int value = atoi(sensorstring);
      Serial.println(value);

error: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'

Is it because it's reading sensorstring as '1'?

Sorry, it's probably time to show us all your code, please. I was guessing that sensorvalue was char[] not String.

-br

billroy:
Sorry, it's probably time to show us all your code, please. I was guessing that sensorvalue was char[] not String.

-br

No Problem, here it is:

#include <SoftwareSerial.h>                                                    //add the soft serial libray
#define rxpin 2                                                                //set the RX pin to pin 2
#define txpin 3                                                                //set the TX pin to pin 3


SoftwareSerial myserial(rxpin, txpin);                                         //enable the soft serial port


String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product


  void setup(){                                                                //set up the hardware
     Serial.begin(38400);                                                      //set baud rate for the hardware serial port to 38400
     myserial.begin(38400);                                                    //set baud rate for software serial port to 38400
     inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
     sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
     }
 
 
 
   void serialEvent() {                                                         //if the hardware serial port receives a char
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
              }  
 
 
 
 void loop(){                                                                   //here we go....
     
  if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty 
      myserial.print(inputstring);                                             //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }
 

  while (myserial.available()) {                                               //while a char is holding in the serial buffer
         char inchar = (char)myserial.read();                                  //get the new char
         sensorstring += inchar;                                               //add it to the sensorString
         if (inchar == '\r') {sensor_stringcomplete = true;}                   //if the incoming character is a <CR>, set the flag
         }
  
   for (int i=6;i<11;i=i+1) {
        Serial.print(sensorstring[i]);
        int value = atoi(sensorstring);
        Serial.println(value);
        }

   if (sensor_stringcomplete){                                                 //if a string from the Atlas Scientific product has been received in its entirety
       Serial.print(sensorstring);                                             //use the hardware serial port to send that data to the PC
       sensorstring = "";                                                      //clear the string:
       sensor_stringcomplete = false;                                          //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
      }
}

Oh you want sensorstring.toInt(), not sensorstring[ i ].

HazardsMind:
Oh you want sensorstring.toInt(), not sensorstring[ i ].

Hi HazardMind,
I'm looking for some sample code to see how this is used. Is this something similar to what you suggest?

I'm not home right now, but I will give you a sample as soon as I can.

Edit: I was able to get this from a post I did a while back. Wow my iPhone likes to stretch things out. Sorry about that.

/*
control test
 */
#include<string.h>
int DRV1,DRV2,STRR,STRL;
int x = 0;
int y = 0;
int s = 0;
void move(int x = 0,int y = 0,int s = 0);
//double z;
String val = "";
String  X= "";
String  Y= "";
String  state = "";
//int state = 0;
int currentCommand = 0;

int ledpin = 13;
byte Mopen = 4;
byte Mclosed = 2;
byte M1L = 3;// PWM
byte M2L = 5;// PWM
byte M1R = 9;// PWM
byte M2R = 6;// PWM

void setup()
{
  pinMode(ledpin, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
  pinMode(Mopen, OUTPUT);                                
  pinMode(Mclosed, OUTPUT);
  pinMode(M1L, OUTPUT);                                
  pinMode(M1R, OUTPUT);
  pinMode(M2L, OUTPUT);                                
  pinMode(M2R, OUTPUT);
  Serial.begin(9600);       // start serial communication at 115200bps

}

void loop() {
  if( Serial.available())       // if data is available to read
  { 
    digitalWrite(ledpin, HIGH);
    char c= Serial.read();
    if (c == ','){    //look at the incoming chars, if it is a ',' then switch the case
      currentCommand++; 
    }
    else {   //if it is not ',' then store chars in string
      val += c;
      //Serial.println(val);
      switch (currentCommand) {

      case 0:
        X += val;
        val = "";
        break;
 
      case 1:
        Y += val;
        val = "";
        break;

      case 2:
      //Serial.println("X: "+X);
      //Serial.println("Y: "+Y);    
        state = val;   // this will only receive 1 value
        currentCommand = 0;
        val = "";
        //Serial.println("state: "+state);
        //Serial.println();
        x=X.toInt();  //string to int 
        y=Y.toInt();  //string to int
        s=state.toInt();  //string to int
        X=""; Y=""; state="";
        move(x, y, s);
        break;
      }
    } 
  }
} 
    
  void move(int x, int y, int s)
  {  
      x=constrain(x, -90, 90); 
      y=constrain(y, -90, 90);

  //Movement varibles
  
  int DRV2 = map(x, -90, 0, 255, 0);
  int DRV1 = map(x, 0, 90, 0, 255);
  int STRR = map(y, -90, 0, 255, 0);
  int STRL = map(y, 0, 90, 0, 255);  

  if(x > 0)//forwards               
  {
    //Serial.println("Driving"); 
    analogWrite(M1L, abs(DRV1 - STRL)); analogWrite(M1R, abs(DRV1 - STRR));   
    analogWrite(M2L, 0); analogWrite(M2R, 0);   
  }
  else if(x < 0)//backwards               
  {
    //Serial.println("Driving"); 
    analogWrite(M1L, 0); analogWrite(M1R, 0);   
    analogWrite(M2L, abs(DRV2 - STRR)); analogWrite(M2R, abs(DRV2 - STRL));   
  }
  else if(x == 0 && y >0)//right              
  {
    //Serial.println("Driving"); 
    analogWrite(M1L, STRL); analogWrite(M1R, 0);   
    analogWrite(M2L, 0); analogWrite(M2R, STRL);   
  }
  else if(x == 0 && y < 0)//left              
  {
    //Serial.println("Driving"); 
    analogWrite(M1L, 0); analogWrite(M1R, STRR);   
    analogWrite(M2L, STRR); analogWrite(M2R, 0);   
  }

  else //full stop
  { 
    digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);        
    digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);    
  }

   if(s == 1)
    {
      //Serial.println("Claw Opening");
      digitalWrite(Mopen, HIGH); digitalWrite(Mclosed, LOW);
    }
    if(s == 2)
    {
      //Serial.println("Claw Closing");
      digitalWrite(Mopen, LOW); digitalWrite(Mclosed, HIGH);
    }
    if(s == 0)
    { 
      digitalWrite(Mopen, LOW);  digitalWrite(Mclosed, LOW);
    } 
   else{
    digitalWrite(ledpin, LOW);
    x=0; y=0; s=0;
  } 
 }

You could also try something like this:

int bufsize=20 ;
char buffer[bufsize] ;
yourString.toCharArray( buffer, bufsize );
int val = atoi( &buffer[0] );

provided it is an integer and not a number with a decimal point in it, this might work.
if you like your strings to be simple character arrays, use C
if you like your strings to be objects, use Java
Dealing with both can be a real pain in the neck.

HazardsMind:
I'm not home right now, but I will give you a sample as soon as I can.

Edit: I was able to get this from a post I did a while back. Wow my iPhone likes to stretch things out. Sorry about that.

/*

control test
*/
#include<string.h>
int DRV1,DRV2,STRR,STRL;
int x = 0;
int y = 0;
int s = 0;
...

Hi HazardsMind,

I went through the sample you suggested and noted what "I think" each line does. The only one which I am figuring out are:

  1. void move(int x = 0,int y = 0,int s = 0); //Not sure why this needs to be done ???
  1. move(x, y, s); // DO NOT UNDERSTAND "move()"
//from http://arduino.cc/forum/index.php?topic=142777.0

/*
control test //name of program
 */
#include<string.h> //name of library needed
int DRV1,DRV2,STRR,STRL; //declare 4 integers
int x = 0; //intialize x
int y = 0; //initialize y
int s = 0; //initialize s
void move(int x = 0,int y = 0,int s = 0);  //Not sure why this needs to be done ???
//double z; //double the z variable (for more precision)
String val = ""; //set some buffer aside for val
String  X= ""; //set some buffer aside for X
String  Y= ""; //set some buffer aside for Y
String  state = ""; //set some buffer aside for state
//int state = 0; //initialize state to 0
int currentCommand = 0; //initialize current command to 0

int ledpin = 13; //set LED pin
byte Mopen = 4; //sets Mopen to B0100
byte Mclosed = 2; //sets Mclosed to B00010
byte M1L = 3;// PWM
byte M2L = 5;// PWM
byte M1R = 9;// PWM
byte M2R = 6;// PWM

void setup()
{
  pinMode(ledpin, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
  pinMode(Mopen, OUTPUT);  // pin Mopen is set as an output                              
  pinMode(Mclosed, OUTPUT); // pin Mclosed is set as an output
  pinMode(M1L, OUTPUT);     //pin MIL is set as an output
  pinMode(M1R, OUTPUT);  // in MIR is set as an output
  pinMode(M2L, OUTPUT);  //pin M2L is set as an output 
  pinMode(M2R, OUTPUT); // pin M2R is set as an output
  Serial.begin(9600);       // start serial communication at 115200bps

}

void loop() {
  if( Serial.available())       // if data is available to read, this is how to check the serial part for strings
  { 
    digitalWrite(ledpin, HIGH); // if data is detected set the pint set to ledpin to HIGH (ON)
    char c= Serial.read(); // the string in serial read() is buffered in to c as characters
    if (c == ','){    //look at the incoming chars, if it is a ',' then switch the case
      currentCommand++;  //for some reason currentCommand increments????
    }
    else {   //if it is not ',' then store chars in string, keep storing the string until a , is found
      val += c;  // same as (val + c = c)val is the next string + the previous character stored in buffer
      //Serial.println(val);
      switch (currentCommand) { // for each string, currentCommand appears to increment by 1

      case 0:  //in the even there was only 1 string before the ',' X is equal to the val in buffer and val buffer is cleared ""
        X += val; // same as (X + val = val), new variable X is equal to the whole string val before the ','
        val = ""; // clear the information in the val buffer
        break; // break out of case 0
 
      case 1: //if currentCommand equals 1
        Y += val; // same as (Y + val = val), Y is now the string waiting in the val buffer
        val = ""; // clear out the val buffer
        break; //get out of case 2

      case 2:
      //Serial.println("X: "+X);
      //Serial.println("Y: "+Y);    
        state = val;   // this will only receive 1 value
        currentCommand = 0; //reset currentCommand counter to 0
        val = ""; // clear out the val buffer
        //Serial.println("state: "+state);
        //Serial.println();
        x=X.toInt();  //string to int , convert string X to integer
        y=Y.toInt();  //string to int, convert string Y to integer
        s=state.toInt();  //string to int, convert state to integer
        X=""; Y=""; state=""; //clear the X, Y and state buffer information
        move(x, y, s); // DO NOT UNDERSTAND "move()"  
        break; // get out of case 3
      }
    } 
  }
} 
//***ALL THIS STUFF BELOW IS FOR SERVO MOVEMENT
//***SENDS OUT VOLTAGE VALUES
  void move(int x, int y, int s)
  {  
      x=constrain(x, -90, 90); 
      y=constrain(y, -90, 90);

  //Movement varibles
  
  int DRV2 = map(x, -90, 0, 255, 0);
  int DRV1 = map(x, 0, 90, 0, 255);
  int STRR = map(y, -90, 0, 255, 0);
  int STRL = map(y, 0, 90, 0, 255);  

  if(x > 0)//forwards               
  {
    //Serial.println("Driving"); 
    analogWrite(M1L, abs(DRV1 - STRL)); analogWrite(M1R, abs(DRV1 - STRR));   
    analogWrite(M2L, 0); analogWrite(M2R, 0);   
  }
  else if(x < 0)//backwards               
  {
    //Serial.println("Driving"); 
    analogWrite(M1L, 0); analogWrite(M1R, 0);   
    analogWrite(M2L, abs(DRV2 - STRR)); analogWrite(M2R, abs(DRV2 - STRL));   
  }
  else if(x == 0 && y >0)//right              
  {
    //Serial.println("Driving"); 
    analogWrite(M1L, STRL); analogWrite(M1R, 0);   
    analogWrite(M2L, 0); analogWrite(M2R, STRL);   
  }
  else if(x == 0 && y < 0)//left              
  {
    //Serial.println("Driving"); 
    analogWrite(M1L, 0); analogWrite(M1R, STRR);   
    analogWrite(M2L, STRR); analogWrite(M2R, 0);   
  }

  else //full stop
  { 
    digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);        
    digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);    
  }

   if(s == 1)
    {
      //Serial.println("Claw Opening");
      digitalWrite(Mopen, HIGH); digitalWrite(Mclosed, LOW);
    }
    if(s == 2)
    {
      //Serial.println("Claw Closing");
      digitalWrite(Mopen, LOW); digitalWrite(Mclosed, HIGH);
    }
    if(s == 0)
    { 
      digitalWrite(Mopen, LOW);  digitalWrite(Mclosed, LOW);
    } 
   else{
    digitalWrite(ledpin, LOW);
    x=0; y=0; s=0;
  } 
 }

Phew, where to begin...

  1. void move(int x = 0,int y = 0,int s = 0); //Not sure why this needs to be done ???

This is a function prototype. The Arduino IDE handles these for you, so typically these are not necessary.

  1. move(x, y, s); // DO NOT UNDERSTAND "move()"

It's just a call to the function with the provided values.
if( Serial.available())       // if data is available to read, this is how to check the serial part for strings
characters, not strings.
char c= Serial.read(); // the string in serial read() is buffered in to c as characters
the character is read, not the string

if (c == ','){    //look at the incoming chars, if it is a ',' then switch the case
      currentCommand++;  //for some reason currentCommand increments????
    }

If the character is a deliminator (comma in this case) increment the number of commands received.

    else {   //if it is not ',' then store chars in string, keep storing the string until a , is found
      val += c;  // same as (val + c = c)val is the next string + the previous character stored in buffer
      //Serial.println(val);

If it's not a comma, concatenate it onto the String val (note the difference in capitalization, String and string are not the same thing).

void move(int x = 0,int y = 0,int s = 0); //Not sure why this needs to be done ???

Its not needed, you can take it out. I originally wrote this code using a C++ based debugger software (old software), and it had to be inserted at the top. so when I brought it to arduino, I forgot to take it out.

move(x, y, s); // DO NOT UNDERSTAND "move()"

Move() is a function I made that feeds those gotten values to my motors, it is get rid of too much cluster in my Loop function.

It is very difficult to modify code on an IPhone, so I just left everything in. I just wanted to show you an example of how to get, convert and split the data to different ints.

And they are not servos, they are DC motors. However same can be done with servos.

Arrch:
Phew, where to begin...
...

Thanks Arrch,
I would like convert this into a floating number but I'm having a hard time finding the Reference syntax in Arduino for the following C equivalents? Would you happen to know where I can find them?

atol (Convert string to long integer) obvoiusly this is "toInt()"
atof (Convert string to double)
strtol (Convert string to long integer)

You need to be clearer about what you want to do. There is a huge difference between a string and a String. The string is not a class and does not have a toInt() method.

Frankly, you should not be using the String class at all. It has a major problem that is going to, sooner or later, bite you.

PaulS:
You need to be clearer about what you want to do. There is a huge difference between a string and a String. The string is not a class and does not have a toInt() method.

Frankly, you should not be using the String class at all. It has a major problem that is going to, sooner or later, bite you.

Now I'm really lost. My goal is to convert the second string to a value so I can write some algorithms.
0.000,0.000,0.000

This looks like it's converting each ASCII character

String : ASCII

0 : 48
. : 46
0 : 48
0 : 48
0 : 48

Ref: http://arduino.cc/en/Tutorial/ASCIITable

I know this doesn't solve my conversion problem but I'm practicing difference string conversions.

This looks like it's converting each ASCII character

Please don't post code using the invisible font.

atof (Convert string to double)

double = float.

Edit.

This method is used for arrays, not strings.

It most certainly is for strings. It is NOT for non-NULL-terminated arrays of chars.

This will take some time to soak in.

Thanks