hey I made this project and did all the circuits and code accordingly , The bluetooth connects so its not in fault , motors and l298 motor driver also works when given direct power , but when i give command to my car from app. Motors dont move at all.
code :
char t;
void setup() {
pinMode(13,OUTPUT); //left motors forward
pinMode(12,OUTPUT); //left motors reverse
pinMode(11,OUTPUT); //right motors forward
pinMode(10,OUTPUT); //right motors reverse
pinMode(9,OUTPUT); //Led
Serial.begin(9600);
}
void loop() {
if(Serial.available()){
t = Serial.read();
Serial.println(t);
}
if(t == 'F'){ //move forward(all motors rotate in forward direction)
digitalWrite(13,HIGH);
digitalWrite(11,HIGH);
}
else if(t == 'B'){ //move reverse (all motors rotate in reverse direction)
digitalWrite(12,HIGH);
digitalWrite(10,HIGH);
}
else if(t == 'L'){ //turn right (left side motors rotate in forward direction, right side motors doesn't rotate)
digitalWrite(11,HIGH);
}
else if(t == 'R'){ //turn left (right side motors rotate in forward direction, left side motors doesn't rotate)
digitalWrite(13,HIGH);
}
else if(t == 'W'){ //turn led on or off)
digitalWrite(9,HIGH);
}
else if(t == 'w'){
digitalWrite(9,LOW);
}
else if(t == 'S'){ //STOP (all motors stop)
digitalWrite(13,LOW);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
}
delay(100);
}
If you remove the HC05 from the circuit do the motors respond to usb serial input? Make certain that the serial monitor is set for no line endings.
If the input from the Serial monitor works correctly, then the issue is with the app.
What is the app? What are the line ending settings for the send from the app.
The application that i use to send commands is ; Arduino Car (available at playstore) also tried other apps like (RC car controller) but still same problem .
With the code you have written, when give the F command quickly followed by the B command you are winding up with both direction inputs HIGH and the motors are stopped.
In the attached image the motors were set to 'F' before the 'B' for only 70 milliseconds and that might not be enough time to start moving.
/*
Code Name: Arduino Bluetooth Control Car
Code URI: https://circuitbest.com/category/arduino-projects/
Author: Make DIY
Author URI: https://circuitbest.com/author/admin/
Description: This program is used to control a robot using a app
that communicates with Arduino through a bluetooth module.
App URI: https://bit.ly/2BlMAea
Version: 1.0
License: Remixing or Changing this Thing is allowed. Commercial use is not allowed.
*/
#define in1 4 //L298n Motor Driver pins.
#define in2 5
#define in3 6
#define in4 7
#define LED 13
int command; //Int to store app command state.
int Speed = 204; // 0 - 255.
int Speedsec;
int buttonState = 0;
int lastButtonState = 0;
int Turnradius = 0; //Set the radius of a turn, 0 - 255 Note:the robot will malfunction if this is higher than int Speed.
int brakeTime = 45;
int brkonoff = 1; //1 for the electronic braking system, 0 for normal.
void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(LED, OUTPUT); //Set the LED pin.
Serial.begin(9600); //Set the baud rate to your Bluetooth module.
}
void loop() {
if (Serial.available() > 0) {
command = Serial.read();
Stop(); //Initialize with motors stoped.
switch (command) {
case 'F':
forward();
break;
case 'B':
back();
break;
case 'L':
left();
break;
case 'R':
right();
break;
case 'G':
forwardleft();
break;
case 'I':
forwardright();
break;
case 'H':
backleft();
break;
case 'J':
backright();
break;
case '0':
Speed = 100;
break;
case '1':
Speed = 140;
break;
case '2':
Speed = 153;
break;
case '3':
Speed = 165;
break;
case '4':
Speed = 178;
break;
case '5':
Speed = 191;
break;
case '6':
Speed = 204;
break;
case '7':
Speed = 216;
break;
case '8':
Speed = 229;
break;
case '9':
Speed = 242;
break;
case 'q':
Speed = 255;
break;
}
Speedsec = Turnradius;
if (brkonoff == 1) {
brakeOn();
} else {
brakeOff();
}
}
}
void forward() {
analogWrite(in1, Speed);
analogWrite(in3, Speed);
}
void back() {
analogWrite(in2, Speed);
analogWrite(in4, Speed);
}
void left() {
analogWrite(in3, Speed);
analogWrite(in2, Speed);
}
void right() {
analogWrite(in4, Speed);
analogWrite(in1, Speed);
}
void forwardleft() {
analogWrite(in1, Speedsec);
analogWrite(in3, Speed);
}
void forwardright() {
analogWrite(in1, Speed);
analogWrite(in3, Speedsec);
}
void backright() {
analogWrite(in2, Speed);
analogWrite(in4, Speedsec);
}
void backleft() {
analogWrite(in2, Speedsec);
analogWrite(in4, Speed);
}
void Stop() {
analogWrite(in1, 0);
analogWrite(in2, 0);
analogWrite(in3, 0);
analogWrite(in4, 0);
}
void brakeOn() {
//Here's the future use: an electronic braking system!
// read the pushbutton input pin:
buttonState = command;
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == 'S') {
if (lastButtonState != buttonState) {
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, HIGH);
delay(brakeTime);
Stop();
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
}
void brakeOff() {
}
Hey i used this code now and when i give commands now , beep sound comes from the L298 motor driver. What is the reason behind it? if its about power im using my laptop to supply power through arduino, is it not enough.?
Your code development process is not what I recommend.
Why would you leap ahead and write more complex code when you have not gotten the more simple code to work properly?
This is just repeating the error from you previous code in a more complicated manner. Don't you see that you are winding up with both inputs active? Your code needs to turn inputs off as well as on whether using digitalWrite() or analogWrite
Did you actually read the turorial suggested?
if its about power im using my laptop to supply power through arduino, is it not enough.?
Probably not. What are the specification on the current used by the motors?
Yes, I meant to say that the pc power supply was probably not enough.
The power supply issue is independent of your code. If you only send one directional code 'F' and not other commands if the motors do not move then there is most likely a power supply issue.
The code has an issue with the 'F' followed by the 'B' command.
If you read the tutorial you would have seen this logic table
When you write the 'F' code, followed by the 'B' both inputs for a single motor are high.
digitalWrite() is persistant. A pin will stay HIGH until you write it LOW.
You need to be managing both pins of each motor in the 'F' and 'B' conditionals.
Im sorry but can you fix it for me , im just lost.
As your are learning Arduino, you need to understand how digitalWrite() works and how the H bridge operates.
If I fixed the code for you, you would still be lost.
Your talking about how I Sent first B command and then F command in serial Monitor , Both inputs are High so motors don't move. Right? And i should stick to the 1st code rather than the 2nd one cuz 2nd one is pretty complicated.
I shall sent F first and check if motors work , if they do then I will upload it on my Arduino board and check using application through HC05 Bluetooth module.
YESSSSSSSSS////// IT WORKS , mY motors respomd when i give them commands , i changed my battery . and now it works . thanks for all your Help CattleDog. im truly grateful learned many things from u THAMKS