Hello, my project consists of a arduino UNO, motor shield and the HC-05 bluetooth module. My issue is that when I send the HC-05 commands such as right and backward, the wheel rotates perfectly. But forward, left is not working. I have connect 3 motors to adafruit motor driver l293d.
//1st motor
#define motor1a 2
#define motor1b 4
#define pwm1 3
//2nd motor
#define motor2a 7
#define motor2b 6
#define pwm2 5
#define motor3a 8
#define motor3b 10
#define pwm3 9
// 3rd motor
#define motor4a 12
#define motor4b 13
#define pwm4 11
char readString;
void setup() {
Serial.begin(9600);
pinMode(motor1a, OUTPUT);
pinMode(motor1b, OUTPUT);
pinMode(pwm1, OUTPUT);
pinMode(motor2a, OUTPUT);
pinMode(motor2b, OUTPUT);
pinMode(pwm2, OUTPUT);
pinMode(motor3a, OUTPUT);
pinMode(motor3b, OUTPUT);
pinMode(pwm3, OUTPUT);
pinMode(motor4a, OUTPUT);
pinMode(motor4b, OUTPUT);
pinMode(pwm4, OUTPUT);
}
void loop() {
while(Serial.available()) {
delay(100);
int data=Serial.read();
if(data == '1'){
Serial.println("Forward");
forward(255);
delay(50);
}
else if (data =='2'){
Serial.println("Left");
left(255);
delay(50);
}
else if (data =='3'){
Serial.println("Right");
right(255);
delay(50);
}
else if(data =='4'){
Serial.println("Backward");
backward(255);
delay(50);
}
if(readString =='6'){
Serial.println("Rightturn");
rightturn(255);
delay(50);
}
if(readString =='8'){
Serial.println("Leftturn");
leftturn(255);
delay(50);
}
else if(data =='5'){
Serial.println("Stop");
Stop();
delay(50);
}
}
}
void forward(int pwm)
{
digitalWrite(motor1a,LOW);
digitalWrite(motor1b,HIGH);
analogWrite(pwm1,pwm);
digitalWrite(motor2a,HIGH);
digitalWrite(motor2b,LOW);
analogWrite(pwm2,pwm);
digitalWrite(motor4a,LOW);
digitalWrite(motor4b,LOW);
analogWrite(pwm4,0);
}
void backward(int pwm)
{
digitalWrite(motor1a,HIGH);
digitalWrite(motor1b,LOW);
analogWrite(pwm1,pwm);
digitalWrite(motor2a,LOW);
digitalWrite(motor2b,HIGH);
analogWrite(pwm2,pwm);
digitalWrite(motor4a,LOW);
digitalWrite(motor4b,LOW);
analogWrite(pwm4,0);
}
void right(int pwm)
{
digitalWrite(motor1a,LOW);
digitalWrite(motor1b,HIGH);
analogWrite(pwm1,pwm/3.5);
digitalWrite(motor2a,LOW);
digitalWrite(motor2b,HIGH);
analogWrite(pwm2,(pwm*6)/10);
digitalWrite(motor4a,LOW);
digitalWrite(motor4b,HIGH);
analogWrite(pwm4,pwm);
}
void left(int pwm)
{
digitalWrite(motor1a,HIGH);
digitalWrite(motor1b,LOW);
analogWrite(pwm1,(pwm*6)/10);
digitalWrite(motor2a,HIGH);
digitalWrite(motor2b,LOW);
analogWrite(pwm2,pwm/3.5);
digitalWrite(motor4a,HIGH);
digitalWrite(motor4b,LOW);
analogWrite(pwm4,pwm);
}
void leftturn(int pwm)
{
digitalWrite(motor1a,HIGH);
digitalWrite(motor1b,LOW);
analogWrite(pwm1,pwm);
digitalWrite(motor2a,HIGH);
digitalWrite(motor2b,LOW);
analogWrite(pwm2,pwm);
digitalWrite(motor4a,LOW);
digitalWrite(motor4b,HIGH);
analogWrite(pwm4,pwm);
}
void rightturn(int pwm)
{
digitalWrite(motor1a,LOW);
digitalWrite(motor1b,HIGH);
analogWrite(pwm1,pwm);
digitalWrite(motor2a,LOW);
digitalWrite(motor2b,HIGH);
analogWrite(pwm2,pwm);
digitalWrite(motor4a,HIGH);
digitalWrite(motor4b,LOW);
analogWrite(pwm4,pwm);
}
void Stop()
{
digitalWrite(motor3a,LOW);
digitalWrite(motor3b,LOW);
analogWrite(pwm3,0);
digitalWrite(motor4a,LOW);
digitalWrite(motor4b,LOW);
analogWrite(pwm4,0);
digitalWrite(motor1a,LOW);
digitalWrite(motor1b,LOW);
digitalWrite(pwm1,0);
digitalWrite(motor2a,LOW);
digitalWrite(motor2b,LOW);
analogWrite(pwm2,0);
}