Reading values from bluetooth with parseInt

I am trying to read data via Bluetooth with softwareserial.h library, but Arduino reads all data coming from Bluetooth as "0". When I run the same code over the Serial monitor, my code works fine. I'm using Arduino Nano (Old Bootloader) and HC-05 (without button). This is the code that I read data from Bluetooth or Serial monitor.

 Serial.println("Speed: ");
    while (bluetooth.available() == 0) {}
    while (bluetooth.available() > 0) {
      speed_int = bluetooth.parseInt();
    }
    Serial.println(speed_int);
    stepper.setMaxSpeed(speed_int);

    Serial.println("Velocity: ");
    while (bluetooth.available() == 0) {}
    while (bluetooth.available() > 0) {
      velocity_int = bluetooth.parseInt();
    }
    Serial.println(velocity_int);
    stepper.setAcceleration(velocity_int);

image

Sent 1000, 200, 1, 0 via Arduino Bluetooth Control app terminal.

I would suggest that you look at the methods in the serial input basics tutorial. Send the data in a comma delimited string. Receive the data all at once and then parse out the values. Use start and end markers to make reception more robust.

1 Like

Please post your full code.

The zeroes can probably explained by the fact that parseXXX reads to the first invalid character and you still have a character remaining.

E.g.
Serial.println(123.4511, 2) will send 123.45 followed by a <cr> (\r) followed by a <lf> ('\n').
When you use parseXXX, it will parse up to the <cr> and the next parse fill see the <lf> which results in zero.

I can not say why you don't see the data (except for the zero).

Part of the problem here could be that just because there are characters available to be read does not mean there is an integer available to be read.

The characters that are available could be any non-digit characters, including spaces or non-printable characters like CR and LF. If that's true, the while loop will execute, the .parseInt() will read and ignore those characters (lookahead parameter defaults to SKIP_ALL) and then there may be no more characters to read, so .parseInt() will time out and return zero.

.parseInt() is very useful, but a problem with it is that it returns zero if it times out but will also return zero if it has read an integer which is zero, and your code won't know which of those 2 things happened.

If zero speed is not allowed, then it's not so much of a problem. If .parseInt() returns zero, your code can ignore it without needing to know if it was a timeout or a zero value read. But if zero speed is allowed, your code needs to work a different way to get around the problem.

This is why @groundFungus suggested start and end markers. With a start marker, you can avoid calling .parseInt() when there is no integer waiting to be read, or at least will follow quickly. The end marker will stop .parseInt() timing out waiting for another numeric character.

I tried the same code with a different hc06 module, and it worked without a problem. But when I connected it to my board with the same connections, it started reading wrong values and then read all data as 0. In the end, it stopped reading any data. I guess there is an issue with my battery pack.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.