Bluetooth HC-05 Serial Accumulating Data

Hello, I've been trying to use an HC-05 Bluetooth Module to communicate with my arduino to drive 2 stepper motors via phone.

I seem to have run into the problem that my serial stores all the commands I'm sending and flushes it all out at once when I've disconnected my phone. I then changed the datastream to be every button press, but it only reads out at the "Stop" function. When I press other buttons, for example forward, it reads out as "SFS". Im guessing it does send data to my board but only sends the "Stop" function for some reason.

We can't see your code. We can't what is connected to your Arduino. We can't see how things are connected. How are we to help you?

Im guessing it does send data

So write a small sketch that only receives data from Bluetooth to confirm reception.

Please read the "how to use this forum-please read" stickies to see how to format and post code and some hints on what we need to know in order to help you.

Very sorry about that.

This is my code:

//This program is used to control a robot using an app that communicates with Arduino through a bluetooth module.
//Error Code Chart: Code 01; Turnradius is higher than Speed; Code 02; Speed is higher than 255;
#define in1 5 //L298n Motor Driver pins.
#define in2 6
#define in3 10
#define in4 11
#define LED 13
int command; //Int to store app command state.
int Speed = 230; // 0 - 255.
int Speedsec;
int buttonState = 0;
int lastButtonState = 0;
int Turnradius = 0; //Set the radius of a turn, 0 - 255 Note:the robot will malfunction if this is higher than int Speed.
int brakeTime = 45;
int brkonoff = 1; //1 for the electronic braking system, 0 for normal.
void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(LED, OUTPUT); //Set the LED pin.
  Serial.begin(9600);  //Set the baud rate to your Bluetooth module.
}
void loop() {
  if (Serial.available() > 0) {
    command = Serial.read();
    Stop(); //Initialize with motors stoped.
    switch (command) {
      case 'F':
        forward();
        break;
      case 'B':
        back();
        break;
      case 'L':
        left();
        break;
      case 'R':
        right();
        break;
      case 'G':
        forwardleft();
        break;
      case 'I':
        forwardright();
        break;
      case 'H':
        backleft();
        break;
      case 'J':
        backright();
        break;
case '0':
        Speed = 100;
        break;
      case '1':
        Speed = 140;
        break;
      case '2':
        Speed = 153;
        break;
      case '3':
        Speed = 165;
        break;
      case '4':
        Speed = 178;
        break;
      case '5':
        Speed = 191;
        break;
      case '6':
        Speed = 204;
        break;
      case '7':
        Speed = 216;
        break;
      case '8':
        Speed = 229;
        break;
      case '9':
        Speed = 242;
        break;
 case 'q':
        Speed = 255;
        break;
    }
    Speedsec = Turnradius;
    if (brkonoff == 1) {
      brakeOn();
    } else {
      brakeOff();
    }
  }
}
void forward() {
  analogWrite(in1, Speed);
  analogWrite(in3, Speed);
}
void back() {
  analogWrite(in2, Speed);
  analogWrite(in4, Speed);
}
void left() {
  analogWrite(in3, Speed);
  analogWrite(in2, Speed);
}
void right() {
  analogWrite(in4, Speed);
  analogWrite(in1, Speed);
}
void forwardleft() {
  analogWrite(in1, Speedsec);
  analogWrite(in3, Speed);
}
void forwardright() {
  analogWrite(in1, Speed);
  analogWrite(in3, Speedsec);
}
void backright() {
  analogWrite(in2, Speed);
  analogWrite(in4, Speedsec);
}
void backleft() {
  analogWrite(in2, Speedsec);
  analogWrite(in4, Speed);
}
void Stop() {
  analogWrite(in1, 0);
  analogWrite(in2, 0);
  analogWrite(in3, 0);
  analogWrite(in4, 0);
}
void brakeOn() {
  //Here's the future use: an electronic braking system!
  // read the pushbutton input pin:
  buttonState = command;
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == 'S') {
      if (lastButtonState != buttonState) {
        digitalWrite(in1, HIGH);
        digitalWrite(in2, HIGH);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, HIGH);
        delay(brakeTime);
        Stop();
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;
  }
}
void brakeOff() {
}

This is my wiring diagram.

These are what my serial monitor puts out.


For the first image, the monitor doesn't print out the commands in real time, it spits them all out when I disconnect my phone. The second image, I've changed the datastream to be on every button press. When I press, say the forward button (F), the monitor prints out "S", when I press it twice, the monitor outputs "SFS".

I am confused. I see nothing in the code that you posted that prints to serial monitor.

You have the BT module connected to hardware serial (pins 0 and 1). You can't have more than one device connected to a serial port. If you want to use serial monitor to output data and debug information, you will need to connect the BT module to a different port. You can use a software serial library to create the extra port for the BT module.

You might try putting the 9v PP3 back in the smoke detector where it belongs, and using a proper battery.

groundFungus:
You can’t have more than one device connected to a serial port. If you want to use serial monitor to output data and debug information, you will need to connect the BT module to a different port.

Sure you can. In this case, the HC-05 will send characters to the Arduino RX pin 0. They will be received just fine. If he happens to type something into the Serial Monitor window and press Send at the same time, that would collide with what comes out of the HC-05. That character may not be received or it may be corrupted.

And anything he prints to Serial would be sent to both the HC-05 and the Serial Monitor window. If the application on the phone is reading from it’s BT device, it will also receive those debug prints. Usually not an issue, because he could use a command “format” that lets the phone distinguish between commands and debug noise. No big deal.

This is actually a good way to test the application without the HC-05 and phone. He can just type in the commands in the Serial Monitor window and press Send. You do have to disconnect pin 0 to upload a new sketch over USB, though.

Otherwise, the OP’s problem appears to be with the phone application, which we can’t see. And, like groundFungus said, we can’t see the sketch that produced that output.

BTW @w00lv3s281, instead of posting a screen capture , just select the text in the Serial Monitor window and paste it into a code block, just like your code.

Hi, so I've made a simple sketch that would let say what my module got:

 // for incoming serial data
String a;

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                a = Serial.readString();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(a);
        }
}

So I have set my data stream frequency to be on every button change/press, and this is what I get on my serial monitor:

I received: S
I received: S
I received: S
I received: S

I've been pressing the Forward button on my android app and it should send an "F", but instead I get an "S" instead. I've then when I changed the data stream frequency to be every 50ms, I get nothing in real time, then I get this when I disconnect my phone:

I received: SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSFFFFFFFFFFFFFFFFFFSSSSSSSSSSSSSSSSSSSSSSFFFFFFFFFFFFFFFFSSSSSSSSSSSSSSSSSSSSBBBBBBBBBBBBSSSSSSSSBBBBBBBBSSSSSSBBBBSSSSSSBBBBSSSSSSSSBBBBBBSSSSBBBBSSSSSSSSFFFFFFSSSSSSFFFFSSSSSSFFFFSSSSSSSSSSSSSSSSSSSSFFFFFFSSSSSSSSSSFFFFSSSSSSFFFFFFSSSSSSSSSSSSSSSSSSFFFFFFFFSSSSSSSSFFFFFFSSSSFFFFFFFFSSSSRRRRRRRRSSRRRRRRRRSSRRRRRRSSRRRRRRRRSSSSRRRRSSSSSSRRRRSSSSSSSSLLLLSSSSLLLLLLSSLLLLLLSSLLLLLLSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSD