Quick question on my hc05 code

hey so ive been using these two codes on different ardunio's that are connected using the hc05 Bluetooth module

everything works fine but for some reason there is like a 5 second delay between a button push on one and it registering on the other.

is this a code issue or is this a issue with the processing speed of either the ardunio or hc05 ?

const int pinButton = 4;

void setup() {
  pinMode(pinButton, INPUT);
  Serial.begin(9600);
}

void loop() {
  int stateButton = digitalRead(pinButton);
  Serial.println(stateButton);
  delay(20);
}
char INBYTE;
 int  LED = 13; // LED on pin 13

 void setup() {
   Serial.begin(9600); 
   pinMode(LED, OUTPUT);

 }

 void loop() {
  Serial.println("Press 1 to turn Arduino pin 13 LED ON or 0 to turn it OFF:");
  while (!Serial.available());   // stay here so long as COM port is empty   
  INBYTE = Serial.read();        // read next available byte
  if( INBYTE == '0' ) digitalWrite(LED, LOW);  // if it's a 0 (zero) tun LED off
  if( INBYTE == '1' ) digitalWrite(LED, HIGH); // if it's a 1 (one) turn LED on
  delay(50);
}

If you let your sending code run for a few seconds it will fill the 64 byte serial buffer on the receiver. Then the receiver processes what it has received one character every 50ms because of your delay. So it will take ~3200ms before the receiver notices a button press.

Alter your sender so that it only sends anything when the button state changes.

In order to come up to sound conclusion, I would like to recommend you to operate your serial communication between HC05s using Software UART Ports and leave the Hardware UART Ports be engaged with the Serial Monitors of the Sender-UNO and Receiver-NANO. Do something like:

wildbill:
If you let your sending code run for a few seconds it will fill the 64 byte serial buffer on the receiver. Then the receiver processes what it has received one character every 50ms because of your delay. So it will take ~3200ms before the receiver notices a button press.

Alter your sender so that it only sends anything when the button state changes.

Sender (UNO) sends a code byte (the very first one), and it reaches to Receiver (NANO); the data byte immediately enters into FIFO. The NANO is reading out from the FIFO (the FIFO is empty now) and does the processing. From where is coming the 5 s delay to register the incoming code?

The UNO sends the next code after 20 ms. It reaches to NANO and enters into FIFO. Now, FIFO has only one data item. The NANO is still in the 50 ms time delay loop; so, NANO is not reading out the data byte from FIFO.

In the meantime, another code byte has reached to NANO from UNO. It has entered into FIFO. Now, there are 2 data items in the FIFO.

The UNO will need another 20 ms to send the next code byte. In the meantime, the NANO has come out 50 ms time delay loop and has read out a data item from the FIFO.

So, in the worst-case situation, the delay could be at beat 50 ms. Why is it be 5 s or 3.2 s?

The sender is sending three characters every 20ms (println). So one every ~7ms on average. The receiver is processing one every 50ms. So they're being sent ~seven times faster than they're being consumed.

It is inevitable then that the receiver's buffer will be filled up and when it is, any button press that causes a '1' to be sent will not be processed until the 64 characters ahead of it are gone. Not to mention that when the buffer is full, that '1' will be thrown away.

so the up shot is I need it to only send the 1 or 0 when the button starts being pushed or stops being pushed ?

munch15a:
so the up shot is I need it to only send the 1 or 0 when the button starts being pushed or stops being pushed ?

That's it.