Go Down

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


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


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


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


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() {

  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.print("writing Angle: ");

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

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


What is the syntax to convert to floating?

Code: [Select]

int n = readString.toInt();


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

Go Up