Go Down

Topic: Convert String From Serial Data to Numerical Value (Read 4195 times) previous topic - next topic

chiques


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.

PaulS

Quote
This looks like it's converting each ASCII character

Please don't post code using the invisible font.

HazardsMind

#17
Jan 16, 2013, 09:02 pm Last Edit: Jan 16, 2013, 09:11 pm by HazardsMind Reason: 1
Quote
atof (Convert string to double)
double = float.

Edit.

My GitHub:
https://github.com/AndrewMascolo?tab=repositories

PaulS

Quote
This method is used for arrays, not strings.

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

chiques

This will take some time to soak in.

Thanks

chiques

I'm not understanding why I'm unable to convert the Liters/Minute to a numberical value.

Quote

7.320,4.734,284.057
4.7340 Liters/Minute
0  in HEX



Code: [Select]

   if (sensor_stringcomplete){                                                 //if a string from the Atlas Scientific product has been received in its entirety
      Serial.println(sensorstring);                                             //use the hardware serial port to send that data to the PC

     
  for (int i=6; i<11; i++)  {
    Serial.print(sensorstring[i]);     //print each string character to verify
    float flowLong = (sensorstring[i] - '0');
                }
    Serial.print(flowLong);Serial.println(" Liters/Minute");
    if (flowLong < 4.5) {
    Serial.println("FLOW WARNING!!!");
    }
       Serial.print(flowLong,HEX);Serial.println("  in HEX");
       sensorstring = "";                                                      //clear the string buffer:
       sensor_stringcomplete = false;                                          //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
       }





Arrch

Start by fixing your indenting; it's atrocious and makes it very difficult to follow.

Code: [Select]
  for (int i=6; i<11; i++)  {
    ...
    float flowLong = (sensorstring[i] - '0');
  }


So loop through 5 characters in a string, creating a new float variable, assigning it a variable, and discarding it.

When you declare a variable like you are doing here, it is only valid inside the innermost curly braces. So the flowLong you create here isn't the same as the flowLong as you are printing to the screen. Not that fixing the scope issue will fix your issue; the algorithm you are using make absolutely no sense and will only result in floatLong containing the numeric value of the last digit.

Drop the String object. Use c style strings and atof().


zoomkat

Simple servo code that takes a numerical string from the serial monitor and converts it into a number for controlling a servo.

Code: [Select]

//zoomkat 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);

  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-delomit-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like 700, or 1500, or 2000,
  //or like 30, or 90, or 180,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          myservo.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          myservo.write(n);
        }

        //do stuff with the captured readString
        readString=""; //clears variable for new input
      }
    } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

chiques

What is the syntax to convert to floating?

Code: [Select]

int n = readString.toInt();

HazardsMind

My GitHub:
https://github.com/AndrewMascolo?tab=repositories

chiques


Easy, readString.toFloat();

Check this out,
http://mootools.net/docs/core/Types/Number


Uhm, not sure what I'm doing wrong.

Code: [Select]

int n = readString.toFloat();


Quote

error: 'class String' has no member named 'toFloat'

Arrch


Easy, readString.toFloat();


Not included with Arduino's version of the String object library, much like sprintf() doesn't accept the %f format specifier.

chiques



Easy, readString.toFloat();


Not included with Arduino's version of the String object library, much like sprintf() doesn't accept the %f format specifier.


Hi Arrrch,
Does this mean its not possible to convert a string to float in Arduino?

HazardsMind

#28
Jan 17, 2013, 08:13 pm Last Edit: Jan 17, 2013, 08:16 pm by HazardsMind Reason: 1
There is a patch you need to download to get .toFloat().
http://www.timewasters-place.com/arduino-string-and-float/

Also you wouldn't store a float value in an int, you would put it in a double.
So,
double n = readString.toFloat();
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Arrch


Hi Arrrch,
Does this mean its not possible to convert a string to float in Arduino?


Consider it a sign to stop using Strings and take a few minutes to learn about strings.

Go Up