how to control 2 motors: one forward other reverse

Hello guyz.. I am new here..this is my 1st post..i m using arduino 1st time so have some problems to control my robot..so need your help..Here my code:

int a=2; 
int b=3;
int c=4;
int d=5;
int obj;
int EN1=10;
int EN2=11;

void setup()
{ 
 Serial.begin(9600);
 pinMode(a,OUTPUT);
 pinMode(b,OUTPUT); 
 pinMode(c,OUTPUT);
 pinMode(d,OUTPUT);
}

void loop()
{
  while (Serial.available()==0);
    obj=Serial.read();
      if(obj=='W')  //move motor 1 & 2 forward
  {
   analogWrite(EN1,255); 
   digitalWrite(a,HIGH);
   digitalWrite(b,LOW);
   analogWrite(EN2,255); 
   digitalWrite(c,HIGH);
   digitalWrite(d,LOW);
      }
     else if(obj=='S') //move motor 1 & 2 Backward
  {
   analogWrite(EN1,255);  
   digitalWrite(a,LOW);
   digitalWrite(b,HIGH); 
   analogWrite(EN2,255); 
   digitalWrite(c,LOW);
   digitalWrite(d,HIGH);
     
  }
    else if(obj=='A') //move motor 1 backward & motor 2 forward(LEFT)
  {
   analogWrite(EN1,255); 
   digitalWrite(a,LOW);
   digitalWrite(b,HIGH);
   analogWrite(EN2,255); 
   digitalWrite(c,HIGH);
   digitalWrite(d,LOW);
  }
    else if(obj=='D') //move motor 2 backward & motor 1 forward(RIGHT)
  {
    analogWrite(EN1,255);
   digitalWrite(a,HIGH);  
   digitalWrite(b,LOW);
   analogWrite(EN2,255);
   digitalWrite(d,LOW);
   digitalWrite(c,HIGH);
     }

  }

bt here i can move motor forward and reverse bt cant move left or right.. It don't run..I'm using Duemilanove ATmega328. What am I doing Wrong..??

You haven't explained how the motor controls work so I don't know if your code will do what you think it will do.

I suggest writing a shorter sketch that has no IF statements and just makes the pair of motors run in one of the 4 combinations - so that you can try out the combinations without the confusion of the IF statements.

When you have that figured out, then move on to the IF statements. If it doesn't work then you will know the problem is with your IF logic.

...R

a, b, c, d etc are very poor choices of symbolic names for the pins - give them
names that explain their purpose.

Like “1”, “2”,“3”,“4”…

I think you need an "H-Bridge", you can build your own or you can buy a computer chip called L293 or L298 (some have letters on the end, like D and N). You can also buy for example, I have a L298N motor driver board (L293D is a lower voltage version) for doing exactly what you want, controlling twin motors independently with the Arduino so it can go forwards/backwards and turn.

For variable names, I would suggest something like LmotorF/LmotorB and RmotorF/RmotorB, for left/right motor and Forward/backward for positive/negative wires/pins.

When you decide to tell us the motor specs let us know. At the moment we have no idea what the rating is for these motors.

KalELonRedkryptonite:
I think you need an "H-Bridge",

The original post implied that the motors do operate so I assume the OP already has a driver of some sort.

...R

bt here i can move motor forward and reverse bt cant move left or right.. It don't run..I'm using Duemilanove ATmega328. What am I doing Wrong..??

Can you draw a schematic of your circuit and take a photo with your cell phone and post it ?
Any data on your motors or driver would be helpful.

Robin2:

KalELonRedkryptonite:
I think you need an "H-Bridge",

The original post implied that the motors do operate so I assume the OP already has a driver of some sort.

...R

Sure, all he has to do is supply power to them and they will go forward or backwards with an Arduino, but he says he can't turn left or right and I simply suspect that is because he can't control them independently of each other, which would imply no H-Bridge. It sounds to me like they either both move forward or they both move backward but not one at a time which would cause turning.

hence this post…

Can you draw a schematic of your circuit and take a photo with your cell phone and post it ?

KalELonRedkryptonite:
Sure, all he has to do is supply power to them and they will go forward or backwards with an Arduino

Which proves there is a h-bridge.

The problem is likely to be with the code.

But the OP seems to be less interested than we are :slight_smile:

...R

Why would it need an Hbridge to simply use forward/reverse? You don't to go in a straight line, but you would if you need to control them separately.

And yes, he seems to have gone AWOL or already fixed it and didn't bother to return. I need to figure out this kind of topic for myself, so I am finding all the information I can to learn from.

Why would it need an Hbridge to simply use forward/reverse?

How else is it going to reverse ? (relay with the +/- on separate contacts ?)
If it only has a single ended power supply (as opposed to bi-polar) , you would need an H-BRIDGE to reverse. If you had a bipolar power supply you could control which rail (+/-) supplies power to the motor thereby making it possible to reverse. I don't think it
has a bi-polar supply , hence either turning is accomplished by leaving one motor(wheel) stationary and driving the other OR turning is accomplished by reversing one while driving the other. If you know another way , let's hear it.

No I don't know another way, I'm new to electronics and I did not realize you needed an HB for reverse. Thank you for explaining in your post.

Maybe this will help clear it up...

problem solved guyz...thnx for help...

If it's not too much trouble could you tell us what you did to "solve" the problem ?