0
votes

I'm making an RC car for my school project, and this is my first time writing codes.

I'm using Arduino Uno, L293D motor shield, and HC-05 for this project.

I was following the guide from https://blog.miguelgrinberg.com/post/building-an-arduino-robot-part-ii-programming-the-arduino, and I couldn't properly communicate with the Arduino through bluetooth connection.

Everything works fine with the sketch below:

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(6,7);

void setup()
{
   BTSerial.begin(9600);
}

void loop()
{
   if (BTSerial.available() > 0) {
       char ch = BTSerial.read();
       BTSerial.print("Received: ");
       BTSerial.println(ch);
       if (ch == 'a') {
           digitalWrite(9, HIGH);
       }
       else {
           digitalWrite(9, LOW);
       }
   }
}

I can turn the LED on and off with my android phone with this sketch, but if I include definitions for motors in the sketch, the arduino continuously sends println messages to my phone and it does not respond to my command.

#include <SoftwareSerial.h>
#include <AFMotor.h>

SoftwareSerial BTSerial(6,7);
AF_DCMotor Motor1(1);
AF_DCMotor Motor2(2);
AF_DCMotor Motor3(3);
AF_DCMotor Motor4(4);

void setup()
{
   BTSerial.begin(9600);
}

void loop()
{
   if (BTSerial.available() > 0) {
       char ch = BTSerial.read();
       BTSerial.print("Received: ");
       BTSerial.println(ch);
       if (ch == 'C') {
           Motor1.setSpeed(255);
           Motor1.run(BACKWARD);
           Motor2.setSpeed(255);
           Motor2.run(BACKWARD);
           Motor3.setSpeed(255);
           Motor3.run(BACKWARD);
           Motor4.setSpeed(255);
           Motor4.run(BACKWARD);
       }
       if (ch == 'D') {
           Motor1.setSpeed(0);
           Motor1.run(BRAKE);
           Motor2.setSpeed(0);
           Motor2.run(BRAKE);
           Motor3.setSpeed(0);
           Motor3.run(BRAKE);
           Motor4.setSpeed(0);
           Motor4.run(BRAKE);
       if (ch == 'E') {
           digitalWrite(9, HIGH);
       }
       if (ch == 'F') {
           digitalWrite(9, LOW);
       }
       }
   }
}

Why is this happening and how do I fix it? Please help.

(For attachments) I did not connect to the motors; they work fine with serial connection to my laptop with modified sketch, but apparently it does not work with bluetooth commands.enter image description here

1

1 Answers

0
votes
#include <SoftwareSerial.h>
#include <AFMotor.h>

SoftwareSerial BTSerial(6,7);
AF_DCMotor Motor1(1);
AF_DCMotor Motor2(2);
AF_DCMotor Motor3(3);
AF_DCMotor Motor4(4);

void setup()
{
   BTSerial.begin(9600);
}

void loop()
{
   while (BTSerial.available()) {
       delay(10)  //delay for giving time to the read buffer
       char ch = BTSerial.read();
       BTSerial.print("Received: ");
       BTSerial.println(ch);
       switch(ch){
       case 'C':
                Motor1.setSpeed(255);
                Motor1.run(BACKWARD);
                Motor2.setSpeed(255);
                Motor2.run(BACKWARD);
                Motor3.setSpeed(255);
                Motor3.run(BACKWARD);
                Motor4.setSpeed(255);
                Motor4.run(BACKWARD);
                break;
       case 'D':
                Motor1.setSpeed(0);
                Motor1.run(BRAKE);
                Motor2.setSpeed(0);
                Motor2.run(BRAKE);
                Motor3.setSpeed(0);
                Motor3.run(BRAKE);
                Motor4.setSpeed(0);
                Motor4.run(BRAKE);
                break;
       case 'E':
               digitalWrite(9, HIGH);
               break;
       case 'F':
               digitalWrite(9, LOW);
               break;
     }
     break;
  }
}

Try this and tell is this working accordingly or not.

Also, you can use an android app available on playstore for controlling. I think you will like this app got really great feature for making different projects.

https://play.google.com/store/apps/details?id=invootech.ashish.anduino_connect