HC-05 to control car has delays and disconnection

I made some simple code and car using 1 arduino, 1 hc-05, 2 L298N, and 4 batteries. I control them with the app Bluetooth RC controller on android. When I send a command on the app, the car sometimes has some delays, varying 4-8 seconds, sometimes it disconnects completely. Here is the code. I don't think the code has any problem and my friend brought up that the problem might be the distortion from a magnetic field because I use some lead to weld some wires

char t; 
int fr1= 2;
int fr2=3;
int fl1=12;
int fl2=13;
int br1=4;
int br2=5;
int bl1 = 7;
int bl2=8;
int Mfr=6;//speed setting
int Mfl=9;
int Mbr=10;
int Mbl=11;
float Speed;

void setup() {
pinMode(fr1,OUTPUT);      //fr1
pinMode(fr2,OUTPUT);      //fr2
pinMode(fl1,OUTPUT);      //fl1
pinMode(fl2,OUTPUT);      //fl2
pinMode(br1,OUTPUT);      //br1
pinMode(br2,OUTPUT);     //br2
pinMode(bl1,OUTPUT);     //bl1
pinMode(bl2,OUTPUT);     //bl2


Serial.begin(9600); 
 
}
 
void loop() {


  
if(Serial.available()){
  t = Serial.read();
  Serial.println(t);
}
 
if(t == 'F'){          
    digitalWrite(fr1, LOW);
    digitalWrite(fr2, HIGH);
    digitalWrite(fl1, LOW);
    digitalWrite(fl2, HIGH);
    digitalWrite(br1, LOW);
    digitalWrite(br2, HIGH);  
    digitalWrite(bl1, LOW);
    digitalWrite(bl2, HIGH);
}
 
else if(t == 'B'){      
    digitalWrite(fr1, HIGH);
    digitalWrite(fr2, LOW);
    digitalWrite(fl1, HIGH);
    digitalWrite(fl2, LOW);
    digitalWrite(br1, HIGH);
    digitalWrite(br2, LOW);  
    digitalWrite(bl1, HIGH); 
    digitalWrite(bl2, LOW);
}
 
else if(t == 'L'){     
    digitalWrite(fr1, LOW);
    digitalWrite(fr2, HIGH);
    digitalWrite(fl1, HIGH);
    digitalWrite(fl2, LOW);
    digitalWrite(br1, HIGH);
    digitalWrite(br2, LOW);
    digitalWrite(bl1, LOW);
    digitalWrite(bl2, HIGH);
}
 
else if(t == 'R'){      
    digitalWrite(fr1, HIGH);
    digitalWrite(fr2, LOW);
    digitalWrite(fl1, LOW);
    digitalWrite(fl2, HIGH);
    digitalWrite(br1, LOW);
    digitalWrite(br2, HIGH);
    digitalWrite(bl1, HIGH);
    digitalWrite(bl2, LOW);
}

else if(t == 'S'){      //STOP (all motors stop)
    digitalWrite(fr1, LOW);
    digitalWrite(fr2, LOW);
    digitalWrite(fl1, LOW);
    digitalWrite(fl2, LOW);
    digitalWrite(br1, LOW);
    digitalWrite(br2, LOW);
    digitalWrite(bl1, LOW);
    digitalWrite(bl2, LOW);
}

}

Upvotes: 0

Views: 461

Answers (2)

Bavenraj
Bavenraj

Reputation: 1

I did a similar project to yours but using 1 Arduino, 1 hc-05, 1 L298N, 2 18650 batteries and MIT App Inventor. Here is how I think you can solve this issue. Try adding in delay in these three parts.

  1. When starting your serial.
Serial.begin(9600);
delay(500);
  1. When reading the serial input.
if(Serial.available()){
t = Serial.read();
delay(10);
}
  1. When ending the loop().
}
delay(100);
}

If any of these don't work, you can refer to my GitHub account for this project: Github Link for Arduino Project

Hope it helps 😊

Upvotes: 0

RemyHx
RemyHx

Reputation: 365

Is it possible its gotten flooded by bytes, serial.read had to read? I imagine maybe the app is sending a lot of characters to the Arduino, is it possible to check/apply a (short) delay in that app?

Upvotes: 0

Related Questions