Pages: 1 [2] 3   Go Down
Author Topic: Convert String From Serial Data to Numerical Value  (Read 3840 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This looks like it's converting each ASCII character
Please don't post code using the invisible font.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 108
Posts: 3785
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
atof (Convert string to double)
double = float.

Edit.

« Last Edit: January 16, 2013, 03:11:35 pm by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This will take some time to soak in.

Thanks
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
   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
       }



Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
  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().

Logged

0
Offline Offline
Tesla Member
***
Karma: 147
Posts: 9750
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
//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
    }
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the syntax to convert to floating?

Code:
int n = readString.toInt();
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 108
Posts: 3785
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Easy, readString.toFloat();

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

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Easy, readString.toFloat();

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

Uhm, not sure what I'm doing wrong.

Code:
int n = readString.toFloat();

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

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Easy, readString.toFloat();

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

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 108
Posts: 3785
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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();
« Last Edit: January 17, 2013, 02:16:59 pm by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: 1 [2] 3   Go Up
Jump to: