Arduino code for controlling 4wd motors

Dear all,

i have bought the 4wd obstacle avoidance car, but it came without any manual or instructions or any code.

i have assembled the car with the correct connections using this diagram.

i want this car to be controlled via bluetooth, so i disabled the ultrasonic sensor and got the HS-06 Bluetooth module and connected it to the Arduino sensor shield on COM PORT and successfully connected to my android device and all working fine.

i have logically combined some pieces of code i found online (FIND BELOW) and installed the BLUETOOTH RC CONTROL app, checked the code and uploaded to my Arduino.

whenever i insert the batteries, 2 motors run immediately and 2 other do not move.

i am trying to analyze the code but it's taking a lot of time, if there is any guide to understand the commands that work with every interface it would be great, but also i need help with repairing this code in order to make my car work and extend it's abilities more.

#include <AFMotor.h>

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

char command;

void setup() 
{       
 Serial.begin(9600);  //Set the baudrate for Bluetooth module.
}

void loop(){
 if(Serial.available() > 0){ 
   command = Serial.read(); 
   Stop(); //initialize with motors stoped
   
   //Serial.println(command);
   switch(command){
   case 'F':  
     forward();
     break;
   case 'B':  
      back();
     break;
   case 'L':  
     left();
     break;
   case 'R':
     right();
     break;
   }
 } 
}

void forward()
{
 motor1.setSpeed(255); //Define maximum velocity
 motor1.run(FORWARD); //rotate the motor clockwise
 motor2.setSpeed(255); //Define maximum velocity
 motor2.run(FORWARD); //rotate the motor clockwise
 
 motor3.setSpeed(255); //Define maximum velocity
 motor3.run(FORWARD); //rotate the motor clockwise
 motor4.setSpeed(255); //Define maximum velocity
 motor4.run(FORWARD); //rotate the motor clockwise
}

void back()
{
 motor1.setSpeed(255); 
 motor1.run(BACKWARD); //rotate the motor counterclockwise
 motor2.setSpeed(255); 
 motor2.run(BACKWARD); //rotate the motor counterclockwise

 motor3.setSpeed(255); 
 motor3.run(BACKWARD); //rotate the motor counterclockwise
 motor4.setSpeed(255); 
 motor4.run(BACKWARD); //rotate the motor counterclockwise
}

void left()
{
 motor1.setSpeed(255); //Define maximum velocity
 motor1.run(FORWARD); //rotate the motor clockwise
 motor2.setSpeed(255); //Define maximum velocity
 motor2.run(BACKWARD); //rotate the motor counterclockwise

 motor3.setSpeed(255); //Define maximum velocity
 motor3.run(FORWARD); //rotate the motor clockwise
 motor4.setSpeed(255); //Define maximum velocity
 motor4.run(BACKWARD); //rotate the motor counterclockwise
}

void right()
{
 motor1.setSpeed(255); //Define maximum velocity
 motor1.run(BACKWARD); //rotate the motor counterclockwise
 motor2.setSpeed(255); //Define maximum velocity
 motor2.run(FORWARD); //rotate the motor clockwise

 motor3.setSpeed(255); //Define maximum velocity
 motor3.run(BACKWARD); //turn motor1 off
 motor4.setSpeed(255); //Define maximum velocity
 motor4.run(FORWARD); //rotate the motor clockwise
}

void Stop()
{
 motor1.setSpeed(0);
 motor2.run(RELEASE); //turn motor1 off
 motor2.setSpeed(0);
 motor2.run(RELEASE); //turn motor2 off

 motor3.setSpeed(0);
 motor3.run(RELEASE); //turn motor3 off
 motor4.setSpeed(0);
 motor4.run(RELEASE); //turn motor4 off
}

i have ordered it from china through aliepress.com ... i'm really not sure about the correct library that initializes the motor module pins. i have the L298N motor driver board.

1 Like

Where did you buy it from? Your code is using the Adafruit motor shield, but your kit may not actually have that shield in it.

Please edit your original post to add [ code ] tags.

i have also found this code and trying to make it work, i have modified the pins values as they are exactly connected, but the order to make them move i what i need.

