Let's Make Robots!

Arduino Motor Control Code Help

I am currently using a arduino duemilanove, and it is awesome. I have been playing around with it for a couple of days now, and I really like it. Anyways I figured it was time to start to building a robot. I decided to start with the motors. For the motor control I decided to use guibot's tutorial. It worked fine, but the motors only traveled in one direction. Because I wanted the motors to also travel in reverse too I decided to experiment with my own code (on only one motor) that looks something like this:

int motorPin1 = 2;
int motorPin2 = 3;
void setup(){
pinMode (motorPin1, OUTPUT);
pinMode (motorPin2, OUTPUT);
}
void loop(){
  digitalWrite (motorPin1, HIGH);
  digitalWrite (motorPin2, HIGH);
 delay ;1000;
digitalWrite (motorPin1, LOW);
digitalWrite (motorPin2, HIGH);
delay ;1000;
digitalWrite (motorPin1, LOW);
digitalWrite (motorPin2, LOW);
delay ;1000;
}

Not suprisingly, it didn't work. I feel like there is something obvious that I'm missing. The motors go in one direction perfectly, but not in reverse. Can somebody help me to get these motors to also go in reverse?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

For anyone who doesn;t recognize the drawing, it was done on Fritzing.

Fritzing is a free breadboard drawing program for the Arduino. 

Yes, I do have the enable pins wired to positive. That was pretty stupid, I should have known that the enable pin was on the l293d. Anyways sounds like  I need external power. I am pretty sure I attach it to GND and VIN, but I'm not sure how much to use...

 I just assumed you were using the motor driver, seeing as you pointed to GUI's Tutorial.

Enable 1 is for the left side (first motor)

Enable 2 is for the right sight (second motor)

Both enables should be tied to the positive on your power supply. And with the motors you're using, as everyone suggested, you need a second supply, you never specified whether you'd used a second supply or not.

If you could post a picture, or maybe use Fritzing and show us how you have your circuit hooked up, may be able to answer a few more questions. I'm guessing it's your power supply though, definately wouldn't risk trying to pull that from your USB drive, or Arduino. When in doubt, use an external supply! (my new saying?)

L293D.gif

Thanks guys, I still have a couple of problems with the motor control though :(.

Telefox: I am using the power from the usb cable. Is this bad/not enough power? Also, I don't believe the problem is with the Arduino because I just got it 3 days ago. I'm not sure if it matters, but it is using the atmega328 not atmega168. I also don't think that the problem is with the code after reading that CaptainObvious tried the code with a led and it worked. I would try all the testing stuff, but I don't own a multimeter. I know, I should probably get one.

CaptainObvious: I cant seem to find an enable pin on my board. Is this some specific pin, or does one not exsist on the new duemilanove's. I am using the same motors as I did on my first robot: http://www.solarbotics.com/products/gmpw_deal/

I think it must be something with the motor reversing. Because it spins fine while regular forward code is put in. Anways I will keep working on it.

The USB only provides (at best) 500mA, which may not be enough. The motors you listed are capable of drawing over 500mA by themselves, but that still doesn't explain the odd 'forward only' behaviour...
The enable pin is on the L293 motor driver chip. You are using a motor driver right? The arduino pins can`t source or sink enough current to drive a motor, you risk damaging the chip without using a driver chip or H bridge.

Also, make sure your ENABLE pin is connected to the V+ off your external supply.  Giving 5volts to the enable pin, well only enables 5 volts!

That was my big problem, I didn't realize that for the longest time when I was running a fan. And of course, make sure the Grounds are connected to your arduino, that can also cause what seems like low-power. And I would use an external supply, rather than battery, for testing purposes at least.

 Well I can say for sure it's not the code, I just uploaded it and tested it and works fine. (only tested using some LEDs)

 

But what kind of motor are you using? Do keep in mind, not all motors are meant to be reversed. If you took it out of a, say mini-fan of some-sorts, from my experience, the motors only like to go one-way. They WILL go reverse, but nowhere near the power of their forward. Assuming because they weren't designed for it. :D

If you don't get it figured out, I'll see what I can do to jimmy-up a ghetto-test rig to help you out with some code.

Best of luck!

Are you using batteries or a DC supply? I always check my battery voltage first when things start acting sluggish.
If the power supply itself is ok, the next thing I'd check is that the Arduino outputs are changing like they're supposed to. If you get a voltage that jumps up and down from nearly zero to nearly the V+ supply about once a second then you can safetly say the problem is not with the code or the Arduino itself.

Thanks Telefox and CaptainObvious, that really cleared things up. Yes, my code was only for one motor. Here is my new code:

int motorPin1 = 2;
int motorPin2 = 3;
void setup(){
pinMode (motorPin1, OUTPUT);
pinMode (motorPin2, OUTPUT);
}
void loop(){
  digitalWrite (motorPin1, LOW);
  digitalWrite (motorPin2, HIGH);
 delay(1000);
digitalWrite (motorPin1, HIGH);
digitalWrite (motorPin2, LOW);
delay(1000);
digitalWrite (motorPin1, LOW);
digitalWrite (motorPin2, HIGH);
delay(1000);
}

 However now using this code the motor is barely turning and I can't really tell which way it is going. Am I not getting enough current to the motor? If so how could I correct this problem?

Just something I noticed aboot your code (no I'm not Canadian:D) was with your Delays, you could use delay(1000); instead of using two seperate lines of code, will save you a byte! :D

But as for the motors, what Telefox suggested should work for you.