HC-05 receives "-1" from MIT App Inventor

I have a robot controlled through slave HC-05, with signal coming from a MIT App Inventor written program.

The idea of the code is simple: on touchdown, a button on the app will transmit a number(let’s say “1”) to the Arduino, and the Arduino will do something (turn in a direction) until it receives another signal transmitted on button touch up.

Most motor:

if(servopos==1){
       for(int i=0;i<200;i++){
         if(wristpos<180){
           wristpos +=1;
           wristservo.write(wristpos);
           delay(5);
           servopos = bluetooth.read();
           if(servopos==3){
             break;
           }
         }
       }
     }
     if(servopos==2){
        for(int i=0;i<200;i++){
          if(wristpos>0){
            wristpos -=1;
            wristservo.write(wristpos);            
            delay(5);
            servopos = bluetooth.read();
            if(servopos==3){
              break;  
            }
          }
        }
      }

For the most part the robot work well, but this certain motor named arm1 jerks and returns to starting position when it receives a “-1” signal randomly. Now nowhere in my code did I use or transmit a “-1”, but I can see it from serial monitor.

arm1 code:

 if(servopos==14){
       for(int i=0;i<200;i++){
         if(armpos1<180){
           armpos1 +=1;
           armservo1.write(armpos1);
           delay(20);
           servopos = bluetooth.read();
           if(servopos==16){
             break;
           }
         }
       }
     }
     if(servopos==15){
        for(int i=0;i<200;i++){
          if(armpos1>0){
            armpos1 -=1;
            armservo1.write(armpos1);
            delay(20);
            servopos = bluetooth.read();
            if(servopos==16){
              break;  
            }
          }
        }
      }

I don’t know if this is a bug from the app code, or from the arduino Bluetooth receiver.

Attached is the MIT app inventor code and arduino code.

Final.ino (9.09 KB)

exoen144:
I don't know if this is a bug from the app code, or from the arduino Bluetooth receiver.

The last thing you blame is the bluetooth receiver. It does nothing more than pass on what you sent to it. You can make a simple test by using a standard bluetooth terminal on the phone and send the same data.

If you get the same result, your Arduino code is faulty.

If it works OK, you app inventor code is faulty.

For the most part the robot work well, but this certain motor named arm1 jerks and returns to starting position when it receives a "-1" signal randomly. Now nowhere in my code did I use or transmit a "-1", but I can see it from serial monitor.

servopos = bluetooth.read();

The .read() function will return -1 if there is no data in the buffer to read.

Either use an if (bluetooth.available()>0) conditional, or else make sure that the code can deal with -1 as a servopos.

In my opinion, you should not perform a .read() operation unless you know there is something there to read.

Thank you! That was what I was suspecting but couldn't find any documentation about.

In my opinion, you should not perform a .read() operation unless you know there is something there to read.

May I ask how would you know when to read?

May I ask how would you know when to read?

if (bluetooth.available()>0)
{
servopos = bluetooth.read();//read one byte
}

Since I have no idea what you are sending from the phone, and what the data format is and line ending issues the reading of a single byte may not be appropriate, but you have used it in the code successfully when it does not return -1.

You may want to review Robin2’s excellent turorial on Serial Input Basics.

exoen144:
That was what I was suspecting but couldn't find any documentation about.

Here it is:
https://www.arduino.cc/en/Reference/SoftwareSerialRead

Returns
the character read, or -1 if none is available