Offline
Newbie
Karma: 0
Posts: 37
|
 |
« Reply #15 on: January 16, 2013, 02:51:08 pm » |
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/ASCIITableI know this doesn't solve my conversion problem but I'm practicing difference string conversions.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #16 on: January 16, 2013, 02:59:19 pm » |
This looks like it's converting each ASCII character Please don't post code using the invisible font.
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1589
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #17 on: January 16, 2013, 03:02:17 pm » |
atof (Convert string to double) double = float. Edit.
|
|
|
|
« Last Edit: January 16, 2013, 03:11:35 pm by HazardsMind »
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #18 on: January 16, 2013, 03:06:17 pm » |
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
Newbie
Karma: 0
Posts: 37
|
 |
« Reply #19 on: January 16, 2013, 03:09:13 pm » |
This will take some time to soak in.
Thanks
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 37
|
 |
« Reply #20 on: January 16, 2013, 06:20:08 pm » |
I'm not understanding why I'm unable to convert the Liters/Minute to a numberical value. 7.320,4.734,284.057 4.7340 Liters/Minute 0 in HEX
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
Edison Member
Karma: 41
Posts: 1888
|
 |
« Reply #21 on: January 16, 2013, 06:43:52 pm » |
Start by fixing your indenting; it's atrocious and makes it very difficult to follow. 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
Online
Tesla Member
Karma: 51
Posts: 6590
Arduino rocks
|
 |
« Reply #22 on: January 16, 2013, 07:26:02 pm » |
Simple servo code that takes a numerical string from the serial monitor and converts it into a number for controlling a servo. //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
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 37
|
 |
« Reply #23 on: January 17, 2013, 01:55:22 pm » |
What is the syntax to convert to floating? int n = readString.toInt();
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1589
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #24 on: January 17, 2013, 01:57:23 pm » |
Easy, readString.toFloat(); Check this out, http://mootools.net/docs/core/Types/Number
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 37
|
 |
« Reply #25 on: January 17, 2013, 02:06:26 pm » |
Uhm, not sure what I'm doing wrong. int n = readString.toFloat();
error: 'class String' has no member named 'toFloat'
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1888
|
 |
« Reply #26 on: January 17, 2013, 02:09:02 pm » |
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
Newbie
Karma: 0
Posts: 37
|
 |
« Reply #27 on: January 17, 2013, 02:11:24 pm » |
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
Edison Member
Karma: 29
Posts: 1589
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #28 on: January 17, 2013, 02:13:42 pm » |
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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1888
|
 |
« Reply #29 on: January 17, 2013, 02:28:43 pm » |
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
|
|
|
|
|
|