# How to make the motor car move left-forward / left-backward/ right-forward/ right-backward

I'm using an Arduino Uno to make a motor car. And I successfully made it move forward / backward / left / right.

``````void forward(void) {
analogWrite(EA, 254); //speed 0 - 255
analogWrite(EB, 255); //speed 0 - 255
digitalWrite(EN2, LOW);
digitalWrite(EN3, HIGH);
digitalWrite(EN4, LOW);
digitalWrite(EN5, HIGH);
}

void backward(void) {
analogWrite(EA, 255); //speed 0 - 255
analogWrite(EB, 255); //speed 0 - 255
digitalWrite(EN2, HIGH);
digitalWrite(EN3, LOW);
digitalWrite(EN4, HIGH);
digitalWrite(EN5, LOW);
}

void left(void) {
analogWrite(EA, 180); //speed 0 - 255
analogWrite(EB, 180); //speed 0 - 255
digitalWrite(EN2, LOW);
digitalWrite(EN3, HIGH);
digitalWrite(EN4, HIGH);
digitalWrite(EN5, LOW);
}

void right(void) {
analogWrite(EA, 180); //speed 0 - 255
analogWrite(EB, 180); //speed 0 - 255
digitalWrite(EN2, HIGH);
digitalWrite(EN3, LOW);
digitalWrite(EN4, LOW);
digitalWrite(EN5, HIGH);
}
``````

The problem is, I found it is quite strange if I need to first press left/ right and press forward to turn around. I would like to make 4 extra buttons for left-forward / left-backward/ right-forward/ right-backward. Any ideas to create 4 extra functions for that?

Thanks for helping.

Update: Connection

``````int EN2 = 14;
int EN3 = 15;
int EN4 = 16;
int EN5 = 17;
int EA = 3;
int EB = 5;
``````

Update: Attempted code and hardware connection

``````void left_top(void) {
analogWrite(EA, 128); //speed 0 - 255
analogWrite(EB, 255); //speed 0 - 255
digitalWrite(EN2, LOW);
digitalWrite(EN3, HIGH);
digitalWrite(EN4, LOW);
digitalWrite(EN5, HIGH);
}

void right_top(void) {
analogWrite(EA, 255); //speed 0 - 255
analogWrite(EB, 128); //speed 0 - 255
digitalWrite(EN2, LOW);
digitalWrite(EN3, HIGH);
digitalWrite(EN4, LOW);
digitalWrite(EN5, HIGH);
}

void left_down(void) {
analogWrite(EA, 128); //speed 0 - 255
analogWrite(EB, 255); //speed 0 - 255
digitalWrite(EN2, HIGH);
digitalWrite(EN3, LOW);
digitalWrite(EN4, HIGH);
digitalWrite(EN5, LOW);
}

void right_down(void) {
analogWrite(EA, 255); //speed 0 - 255
analogWrite(EB, 128); //speed 0 - 255
digitalWrite(EN2, HIGH);
digitalWrite(EN3, LOW);
digitalWrite(EN4, HIGH);
digitalWrite(EN5, LOW);
}
``````

Left top / right top seem not to move at all, and right top / down is just similar to right / left.

And for hardware, I am using

Arduino AR 293d For the motor connection, it's like this: • What are the EA, EB, EN2-5 connected to? – rslite Mar 4 '15 at 22:04

Regarding the pins - I wanted to find out the physical (hardware) connections.

Pending an updated answer I presume the EA and EB will set the speed for two motors and EN2/EN3 and EN4/EN5 control the direction of the motors. If that's the case you could make the car turn sideways but changing the speed of only one of the motors. For example:

``````void forwardLeft(void) {
analogWrite(EA,128); //speed 0 - 255
analogWrite(EB,255); //speed 0 - 255
digitalWrite(EN2,LOW);
digitalWrite(EN3,HIGH);
digitalWrite(EN4,LOW);
digitalWrite(EN5,HIGH);
}
``````

I assume that EA is the left motor and EB is the right motor. This will make the right one rotate twice as fast as the left one so the car should go forward, but also turn left. If you keep this running the car should end up going in a circle. By making the `128` lower you will change how fast the car is turning.

The other functions should be similar.

Caveat emptor - of course this will mean nothing if the beginning assumptions are incorrect.

EDIT
Can't figure out why it doesn't work without having the thing in my hands. Have not found a schematic for that board, but intuitively it should work as described above.

Things you can do to try to figure out - try different numbers instead of 128, the speed of the motors might not vary linearly with the voltage. What you need to obtain is to have one motor go twice as fast as the other one and in the same direction. That should turn the car around.

• sorry just return from work , updated question , would you mind have a look? thanks – user782104 Mar 5 '15 at 14:42
• yes , the reason fail is because it reads only one character in arundio , the fuction itself has no problem – user782104 Mar 9 '15 at 1:10

just go to this link. Visit to https://www.arduino.cc/en/Tutorial/PWM

At first use PWM to both right side and left side motor. When right side motor spin/rotate at 255 value make leftside motor rotation speed at about 100 then you will see that it goes left-forward like wise you do rest of other.

Eg.

``````void setup()
{
//LEFT MOTOR
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT); // connect pwm pin of left motor at pin9

//right motor
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT); motor // connect pwm pin of right motor at pin12
}

void loop()
{
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
analogWrite(9, 100);
delay(1000);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
analogWrite(12, 255);
delay(1000);
}
``````