Dual Motor control

I have a Arduino Duemilanove (ATmega328P with a Rugged dual 2A motor sheld. My goal is to drive a robot forward for 10 seconds. Then wait for 2 hours before returning in reverse to its starting point. Seems simple the motors start at the same time in forward. But everything I have tried to make them stop and wait 2 hours causes in out of sync motors. Basically one starts before the other.

/* Define which board you are using. Only uncomment one of the
following #define's. Or comment them all and let this file
guess the board based on the processor:
ATmega328P --> Duemilanove/Uno
ATmega324P --> Gator
ATmega1280 --> Mega (also ATmega2560)
/
//#define BOARD 0 /
Arduino Duemilanove/Uno (ATmega328P) /
//#define BOARD 1 /
Arduino Mega (ATmega1280) /
//#define BOARD 2 /
Rugged Circuits Gator (ATmega324P) */

// In case no board is defined, guess from the processor
#ifndef BOARD

if defined(AVR_ATmega328P)

define BOARD 0

elif defined(AVR_ATmega1280) || defined(AVR_ATmega2560)

define BOARD 1

elif defined(AVR_ATmega324P)

define BOARD 2

else

error You must define BOARD near the top of this file

endif

#endif

#if (BOARD!=0) && (BOARD!=1) && (BOARD!=2)
#error Unknown board
#endif

// Enable (PWM) outputs
#define EN1_PIN 3
#define EN2_PIN 11

// Direction outputs
#define DIR1_PIN 12
#define DIR2_PIN 13

void setup()
{
// Configure all outputs on for now
pinMode(EN1_PIN, OUTPUT); digitalWrite(EN1_PIN, HIGH);
pinMode(EN2_PIN, OUTPUT); digitalWrite(EN2_PIN, HIGH);
pinMode(DIR1_PIN, OUTPUT); digitalWrite(DIR1_PIN, HIGH);
pinMode(DIR2_PIN, OUTPUT); digitalWrite(DIR2_PIN, HIGH);

digitalWrite(DIR1_PIN, HIGH); // Set Motor 1 forward direction
analogWrite(EN1_PIN, 255); // Motor 1 on in forward direction
delay(10000);

digitalWrite(DIR1_PIN, HIGH); // Set Motor 2 forward direction
analogWrite(EN2_PIN, 255); // Motor 2 on in forward direction
delay(10000);

}

Just have a single drive axle with one motor.
edit: or just wire the two motors in parallel.

Due to the large size of the tracked robot 27" lenght 15" width and 48" height, I need to use two high power motors.

The motoers are not in sync after the first forward 10 seconds. One starts then one second later the other starts.

This causes the robot not ot return to uts starting place.

Is there more code? I don't see anything to make it wait.

// Configure all outputs on for now
pinMode(EN1_PIN, OUTPUT); digitalWrite(EN1_PIN, HIGH); << so you start motor1 here
pinMode(EN2_PIN, OUTPUT); digitalWrite(EN2_PIN, HIGH); << you start motor2 here
pinMode(DIR1_PIN, OUTPUT); digitalWrite(DIR1_PIN, HIGH); << both going the same direction apparantly
pinMode(DIR2_PIN, OUTPUT); digitalWrite(DIR2_PIN, HIGH); << I think I would define the direction first tho ...

digitalWrite(DIR1_PIN, HIGH); // Set Motor 1 forward direction << set the direction again ??
analogWrite(EN1_PIN, 255); // Motor 1 on in forward direction << and then you have motor1 keep on going here
delay(10000); << you delay 10 seconds, but don't actually change any motor controls,
<< so one would imagine it keeps going ..

<<< does the pinMode change automatically if you do digitalWrite on a pin and then analogWrite?? Something to check >>>

digitalWrite(DIR1_PIN, HIGH); // Set Motor 2 forward direction << set the same diretion again ??
analogWrite(EN2_PIN, 255); // Motor 2 on in forward direction << so motor 1 is still going and you command motor2 to keep going
delay(10000); << and more delay with no change in motor commands

Where is void loop() { do other commands } ??
Need to work on your logic here.

set the direction
enable the motors
delay for 10 seconds while moving

disable the motors

read the time
when 2 hrs elapsed

set the direction
enable the motors
delay while moving
disable the motors

Do it all in setup if you just want to run once, that's fine
then just sit in
void loop() {}
forever after that

There might be mechanical problem, more friction on the other side? Something could resist it to roll freely.

Cheers,
Kari

For now it is set to start and keep going. This is because everything I have wrote to wait 2 hour and then reverse has not worked as planned. Both motors start and run forward for 10 seconds but then do not restart together. Here is one of my code Examples:

/* Define which board you are using. Only uncomment one of the
following #define's. Or comment them all and let this file
guess the board based on the processor:
ATmega328P --> Duemilanove/Uno
ATmega324P --> Gator
ATmega1280 --> Mega (also ATmega2560)
/
//#define BOARD 0 /
Arduino Duemilanove/Uno (ATmega328P) /
//#define BOARD 1 /
Arduino Mega (ATmega1280) /
//#define BOARD 2 /
Rugged Circuits Gator (ATmega324P) */

// In case no board is defined, guess from the processor
#ifndef BOARD

if defined(AVR_ATmega328P)

define BOARD 0

elif defined(AVR_ATmega1280) || defined(AVR_ATmega2560)

define BOARD 1

elif defined(AVR_ATmega324P)

define BOARD 2

else

error You must define BOARD near the top of this file

endif

#endif

#if (BOARD!=0) && (BOARD!=1) && (BOARD!=2)
#error Unknown board
#endif

// Enable (PWM) outputs
#define EN1_PIN 3
#define EN2_PIN 11

// Direction outputs
#define DIR1_PIN 12
#define DIR2_PIN 13

void setup()
{
// Configure all outputs off for now
pinMode(EN1_PIN, OUTPUT); digitalWrite(EN1_PIN, HIGH);
pinMode(EN2_PIN, OUTPUT); digitalWrite(EN2_PIN, HIGH);
pinMode(DIR1_PIN, OUTPUT); digitalWrite(DIR1_PIN, HIGH);
pinMode(DIR2_PIN, OUTPUT); digitalWrite(DIR2_PIN, HIGH);

// Both motors off for now
analogWrite(EN1_PIN, 0);
analogWrite(EN2_PIN, 0);

}

void loop()

{
digitalWrite(DIR1_PIN, HIGH); // Set Motor 1 forward direction
analogWrite(EN1_PIN, 255); // Motor 1 on in forward direction
delay(10000);

digitalWrite(DIR2_PIN, LOW); // Set Motor 2 forward direction
analogWrite(EN2_PIN, 255); // Motor 2 on in forward direction
delay(10000);

analogWrite(EN1_PIN, 0); // Motor 1 off
delay(720000);

analogWrite(EN2_PIN, 0); // Motor 2 off
delay(720000);

digitalWrite(DIR1_PIN, HIGH); // Set Motor 1 reverse direction
analogWrite(EN1_PIN, 255); // Motor 1 on in reverse direction
delay(10000);

digitalWrite(DIR2_PIN, HIGH); // Set Motor 2 reverse direction
analogWrite(EN2_PIN, 255); // Motor 2 on in reverse direction
delay(10000);

analogWrite(EN1_PIN, 0); // Motor 1 off
delay(720000);

analogWrite(EN2_PIN, 0); // Motor 2 off
delay(720000);

}

First things first. Get out a magnifying glass, and figure out what kind of microcontroller you have. Then, get rid of this crap:

/* Define which board you are using. Only uncomment one of the
   following #define's. Or comment them all and let this file
   guess the board based on the processor:
      ATmega328P --> Duemilanove/Uno
      ATmega324P --> Gator
      ATmega1280 --> Mega (also ATmega2560)
 */
//#define BOARD  0   /* Arduino Duemilanove/Uno (ATmega328P) */
//#define BOARD  1   /* Arduino Mega (ATmega1280) */
//#define BOARD  2   /* Rugged Circuits Gator (ATmega324P) */

// In case no board is defined, guess from the processor
#ifndef BOARD
#  if defined(__AVR_ATmega328P__)
#    define BOARD 0
#  elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#    define BOARD 1
#  elif defined(__AVR_ATmega324P__)
#    define BOARD 2
#  else
#    error You must define BOARD near the top of this file
#  endif
#endif

#if (BOARD!=0) && (BOARD!=1) && (BOARD!=2)
#error Unknown board
#endif

The people that you stole the code from may want there code to deal with a variety of boards. The IDE targets a specific board. If you don't know what it should be targeting, you should not be building robots.

Second, figure out what you want the code to do, and make it do that. Understand what the commands do, and stop guessing.

void setup()
{
  // Configure all outputs off for now
  pinMode(EN1_PIN, OUTPUT); digitalWrite(EN1_PIN, HIGH);
  pinMode(EN2_PIN, OUTPUT); digitalWrite(EN2_PIN, HIGH);
  pinMode(DIR1_PIN, OUTPUT); digitalWrite(DIR1_PIN, HIGH);
  pinMode(DIR2_PIN, OUTPUT); digitalWrite(DIR2_PIN, HIGH);

  // Both motors off for now
  analogWrite(EN1_PIN, 0);
  analogWrite(EN2_PIN, 0);
}

Why are you turning the motors on full speed before you set a direction? Why, after you do set a direction, do you turn them back off?

If you don't want them on, don't turn them on.

  digitalWrite(DIR1_PIN, HIGH);  // Set Motor 1 forward direction
  analogWrite(EN1_PIN, 255);    // Motor 1 on in forward direction
  delay(10000);

  digitalWrite(DIR2_PIN, LOW);  // Set Motor 2 forward direction
  analogWrite(EN2_PIN, 255);    // Motor 2 on in forward direction
  delay(10000);

Every pass through loop, you run one motor in one direction for 10 seconds, then turn the other motor on for 10 seconds, going the other direction.

And the robot fails to go in a straight line, and you can't figure out why. Give me a break.

  analogWrite(EN1_PIN, 0);      // Motor 1 off
  delay(720000);

  analogWrite(EN2_PIN, 0);      // Motor 2 off
  delay(720000);

Then, you turn one motor off, (think you are going to) wait two hours and turn the other off. Is that really what you want to do?

You know, it is possible to do two things, then wait...

As for the "think you are going to" comment above, all constants are treated as integers, unless you tell the compiler otherwise.

Look at the range of values that fit in an int. Does 720000 fit in an int? No. You need to look at the page above to figure out how to define a wait time of two hours.

http://arduino.cc/forum/index.php/topic,53128.0.html

Isn't it about time there was a cross-posters' blacklist?

I'm really starting to get fed-up with this.

wait two hours

sp. "twelve minutes"