hc-05 weird delay

Hello!
I made an arduino based remote for my electric skateboard. I am using two hc-05 for communication between receiver and transmitter. The reason to have an arduino on each end is to add additional features later such as controlling lights. I used simple rf transmitters and receivers in the past, but it seems to get interference at certain places outside. The hc-05 has problems as well. I am getting a few second delay after a while, which is fixed by resetting the arduino. I think it’s something related to losing connection and building some kind of buffer, but i’m not sure. Anyway, if anybody has experienced this before, I would love some advice.

Transmitter

#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX

int pot=A0;
int val=0;

 
void setup() 
{
   
    BTserial.begin(9600);  
}
 
void loop()
{
 val=map(analogRead(pot),0,1048,0,255);
    
 
 BTserial.write(val);  
 delay(80);
}

Receiver

#include <Servo.h>


int val = 0;

Servo esc;
int throttle = 0;
int i = 0;
void setup()
{
  Serial.begin(9600);
  esc.attach(5);
}

void loop()
{
  if (Serial.available())
  {
    i = 0;
    val = Serial.read();
    throttle = map(val, 26, 105, 1000, 2000);
    
  }
  else {
    i += 1;

  }
  if (i > 3) { //failsafe
    throttle = 1000;
  }
  if (throttle < 1000) {
    throttle = 1000;
  }
  if (throttle > 2000) {
    throttle = 2000;
  }
  esc.write(throttle);
  delay(80);
}

Take the delay(80) out of the receiver code. You only need it in the Tx program.

...R

Robin2:
Take the delay(80) out of the receiver code. You only need it in the Tx program.

...R

Thanks for the reply! I will try it, do you think that delay would be able to cause a one or two second delay?

timmie:
Thanks for the reply! I will try it, do you think that delay would be able to cause a one or two second delay?

Who knows?

Think about it like this. Imagine you have large box of tennis balls to throw to your partner. You and your partner also have people to keep time. Normally you have your eyes closed. After 20 seconds your timekeeper touches you on the shoulder and you open your eyes, thow a ball to your partner and close your eyes again.

Now think about the partner. Suppose he also has his eyes closed and only opens them when his timekeeper touches him on the shoulder when 20 seconds has elapsed. What are the chances of the partner catching the ball that you threw? Nil? Obviously the partner needs to have his eyes open all the time.

Same with code that is receiving data.

...R

val=map(analogRead(pot),0,1048,0,255);

should actually be:

val=map(analogRead(pot),0,1023,0,255);