How to reduce bluetooth latency

Hello,

I want to use two arduino nanos with one HC-05 Bluetooth modul each for my project, where I have to send data (< 2 bytes) with a latency < 10 ms from one arduino to the other.

I made a little test set up where I connected a push button to one arduino board and an LED to the other board. The connections are very similar to the one in this tutorial, except for the servo motor and potentiometer:

Unfortunately there is a delay of approximately 0.5 seconds from pushing the button to flashing the LED and I wonder where this delay is coming from.

These are my codes:

Master

#define button 3

boolean button_state = 0;


void setup() {
  
pinMode(button,INPUT);
Serial.begin(115200);

}

void loop() {
 

 button_state = digitalRead(button);
 if (button_state == HIGH) {
   Serial.write('1'); // Sends '1' to the master to turn on LED
 }
 else {
   Serial.write('0');
 }  
}

Slave

#define ledPin 3


int state = 0;

void setup() {
  
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(115200);

}

void loop() {

 if(Serial.available() > 0){ 
    state = Serial.read(); 
 }
 
 if (state == '1') {
  digitalWrite(ledPin, HIGH); 
  
 }
 else if (state == '0') {
  digitalWrite(ledPin, LOW); 
  
 }

 
}

Just a hunch but you are sending a continuous stream of 0s when the button isn't pressed so the serial buffer will always be full with data waiting to be sent, so when you press the button the 1s have to queue behind all the 0s in the buffer, introducing a delay.

I stress this is a hunch and given the high serial speed might be wrong.

Hi Perry,

thanks a lot! After adding a little delay in the master loop, the problem is solved!

Thanks!:slight_smile:

Well, I’m glad it works but delay wasn’t what I was thinking of. Delay will cause you problems if you continue to use it, search these fora you will find much said about it.

I wanted you to think about ways to send 0 or 1 only once.

PerryBebbington:
Well, I'm glad it works but delay wasn't what I was thinking of.

This made me laugh. :slight_smile: I hope joo00 comes back with a better solution.

@joo00 This is where learning begins.

Haha, you´re right. I actually know that delays should be avoided. Probably I will use a while loop to get the measurements and use the millis function to send data in a certain interval. This is probably what you mean right?

However, with that solution I still have to make sure that my void loop of my receiving arduino is quicker than the one of my sending arduino. Otherwise the buffer would fill up...
One other solution that I can think of is a Lifo buffer instead of Fifo. So the receiving arduino would always read the most recent value if there´s a queue. Not sure how to implement that though...

Only send data when the button state changes, not continuously.

Ok, but what if it´s not just ones and zeros but measurements, so every value is different from the one before?

At some point you have to think about a protocol. There are some libraries that you can use or write something simple yourself.

Have a look at the NMEA 0183 protocol.

It is used by GPS modules to send data via UART without any configuration. The TinyGPS library can be used to decode the messages. Have a look at the source code for inspiration. You can do something similar but simpler. e.g.

$A1: $ - start symbol, A - button 1, 1 - state changed from 0 to 1
$B2: $ - start symbol, B - button 2, 2 - state changed from 1 to 0

If you need values larger then 0-9 you can add termination character e.g. $A255%.

All right thanks, I´ll check it out!