/*##### Motor Shield (L298N) #####*/
/*
* Arduino Pin --> L298N
* 1 --> ENA
* 6 --> ENB
* 2 --> IN1
* 3 --> IN2
* 4 --> IN3
* 5 --> IN4
*/
const int ENA = 1;
const int ENB = 6; 
/*
* IN1: HIGH; IN2: LOW --> Direction 1 
* IN1: LOW; IN2: HIGH --> Direction 2
* IN3: HIGH; IN4: LOW --> Direction 1
* IN3: LOW; IN4: HIGH --> Direction 2
*/
const int IN1 = 2; 
const int IN2 = 3;
const int IN3 = 4;
const int IN4 = 6;
char command;
void setup()
{
 pinMode(ENA, OUTPUT);
 pinMode(ENB, OUTPUT);
 pinMode(IN1, OUTPUT);
 pinMode(IN2, OUTPUT);
 pinMode(IN3, OUTPUT);
 pinMode(IN4, OUTPUT);
 
 // Enable Motor A, Motor B: Constant Speed
 digitalWrite(ENA, HIGH);
 digitalWrite(ENB, HIGH);
 // Serial communication
 Serial.begin(9600);
}
 
void loop(){
  if(Serial.available() > 0){ 
    command = Serial.read();

If you just told us which one you have, it would make this easier.

which one what? man i prefer reading than answering a 24 hours some words question ... thanks and close the thread man. all what i need is to make this damn car move and don't want to launch a nuclear missile using ardiuno ...

It's your car, sitting in front of you. We can't see it. If you tell us exactly which hardware you have, we can help you.

Dear, i have bought this item from this link UNO Robot Car Kit and includes the following items listed below:

Bluetooth multi-function car kit including:
4pcs gear motor.
4pcs high quality tyre
4pcs motor fixed pieces
2pcs 100 * 213 * 5 mm plate
1pcs L298N motor driver board
1pcs uno r3
1pcs sensor extension board V5
1pcs holder
1pcs SG90 servo
1pcs ultrasonic sensor module
3pcs TCRT5000 tracing module
1pcs infrared receiving sensor module
1pcs remote
1pcs 18650 battery box
1pcs 18650 charger
1pcs bluetooth module
1pcs 40pin dupont line
1pcs USB cable
1pcs screw kit

also the image of the items included is attached ... as i mentioned in my first post, it did not include any instructions or any code or any documents.

thanks for your reply.

That's unfortunate, but I think we can work it out.

Here's the picture inline (There's an guide on this forum that shows you how to get attachments to show inline.)

The L298N motor driver is at the top-right. The red board. That shows two motors: Motor A and Motor B. So to make it drive 4 motors, you have to attach the motors in pairs. Put both the left motors on "A" and both the right motors on "B". When you get some test code working, make sure each pair of motors drives the same direction. If not, swap the wires on one of them.

That code you posted for the L298N looks like a good start, but it got cut off. It looks like it might be reading the serial input to get direction commands. I would not even do that. Just make it drive A and B (or left and right) forwards 'forever'. Then you can check that both sides actually do go forwards and your power supply is able to give enough power to the motors.

Then bluetooth seems like a significant part of this project. Put aside the motors and try to get the bluetooth to send and receive data.

When you've tested every sensor/input/output separately, you will have a bunch of different Arduino sketches littered around your Arduino folder. Keep them all. You are going to get 90% done and then something won't work, so you need a quick test sketch to see if that sensor is still connected like you thought it was. I keep a folder of "Basic Tests" that excercises every sensor or output that I've ever attached to an Arduino.

thanks a lot for your explanation, i'll keep trying to test the code floating around. also i can't upload the code to the arduino unless i remove the sensor shield from the arduino then have to connect it again to test my code. is that normal or it's a cheap shield or what? please advise.

thanks again :slight_smile:

1 Like

The UNO must have pins 0 and 1 unencumbered when uploading code. The reset pin is also important but it's rare to ever attach anything to that. It is possible for some circuits to work on 0 and 1 without disabling uploads but usually detaching the sensor or whatever it is is what you have to do to make it upload.

But you also need 0 and 1 for Serial communication with the PC. If you have another sensor on those pins then you can't get Serial feedback from the sketch as it runs. You really should try to leave those pins unconnected in your circuit.

Bluetooth is usually a serial device. Since the UNO only has one hardware serial, you need to use SoftwareSerial to talk to the Bluetooth on another pair of pins. I hope you don't have any other serial devices, because it's impractical to run two copies of SoftwareSerial.