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.

You've only got 2 motor pins in your code, are you just running one motor for now? If so, here's what your code is doing:

• [motorPin1 & motorPin2 = HIGH] Nothing happens here since both sides of the motor driver are at the same voltage. Basically this acts as a weak electrical brake.
• [motorPin1 = LOW, motorPin2 = HIGH] Now we are getting somewhere. With Pin2 set to HIGH and Pin1 set to LOW current is made to flow through the motor, in this case from output2 to output1 (normally I'd label this as the 'reverse' direction, since it goes from output2 to output1).
• [motorPin1 & motorPin2 = LOW] This is the same as the option where both inputs were set to HIGH, the motor is held stationary, not very exciting.

If you want the motor to run in the 'forward' direction you need to do the opposite of the second command, ie: set motorPin1=HIGH and motorPin2=LOW. That way the current will flow from output1 to output2, thus turning the motor the other way.