HC-05 Serial data issue

I am learnig about how to use the HC-05. I’ve wrotten the code and it works recieving the data, but when I hold any button the data changes
(I’ve delcared some buttons as 1 and 3, but if I hold any of them the data appears as -30009)
.

tanque.ino (897 Bytes)

DiegoLeone42:
I am learnig about how to use the HC-05. I've wrotten the code and it works recieving the data, but when I hold any button the data changes
(I've delcared some buttons as 1 and 3, but if I hold any of them the data appears as -30009)
.

What button?

ieee488:
What button?

The buttons are in a bluetooth controller app

DiegoLeone42:
The buttons are in a bluetooth controller app

So we are supposed to guess what the app is and what the buttons are intended to do?

Sorry, my crystal ball is lost in the snow today.

...R
Serial Input Basics - simple reliable ways to receive data.

Robin2:
So we are supposed to guess what the app is and what the buttons are intended to do?

Sorry, my crystal ball is lost in the snow today.

...R
Serial Input Basics - simple reliable ways to receive data.

Robin2:
So we are supposed to guess what the app is and what the buttons are intended to do?

Sorry, my crystal ball is lost in the snow today.

...R
Serial Input Basics - simple reliable ways to receive data.

Well... I am "controlling" motors for a car, so the controller has declared the variables by this way:
-up arrow :1
-Right arrow: 2
-Down arrow: 3
-Left arrow: 4

The problem is when I try to read the data from the Serial1 port. If I hold any button the variable value increases (like 234000) instead of showing the real value
(the program is:

void loop() {
if(Serial.available ()>0){
(Serial.read());

}
if(Serial1.available ()>0){
(Serial1.read());

}

int dir= Serial1.readString().toInt();
Serial.print("dir:");
Serial.println(dir);
//"dir" is for Direction)

DiegoLeone42:
The problem is when I try to read the data from the Serial1 port. If I hold any button the variable value increases (like 234000) instead of showing the real value

 int dir= Serial1.readString().toInt();

Serial.print(“dir:”);
    Serial.println(dir);
//“dir” is for Direction)

I suspect the problem is that your readString() function is treating a succession of values as a single number and if it converts a number bigger than 32767 to an int it will show as a negative value.

Can you get your sending program to send the data like this “<1>” and “<2>” etc. If so, the 3rd example in my link will receive the data reliably.

Even if you can’t change the data format study my link carefully. The first example may work - but it won’t be as reliable.

…R