The car is not moving forward

I am currently making a voice control + obstacle avoidance car using an Arduino Uno, motor shield l293d, 4 DC motors and 2 IR sensor. The car should take command forward, backward, turn left, turn right, and stop. The car should stop if the front and back IR sensor sense an obstacle. However my car cannot moving forward.

This is the latest version of my source code:

#include <AFMotor.h>
#define IR_front A4
#define IR_back A5

String voice;
int detection_front = HIGH;
int detection_back = HIGH;

AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);


void setup() {
  Serial.begin(9600);
  pinMode(IR_front, INPUT);
  pinMode(IR_back, INPUT);
}

void loop() {
  detection_front = digitalRead(IR_front); 
  detection_back = digitalRead(IR_back);

  if(Serial.available() > 0) {
    voice="";
    delay(2);
    voice = Serial.readString();
    delay(2);
    Serial.println(voice);

    if(voice == "turn left"){
      motor1.run(BACKWARD);
      motor1.setSpeed(255);
      motor2.run(BACKWARD);
      motor2.setSpeed(255);
      motor3.run(FORWARD);
      motor3.setSpeed(255);
      motor4.run(FORWARD);
      motor4.setSpeed(255);
      delay(700);
      motor1.run(RELEASE);
      motor2.run(RELEASE);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
    else if(voice == "turn right"){
      motor1.run(FORWARD);
      motor1.setSpeed(255);
      motor2.run(FORWARD);
      motor2.setSpeed(255);
      motor3.run(BACKWARD);
      motor3.setSpeed(255);
      motor4.run(BACKWARD);
      motor4.setSpeed(255);
      delay(700);
      motor1.run(RELEASE);
      motor2.run(RELEASE);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
    else if(voice == "move forward"){
      if(detection_front == LOW) {
        motor1.run(RELEASE);
        motor2.run(RELEASE);
        motor3.run(RELEASE);
        motor4.run(RELEASE);
        voice="";
      }else {
        motor1.setSpeed(255); 
        motor1.run(FORWARD); 
        motor2.setSpeed(255); 
        motor2.run(FORWARD); 
        motor3.setSpeed(255); 
        motor3.run(FORWARD);
        motor4.setSpeed(255); 
        motor4.run(FORWARD); 
      }
    }
    else if(voice == "move backward"){
      if(detection_back == LOW) {
        motor1.run(RELEASE);
        motor2.run(RELEASE);
        motor3.run(RELEASE);
        motor4.run(RELEASE);
        voice="";
      }else {
        motor1.setSpeed(255); 
        motor1.run(BACKWARD); 
        motor2.setSpeed(255); 
        motor2.run(BACKWARD); 
        motor3.setSpeed(255); 
        motor3.run(BACKWARD);
        motor4.setSpeed(255); 
        motor4.run(BACKWARD); 
      }
    }
    else if(voice == "stop"){
      motor1.run(RELEASE);
      motor2.run(RELEASE);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
  }
}

Could anyone please help me solving this issue?

Welcome to the forum
what have you got the Line Ending set to in the Serial monitor ? It should be set to No Line Ending

Does the motor work if you send the required command to it regardless of user input ?

How is the motor powered ?

i'm building a voice control car with bluetooth module.

the car will move backward if i say "move backward" and stop if it sense an obstacle. other than "move backward" command, the motor will not working. same goes for the other commands. the motor should only work if the input are move backward, move forward, turn left, turn right and stop

i powered the motor with two 18650 battery cell 3.7v

Which Arduino board are you using ?
Are you using an external Bluetooth module ?
Are the messages being received ?
What do you see if you print the voice String like this

Serial.print(">");
Serial.print(voice);
Serial.println("<");

Is everything printed on one line ?

What do you mean by writing "same goes for the other commands."

You should explicitly write
move forward: what happends? Whatever it is write it
move forward: what happends? Whatever it is write it
turn left: what happends? Whatever it is write it
turn right: what happends? Whatever it is write it
stop: what happends? Whatever it is write it

What does you car do if you run a test-code that does simply

        motor1.setSpeed(255); 
        motor1.run(FORWARD); 
        motor2.setSpeed(255); 
        motor2.run(FORWARD); 
        motor3.setSpeed(255); 
        motor3.run(FORWARD);
        motor4.setSpeed(255); 
        motor4.run(FORWARD); 

Hello plantcosmos88

Welcome to the world's best Arduino forum ever.

Post the current connection diagram of your project.

add Serial.println() s to each case to see if its recognizing the command, as well as indicating other conditions such as detection_front

These "IR Sensors" tend to fail, by being stuck HIGH or LOW and the sensitivity potentiometer only allows changing them HIGH or LOW. Try other IR Sensors or use a different proximity sensor.

@plantcosmos88 these three advisors are all saying things around the idea that you should break the project into small pieces, and get each piece working as close to perfectly as possible before then combining them.

You appear to already be taking that approach if I read your code and prose right - for now you are using serial input, but one day that will be coming in over some kind of speech recognizing mechanism.

Take that further. Test the sensors doing nothing but sensing and reporting. Always educational. You need to get acquainted with how (and if) they work.

Use pushbuttons instead of sensors when you test the motors or whatever.

Print out what you serial read, again you may see instantly that you aren't getting what you thought.

Simplify the serial input development side track, just use single characters as commands. Doing woukd mean you could think about more elaborate communications later, or in fact since that was all "middle man" stuff to begin with, never ever bother getting and testing "move left" as such.

It'd be a lot less typing also.

Divide and conquer.

Yes, please.

a7

1 Like

thanks, i will come back to you guys after running the test

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.