Notifications
Clear all

Bluetooth controlled Robot using Arduino


Nahian_Mugdho
(@nahian_mugdho)
New Member
Joined: 5 months ago
Posts: 2
Topic starter  

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 motor speed with the following code . What is the problem and 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;
}
}





Quote
Will
 Will
(@will)
Reputable Member
Joined: 3 months ago
Posts: 368
 

Can you please show us your wiring diagram or at least denote which Arduino pins are connected to which L298N pins.

What is the difference between 's' (pause) and '0' (set speed to zero) ? Is it faster using '0' than 's' ?

As an aside, you could make your code much smaller and easier to understand by putting the commands to set the L298N into a subroutine and then calling it from your loop's if statement.

For example

 

void setup() {
    // ... as supplied
}

void loop() {
    // ...
    if (sms=='l')
        setL298N(spd,HIGH,LOW,LOW,LOW);
    if (sms=='r')
        setL298N(spd,LOW,LOW,HIGH,LOW);
    // ...
}

void setL298N(int spd, bool MA1, bool MA2, bool MB1, bool MB2) {
    analogWrite(MotorA_PWM,spd);
    analogWrite(MotorB_PWM,spd);
    digitalWrite(MotorA_IN1,MA1);
    digitalWrite(MotorA_IN2,MA2);
    digitalWrite(MotorB_IN3,MB1);
    digitalWrite(MotorB_IN4,MB2);
}

This would eliminate all of the subroutines from forward to left.

 


ReplyQuote
MadMisha
(@madmisha)
Reputable Member
Joined: 2 years ago
Posts: 319
 
Posted by: @nahian_mugdho

 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 motor speed with the following code . What is the problem and What can I do to solve these problems?
Here is my code- 

It might be more efficient to use else if statements nested in the if serial statement. You are running through and testing everything every time. That way you spend most of the time listening to serial because now you are just rerunning the function of the last thing sent over and over again.

 

So it would be:

 

void loop() {
  if(Serial.available()) {
    sms = Serial.read();
    
    if (sms == 's') {
      pause();
    } else if (sms == 'f') {
      forward();
    } else if (sms == 'b') {
      backward();
    } else if (sms == 'r') {
      right();
    } else if (sms == 'l') {
      left();
    } else if (sms == '0') {
      spd = 0;
    } else if (sms == '1') {
      spd = 100;
    } else if (sms == '2') {
      spd = 200;
    } else if (sms == '3') {
      spd = 255;
    }
  }//if(Serial.available())
}//Loop()

Also, as a note, the IDE will ignore spaces and tabbing so use them to your advantage to make your code easily readable. Adding comments when closing a bracket helps too. It's a pain when you have to find where you are missing one. The larger it is, the harder it is to fix. Keeping the opening bracket on the first line also helps.

 

As for the issue of changing speed, if you made these changes, you are changing the variable but you are not running a function to write it. You might need another variable that logs what command you are running. So when you send the speed it updates the speed and if forward was the current direction, run that function.

You could make an int and decide that 0 is pause, 1 is back, 2 forward, 3 left and 4 right. Set that int in your if statements. Make a switch case or if else statements to run the appropriate function after you set speed.

 

Make sure your speed pins that you are controlling with PWM are connected to the ENA and ENB pins(No jumper).


ReplyQuote