Hi
about a month ago I have started to work on a project. I want to make a bluetooth controled vehicle. I am using arduino uno and adafruit motor shield. I have connected the two small dc motors on the shield and made a code and everything works perfectly while i communicate with the arduino directly via usb adapter but when I try to do the same via bluetooth in the arduino program when I select the right com port and enter any command it instantly freezes. I tryed using some bluetooth terminal programs and they are able to connect to the hc-05 but i cant send anything to the modul. I have searched the internet and can’t find an explanation. I connected the bluetooth modul correctly 5v pin to 5v on shield gnd to gnd rx on modul to tx on shield and tx on modul to rx on shield. I can find him on my smartphone but when i try to connect to him via application it keeps sendig me errors like unable to connect to the device and I tryed 1234 and 0000 as codes. I did not change anything on the modul or arduino.
Here is the code that i am using :
#include <AFMotor.h>
AF_DCMotor desniMotor ( 1 ); // x - right dc motor
AF_DCMotor lijeviMotor ( 2 ); //y left dc motor
char dataIn = 'S'; //data comming from the phone
char determinant; //used in the check function
char det; //used in the loop function
int velocity = 0;
void setup()
{
Serial.begin(9600); //If you are using MDFLY module use 9600, for Bluetooth Mate use 115200.
}
void loop()
{
det = check();
while (det == 'F') //if incoming data is a F, move foward
{
desniMotor.run (FORWARD);
desniMotor.setSpeed (150);
lijeviMotor.run (FORWARD);
lijeviMotor.setSpeed (150);
det = check();
}
while (det == 'B') //if incoming data is a B, move back
{
desniMotor.run (BACKWARD);
desniMotor.setSpeed (255);
lijeviMotor.run (BACKWARD);
lijeviMotor.setSpeed (255);
det = check();
}
while (det == 'L') //if incoming data is a L, move wheels left
{
desniMotor.run (FORWARD);
desniMotor.setSpeed (255);
lijeviMotor.run (BACKWARD);
lijeviMotor.setSpeed (255);
det = check();
}
while (det == 'R') //if incoming data is a R, move wheels right
{
desniMotor.run (BACKWARD);
desniMotor.setSpeed (255);
lijeviMotor.run (FORWARD);
lijeviMotor.setSpeed (255);
det = check();
}
while (det == 'I') //if incoming data is a I, turn right forward
{
desniMotor.run (FORWARD);
desniMotor.setSpeed (215);
lijeviMotor.run (FORWARD);
lijeviMotor.setSpeed (255);
det = check();
}
while (det == 'J') //if incoming data is a J, turn right back
{
desniMotor.run (BACKWARD);
desniMotor.setSpeed (215);
lijeviMotor.run (BACKWARD);
lijeviMotor.setSpeed (255);
det = check();
}
while (det == 'G') //if incoming data is a G, turn left forward
{
desniMotor.run (FORWARD);
desniMotor.setSpeed (255);
lijeviMotor.run (FORWARD);
lijeviMotor.setSpeed (215);
det = check();
}
while (det == 'H') //if incoming data is a H, turn left back
{
desniMotor.run (BACKWARD);
desniMotor.setSpeed (255);
lijeviMotor.run (BACKWARD);
lijeviMotor.setSpeed (215);
det = check();
}
while (det == 'S') //if incoming data is a S, stop
{
desniMotor.run (RELEASE);
lijeviMotor.run (RELEASE);
det = check();
}
}
int check()
{
if (Serial.available() > 0) //Check for data on the serial lines.
{
dataIn = Serial.read(); //Get the character sent by the phone and store it in 'dataIn'.
if (dataIn == 'F')
{
determinant = 'F';
}
else if (dataIn == 'B')
{
determinant = 'B';
}
else if (dataIn == 'L')
{
determinant = 'L';
}
else if (dataIn == 'R')
{
determinant = 'R';
}
else if (dataIn == 'I')
{
determinant = 'I';
}
else if (dataIn == 'J')
{
determinant = 'J';
}
else if (dataIn == 'G')
{
determinant = 'G';
}
else if (dataIn == 'H')
{
determinant = 'H';
}
else if (dataIn == 'S')
{
determinant = 'S';
}
else if (dataIn == '0')
{
velocity = 0;
}
else if (dataIn == '1')
{
velocity = 25;
}
else if (dataIn == '2')
{
velocity = 50;
}
else if (dataIn == '3')
{
velocity = 75;
}
else if (dataIn == '4')
{
velocity = 100;
}
else if (dataIn == '5')
{
velocity = 125;
}
else if (dataIn == '6')
{
velocity = 150;
}
else if (dataIn == '7')
{
velocity = 175;
}
else if (dataIn == '8')
{
velocity = 200;
}
else if (dataIn == '9')
{
velocity = 225;
}
else if (dataIn == 'q')
{
velocity = 255;
}
}
return determinant;
}