0

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);
 }
 }
 }
}
asked Mar 29, 2020 at 14:16

1 Answer 1

1

You are mixing ASCII characters and integer values.

The serial data is a pure binary stream, a series of bytes. It is up to the programs on both sides to interpret, how these bytes are meant. When you type 1 into your bluetooth serial app, it already does this. It complies to the ASCII standard, which gives a meaning to every binary value between 0 and 127. Google "ASCII" to get a table of the mapping. Each character (alpha numeric and some special characters and control characters) has a binary value associated with it. That way we can interpret a binary data stream as text.

The ASCII character 1 corresponds to the decimal value 49. You are saving the read byte from the bluetooth serials as long, which is a big integer. So, when you are printing it, you will see the decimal value, which is 49.

What to do now? The easiest way is to subtract the ASCII value of '1' from the received byte:

players = BT.read() - '0';

'0' is the C/C++ representation of a single ASCII encoded character. It's the same as subtracting the decimal value 48, but it's a bit clearer to see, what happens here. We are shifting the numbers row (0 to 9) from position 48 to position 0. That way the ASCII '0' is transformed to a decimal 0, the ASCII '1' to a decimal 1, and so on up to 9.

Note: You might want to make sure, that only numbers are used for these variables, as the principle will result in garbled data, when the user inputs some other characters. You would need to read the binary data into a temporary variable (here called c) and then check, if they are numeric. Either by hand:

if(c >= '0' && c <= '9')

or by a convinient function

if(isDigit(c))
answered Mar 29, 2020 at 14:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.