Assigning Multiple Numbers From Serial to Float

Hey everyone,

As a quick background, I'm building an incubator with temperature tolerance parameters that can be adjusted via Bluetooth (HC-05). I have it set to print "Enter low-end target temperature." when the user submits the character, "b" to the serial (assigned to char "state"). Following that, it waits for numeric input, which it assigns to "state", and then float "low". Then, it asks for a high-end target temperature (float "high"). My code for that section is as follows:

  if (state == 'b'){
    Serial.println("Enter the low-end target temperature.");
    do {
      if(Serial.available()>0){
        state = Serial.read();
      }
    } while (isAlpha(state)==true);
      
      if (isDigit(state)==true){
        Serial.println(state);
        low = atof(&state);
        state = 'z';
        Serial.println("Enter the high-end target temperature.");
        do {
          if(Serial.available()>0){
          state = Serial.read();
          }
        } while (isAlpha(state)==true);
        
          if (isDigit(state)==true){
            Serial.println(state);
            high = atof(&state);
            state = 'z';
            Serial.print("Temperature tolerance range set from ");
            Serial.print(low);
            Serial.print("C to ");
            Serial.print(high);
            Serial.println("C.");
          }
      }
  }
}

While I'm using single digits, the code works flawlessly. When I try to use two digits, it only takes one for each value. For example, if I type in "27" for low, it will assign 2 to "low" and 7 to "high". If I understand correctly, my research has taught me the Arduino simply reads one of the digits from the serial, which it assigns before the second digit is read. I have come across several solutions for issues similar to this, but I am very new to Arduino and did not understand how to apply them under the context of my project. Any insight is greatly appreciated!

Thanks!

Maybe reading the updated Serial Input Basics thread might get you on the right track.

PS
Karma added for using code tags in your first post.

sterretje:
Maybe reading the updated Serial Input Basics thread might get you on the right track.

Thank you! I came across that post shortly before writing this thread and have toyed with some of the examples a little (specifically the second, since char can be converted into float), but am ignorant enough to not quite understand how to work them into my code. That being said, it seems the second example is specifically tailored to receive 32 characters. Would I need to modify "numChars" to be equal to 2 (permitting a receivable range of 00-99)? If I understand that correctly, I'm not sure I see the purpose in having it recognize an end character.

Thanks again!

fourgas4:
That being said, it seems the second example is specifically tailored to receive 32 characters. Would I need to modify "numChars" to be equal to 2 (permitting a receivable range of 00-99)?

No, that is not the correct idea. The 32 just sets aside space for a message up to 32 chars long (and you can change it if you need more space).

Start by trying the second example in Serial Input Basics on its own with no changes at all. That should allow you to enter the numbers that you want and have them displayed.

If that works then you need to use the atoi() function to convert ascii to integer or the atof() function to convert ascii to float. That is illustrated in the parse example.

You might consider sending your data in the form <b,99> in which case the 3rd example and the parse example would be more appropriate.

You might also like to look at the simple user-input example in Planning and Implementing a Program

...R

Robin2:
Start by trying the second example in Serial Input Basics on its own with no changes at all. That should allow you to enter the numbers that you want and have them displayed.

Thank you for the response! I've tinkered with it off and on for the past week, but haven't quite figured up a solution. I managed to get the relevant codes in that link working, but I haven't managed to apply them to my project. Between that thread and a few others I sifted through, I devised the code below. It seems to successfully execute all the way through, but when it prints "state" at the end, it shows a symbol depicting an unrecognized character (�). All of the variables are int except state, which is char. Do you happen to have any insight?

if (state == 'b'){                
    Serial.println("Enter the low-end target temperature.");
    state = 'z';    
    counter = 2;
    Serial.println("Counter.");
    while (counter = 2){
      trigger = Serial.read();
      if (isDigit(trigger)) break;
    }
      if (counter == 2){
        valueHolder[3] = Serial.read();
        state += valueHolder[3];
        counter -= 1;
        Serial.println("First indicator.");  //Indicates the code has successfully executed previous steps.
      }
      if (counter == 1){
        valueHolder[3] = Serial.read();
        state += valueHolder[3];
        counter -= 1;
        Serial.println("Second indicator.");  //Indicates the code has successfully executed previous steps.
        Serial.println(state);
      }
    }

your snippet doesn't tell the whole story...

what commands would your code expect?

(e.g. 'b' followed by a number followed by a new line character)

fourgas4:
While I'm using single digits, the code works flawlessly. When I try to use two digits, it only takes one for each value.

There are two ways to do numeric input.

The simple way is to accept each digit individually and total them as you go. That is, reset the number to zero and start accepting digits. Each time you get a digit, multiply the total by 10 and add the next digit. This works ok for positive integers.

The other way is to read the digits into a char[] buffer. When there are no more digits (eg, the user has hit enter or tab), parse the number using atoi or atof.

fourgas4:
Thank you for the response! I've tinkered with it off and on for the past week, but haven't quite figured up a solution. I managed to get the relevant codes in that link working, but I haven't managed to apply them to my project.

If you post the program that represents your best attempt I will try to help.

...R