Unable to parse serial integer

Hi everyone,

Been at this all day and just can't seem to be able to make this work.

I'm trying to receive a 2 digit integer from the serial monitor and save it to a variable.

My code works great for my bluetooth serial connection, but causes it to hang when i try to enter a value through the terminal.

 if (BTserial.available() > 0) {   // something came across serial
    integerValue = 0;         // throw away previous integerValue
    while (1) {           // force into a loop until 'n' is received
      incomingByte = BTserial.read();
      if (incomingByte == '.') break;   // exit the while(1), we're done receiving
      if (incomingByte == -1) continue;  // if no characters are in the buffer read() returns -1
      integerValue *= 10;  // shift left 1 decimal place
      // convert ASCII to integer, add, and shift left 1 decimal place
      integerValue = ((incomingByte - 48) + integerValue);
      SetPoint = integerValue;}
    BTserial.print("SetPoint is now ");
    BTserial.print(SetPoint);
    BTserial.println("Deg F");
    Serial.print("SetPoint is now ");
    Serial.print(SetPoint);
    Serial.println("Deg F");

  }

 if (Serial.available() > 0) {   // something came across serial
    integerValue = 0;         // throw away previous integerValue
    incomingByte = 0;
    while (1) {           // force into a loop until 'n' is received
     
      incomingByte = Serial.read();
      
      if (incomingByte == '.') break;   // exit the while(1), we're done receiving
      if (incomingByte == -1) continue;  // if no characters are in the buffer read() returns -1
      
      integerValue *= 10;  // shift left 1 decimal place
      // convert ASCII to integer, add, and shift left 1 decimal place
      integerValue = ((incomingByte - 48) + integerValue);
      SetPoint = integerValue;
      if (SetPoint > 100) {
      SetPoint = 70;
      }

    }
    BTserial.print("SetPoint is now ");
    BTserial.print(SetPoint);
    BTserial.println("Deg F");
    Serial.print("SetPoint is now ");
    Serial.print(SetPoint);
    Serial.println("Deg F");

  }

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R

These lines:

      integerValue *= 10;  // shift left 1 decimal place

      integerValue = ((incomingByte - 48) + integerValue);

are out of order. You need to subtract the '0' before you have a "pure" integer value that can be manipulated in a mathematical sense.

econjack:
These lines:

      integerValue *= 10;  // shift left 1 decimal place

integerValue = ((incomingByte - 48) + integerValue);




are out of order. You need to subtract the '0' before you have a "pure" integer value that can be manipulated in a mathematical sense.

Looks OK to me.
Of course, setting integerValue to zero each time may not be the smartest thing to do.
I'd prefer to subtract '0', just to make it clearer.

econjack:
These lines:

      integerValue *= 10;  // shift left 1 decimal place

integerValue = ((incomingByte - 48) + integerValue);




are out of order. You need to subtract the '0' before you have a "pure" integer value that can be manipulated in a mathematical sense.

I've tried swapping those two lines, and then the code doesn't work at all, as is, the first value i enter correctly adjusts the set point, but any subsequent values cause the setpoint to change to an undesired value

I just don't understand why exactly the same code on the BT serial port works flawlessly :frowning:

      if (incomingByte == '.') break;   // exit the while(1), we're done receiving
      if (incomingByte == -1) continue;  // if no characters are in the buffer read() returns -1

Do you indeed send the dot? Do you receive it?

Maybe a Serial.println(incomingByte, HEX) after the reading of the port will show why it's stuck?

Is it a good idea to loop? Won't this block the serial port from actually getting anything?

It does not block the serial port (obviously blocks the rest of the code) as filling of the RX buffer is interrupt driven.