Bluetooth HC-05 data reading problem with servos

I have a problem with bluetooth module HC-05 and servos. If I type in bluetooth terminal 4,1 I want that servo1 turns 60 degrees 3 times. Briefly servo1 turns 60 degrees and then servo2 goes 180 and turns back and again servo1 turns 60 degrees and so on.

The bluetooth mixes the numbers. If I type “1” it gives me “49” and that’s why the system is not working properly. How do I fix this data problem?

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



Servo myservo1;
Servo myservo2;
long players, cards;
int pos1 = 0;
int pos2 = 0;
int servo;
int pos = 0;
int deal = 0;

SoftwareSerial BT(2,3);

void setup() {
  Serial.begin(9600);
  BT.begin(9600);
  myservo1.attach(10);
  myservo2.attach(11);
  servo = 90;



}

void loop() {
  if(BT.available()>0) {
    players = BT.read();
    cards = BT.read();

    Serial.println(players);
    Serial.print(cards);

    pos = 180/(players-1);
    for(deal = 0; deal < cards; deal++) {
      for (pos1 = 0; pos1 <= 180 ; pos1 += pos ) { 
          delay(20);
          myservo1.write(pos1);

          for (pos2=0; pos2<=180 ; pos2 +=1){
            myservo2.write(pos2);
            delay(10);
          }

          for (pos2=180; pos2>=0 ; pos2 -=1){
            myservo2.write(pos2);
            delay(15);
          }

    }
      for (pos1 <= 180; pos1 >= 0; pos1 -=1 ) {
          pos2=0;
          myservo1.write(pos1);  
          delay(10);
      }
    }

  }
}

49 is the ASCII character code for '1'. To get the number (0 to 9) from a character code, subtract the decimal 48 or hex 0x30 or '0' from the character code (49 - 48 = '1' or 49 - '0' - '1').

More info about characters in the language reference.

The serial input basics tutorial may have information that will help you.

Please, answer to the following questions under the assumption that the settings of your BT terminal of the phone are: ASCII and LF as terminating character. I have asked these question; because, I see the following codes (in your sketch) which do not appear to be correct. (You are reading a character from BT as it is coming and storing it into variable players; the Serial BUFFer of Arduino becomes empty; you are again reading (in the same cycle) from the empty Serial Buffer (?) and storing it in the variable cards. Are you doing the right things?)

if(BT.available()>0) 
{
    players = BT.read();
    cards = BT.read();

    Serial.println(players);
    Serial.print(cards);

1. What value the variable players will store and what value the variable cards will store when you send this 1-character string (say: 4) from your BT Terminal? (BT sends 0x0A for the LF charcater to Arduino.)

2. What value the variable players will store and what value the variable cards will store when you send this 2-character string (say: 41) from your BT Terminal? (BT sends 0x0A for the LF charcater to Arduino.)

If I put "4,4" in my bluetooth terminal Serial monitor prints "52,52" and if I put "41,41" Serial monitor prints "51,44"

Correct your codes as follows so that when you send 4,4 (ASCII Mode and LF character) from BT, the Serial Monitor shows exactly 4, 4 (Fig-1); when you send 41,41 (ASCII Mode and LF character) from BT, the Serial Monitor shows exactly 41,41 (Fig-1). After that, you proceed to create your sketch to achieve your objectives around the servos.

#include <Servo.h>
#include <SoftwareSerial.h>
Servo myservo1;
Servo myservo2;
char players, cards;
int pos1 = 0;
int pos2 = 0;
int servo;
int pos = 0;
int deal = 0;

SoftwareSerial BT(2, 3);

void setup() 
{
  Serial.begin(9600);
  BT.begin(9600);
  myservo1.attach(10);
  myservo2.attach(11);
  servo = 90;
}

void loop() 
{
  if (BT.available() > 0) 
  {
    players = BT.read();
   // cards = BT.read();

    Serial.print(players);
   // Serial.println(cards);
  //  Serial.println("OK");

    pos = 180 / (players - 1);
    for (deal = 0; deal < cards; deal++) {
      for (pos1 = 0; pos1 <= 180 ; pos1 += pos ) 
      {
        delay(20);
        myservo1.write(pos1);

        for (pos2 = 0; pos2 <= 180 ; pos2 += 1) 
        {
          myservo2.write(pos2);
          delay(10);
        }

        for (pos2 = 180; pos2 >= 0 ; pos2 -= 1) {
          myservo2.write(pos2);
          delay(15);
        }

      }
      for (pos1 <= 180; pos1 >= 0; pos1 -= 1 ) {
        pos2 = 0;
        myservo1.write(pos1);
        delay(10);
      }
    }
  }
}

smbt1.png

smbt1.png