-1

I am using a HC-06 and i want to read a number ending with f as a delimiter, the first three numbers as my servo position, however converting the string into int is giving an endless series of 0s. I added the Serial.print(servopos) to see that its a never ending series of 0s, and nothing seems to fix this problem. What am I doing wrong? (I tried using long for servopos instead of int, it didn't fix the issue)

#include <SoftwareSerial.h>
#include <Servo.h> 

Servo myservo; // servo name

int bluetoothTx = 10; 
int bluetoothRx = 11;
int fwd = 12; 
int bwd = 13;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
String content = "";
char character;

void setup()
{
  myservo.attach(9); // attach servo signal wire to pin 9
  //Setup usb serial connection to computer
  Serial.begin(9600);

  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);

  pinMode(fwd, OUTPUT);
  pinMode(bwd, OUTPUT);

}

void loop()
  { 
  if(bluetooth.available()> 0 ) // receive number from bluetooth

    character = bluetooth.read();

    if (character!='f'){
        content += (char) character;
        delay(10);
      }

    else{
      int servopos = content.substring(0,3).toInt(); 
      Serial.print(servopos);
     // myservo.write(servopos); 

      String directMvt = content.substring(3);
      if (directMvt == "00"){
            digitalWrite(fwd, LOW);
            digitalWrite(bwd, LOW);
      }
      else if (directMvt == "10"){
            digitalWrite(fwd, HIGH);
            digitalWrite(bwd, LOW);
      }
      else if (directMvt == "01"){
            digitalWrite(fwd, LOW);
            digitalWrite(bwd, HIGH);
      }
      else if (directMvt == "11"){
            digitalWrite(fwd, LOW);
            digitalWrite(bwd, LOW);
      }
      content = "";
  }
}

closed as unclear what you're asking by per1234, VE7JRO, Juraj, MatsK, jose can u c Apr 3 '18 at 15:16

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Have you tried examining the contents of directMvt yet? – Ignacio Vazquez-Abrams Mar 31 '18 at 15:39
  • 2
    Don't post code as picture. Use copy paste – chrisl Mar 31 '18 at 15:42
  • Instead of printing the converted number, try printing the substring, that you try to convert in the line above. Most likely the substring doesn't contain only numbers – chrisl Mar 31 '18 at 15:45
  • Don't put delays in serial receiving code! – Chris Stratton Mar 31 '18 at 18:12
  • I printed the directMvt and the substring, they give the right answer, the problem seems to be in the toInt() function @IgnacioVazquez-Abrams – Elie Apr 1 '18 at 7:40
0

I've found your problem: You have a missing pair of { }, that should enclose the if(bluetooth.available()>0) statement. Without the parenthesis it only encloses the following statement, which is the serial reading.

Now follow the logic: Before you are sending something over serial, the global variable character should be zero. So then, since the NULL character is not equal to f, the NULL characters are added to the string. Then you send a string like 18000f via bluetooth serial. The code is adding the characters to the string variable, until it find the f character. Now it goes directly in the else-statement, printing the value of servopos and then resetting the string variable. On the next loop iteration, there isn't something available at the SoftwareSerial, so it goes to the if(character !='f') statement. The character variable is global and it is still filled with the f value, that it got in the last loop iteration. So the else statement is executed forever very fast. But since you reseted the content of the string variable in the last iteration, there is nothing to convert to integer here. So this gives you a forever lasting series of zeros.

Enclosing the code in the first if statement solves your problem, since then, it only does something, when data is available at the serial interface. Also you should consider using a local variable for character, since you only need it there and your problem would have been obvious (through a compiler error), if you used a local variable here. Using global variables, when there is no need to, can make things more complex and error-prone.

Not the answer you're looking for? Browse other questions tagged or ask your own question.