HC-05 Master slave latency

Hi all,

I would be forever grateful if someone could help me with a problem I got. I'm trying to get two hc-05 bluetooth modules to communicate in a master slave setup. After a lot of struggling with different types of modules I've managed to get them connected, but now I got a new problem ...

The Slave seems to only receive data approx every 5 seconds, and that is in large chunks of it. It's as if the master is executing the code and accumulating data that it wants to send and when it eventually does, it overflows the buffer at the receiving end. If I print Serial.avaliable to the monitor (As can be seen in my Slave-code) it'll look something like:

0
0
0
63
62
61 and so on

Right now I've set the Baud rate of the modules to 9600. but I've tried several different ones. Mostly 115200.

The code I'm sending is a little messy. I've commented away parts of it that I intend to use, but right now I just want the master to send the value 2 and for the slave to receive and display it in the serial monitor (got a MEGA as a slave). I've played around with different types of delays in the code, but the 5 sec delay between every received chunk of data persists.

//MASTER

//#define joyX A0
//#define joyY A1
int joyX = 0;
int joyY = 1;
int xValue = 5;
int yValue = 5;

void setup() {

// pinMode(ledPin, OUTPUT);
//digitalWrite(ledPin, LOW);
Serial.begin(9600); // Default communication rate of the Bluetooth module
}
void loop() {
//xValue = analogRead(joyX);
//yValue = analogRead(joyY);

//Serial.print(xValue);

Serial.write(2);

delay(50);

}

//SLAVE

int test;
int test2;
int led = 5;
void setup() {

pinMode (led, OUTPUT);
digitalWrite(led, LOW);

Serial.begin(9600); //Serial Monitor
Serial1.begin(9600); //BT
}
void loop() {
// test = Serial1.read();
// Serial.println(test);
Serial.println(Serial1.available());
if(Serial1.available() > 0){ // Checks whether data is comming from the serial port

test = Serial1.read();

digitalWrite(led, HIGH);

// Serial.println(test);
}
else{
digitalWrite(led, LOW);

}

}

Use an latest version of high priority code to data.

Thanks for the answer but I'm not sure if I follow ... What do you mean?

Please follow the advice given in the link below when posting code . Use code tags when posting code here to make it easier to read and copy for examination

//MASTER

//#define joyX A0
//#define joyY A1
int joyX = 0;
int joyY = 1;
int xValue = 5;
int yValue = 5;

void setup() {

// pinMode(ledPin, OUTPUT);
//digitalWrite(ledPin, LOW);
Serial.begin(9600); // Default communication rate of the Bluetooth module
}
void loop() {
//xValue = analogRead(joyX);
//yValue = analogRead(joyY);

//Serial.print(xValue);

Serial.write(2);

delay(50);

}

//SLAVE

int test;
int test2;
int led = 5;
void setup() {

pinMode (led, OUTPUT);
digitalWrite(led, LOW);

Serial.begin(9600); //Serial Monitor
Serial1.begin(9600); //BT
}
void loop() {
// test = Serial1.read();
// Serial.println(test);
Serial.println(Serial1.available());
if(Serial1.available() > 0){ // Checks whether data is comming from the serial port

test = Serial1.read();

digitalWrite(led, HIGH);
// Serial.println(test);
}
else{
digitalWrite(led, LOW);

}

}```

Right. FIxed it. Ill do this from now on!

Thank you