Bluetooth Controlled Car Response Time Problem

I have made this bluetooth controlled car... I have used HC-05 and L298N motor driver. My problem is when I send a value through bluetooth from my phone to Arduino, It takes much time to respond. Suppose when I send 'f' to arduino , my robot moves forward but after that when I send 's' it takes time to stop. And I can't control my speed. What can I do to solve these problems?
Here is my code-

int MotorA_IN1 = 2;
int MotorA_IN2 = 3;
int MotorB_IN3 = 4;
int MotorB_IN4 = 5;
int MotorA_PWM = 9;
int MotorB_PWM = 10;
int spd = 150;
char sms;
void forward()
{
  analogWrite(MotorA_PWM,spd);
  analogWrite(MotorB_PWM,spd);
  digitalWrite(MotorA_IN1,HIGH);
  digitalWrite(MotorA_IN2,LOW);
  digitalWrite(MotorB_IN3,HIGH);
  digitalWrite(MotorB_IN4,LOW);
}
void pause()
{
  
  digitalWrite(MotorA_IN1,LOW);
  digitalWrite(MotorA_IN2,LOW);
  digitalWrite(MotorB_IN3,LOW);
  digitalWrite(MotorB_IN4,LOW);
}
void backward()
{
  analogWrite(MotorA_PWM,spd);
  analogWrite(MotorB_PWM,spd);
  digitalWrite(MotorA_IN1,LOW);
  digitalWrite(MotorA_IN2,HIGH);
  digitalWrite(MotorB_IN3,LOW);
  digitalWrite(MotorB_IN4,HIGH);
}
void right()
{
  analogWrite(MotorA_PWM,spd);
  analogWrite(MotorB_PWM,spd);
  digitalWrite(MotorA_IN1,LOW);
  digitalWrite(MotorA_IN2,LOW);
  digitalWrite(MotorB_IN3,HIGH);
  digitalWrite(MotorB_IN4,LOW);
}
void left()
{
  analogWrite(MotorA_PWM,spd);
  analogWrite(MotorB_PWM,spd);
  digitalWrite(MotorA_IN1,HIGH);
  digitalWrite(MotorA_IN2,LOW);
  digitalWrite(MotorB_IN3,LOW);
  digitalWrite(MotorB_IN4,LOW);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
pinMode(MotorA_IN1,OUTPUT);
pinMode(MotorA_IN2,OUTPUT);
pinMode(MotorB_IN3,OUTPUT);
pinMode(MotorB_IN4,OUTPUT);
pinMode(MotorA_PWM,OUTPUT);
pinMode(MotorB_PWM,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
if(Serial.available())
{
  sms = Serial.read();
}
if (sms == 's')
{
  pause();
}
if (sms == 'f')
{
  forward();
}
if (sms == 'b')
{
  backward();
}
if (sms == 'r')
{
  right();
}
if (sms == 'l')
{
  left();
}
if(sms == '0')
{
  spd = 0;
}
if(sms == '1')
{
  spd = 100;
}
if(sms == '2')
{
  spd = 200;
}
if(sms == '3')
{
  spd = 255;
}
}

I don'tknow if it will fix your problem but, if you want speed, configuring Bluetooth to run at 115200 baud, or maybe faster, is probably a good idea. Using "case" instead of all those ifs might also help.

At the very least I would use if/else rather than multiple if statements

Is the slow response caused by the Arduino taking a long time to receive the commands or is it the time that it takes to respond to them ? Some Serial.print()s in the code would give you some clues

You will need to sort out if the issue is the response time of the code, or the response time of the car due to momentum. What terminal program are you using to send the commands? I don't really see a major issue in the Arduino code, although as others have said, it could be made somewhat more efficient.

If the problem is momentum and the time taken to coast to a stop, you may need to place the motors in reverse for a bit to help stop the car.

If the problem is the time taken to coast to a halt then you can usually arrange for the motors to be shorted by the motor controller when not running. This causes a back EMF to be induced as the motors run on which opposes that motion so the motors stop quickly

I have used HC-05 and L298N motor driver

It's not clear how much can be achieved with the LN298.

https://forum.arduino.cc/t/l298-fast-stop-and-free-running-stop/229393

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.