Dual motors out of sync

Motors out of sync after wait time.

In my previous post i neglected to provide a full example of my code. Sorry

I have a Arduino Duemilanove (ATmega328P with a Rugged dual 2A motor shield. My goal is to drive a robot forward for 10 seconds. Then wait for 2 hours before returning while 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 out of sync motors. Basically one re-starts before the other.

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

Right now the motors are not in sync after the first forward 10 seconds. One starts then one second later the other starts.

This causes the robot not to return to its starting place.
Sorry, I don't know how to includ the code in a code screen.
Here is one of my many code attempts:

/* 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);

}

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);

}

Then wait for 2 hours

There are 7.2 million milliseconds in two hours, not 720 000.

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

You start one motor, then 10 seconds later you start the other.
Is that what you wanted?
This doesn't square with what you said here:

One starts then one second later the other starts.

I know there is a problem with my code. I reduces the wait seconds for now just to test. When I power on, both motors start in forward and in sync. That is what I want it to do. But when it stops and starts in reverse the motors are no longer are in sync. One stops and starts before the other. I know there are many robot platforms with two wheel and track designs. This should be very simple but i am new and have tried every thing i could find on the internet.

This robot simply needs to travel from my kitchen to a hospital bed in the next room. Then 2 hours later return back to the kitchen. It does not even need to turn.

When I power on, both motors start in forward and in sync

Even with the ten second delay between enabling mototr 1 and motor 2?
How are they wired?

In the code that I wrote the motors work by going forward 10 seconds and in sync. They run non stop.

The previous code was one of many attempts to complete the code that did not work.

Here is the basic code that starts the motors forward and in sync.

/* 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);

}

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, HIGH); // Set Motor 2 forward direction
analogWrite(EN2_PIN, 255); // Motor 2 on in forward direction
delay(10000);

}

I’m sorry, there must be something fundamental that I don’t understand.

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

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

Here you set motor 1 running, then delay ten seconds, then start motor 2.

In what sense are they “in sync”?

Why do you have two similar topics active?

Kari

it woulds be easier for you if you split up your delay so it equels the two hours, that and as a note, you probably cant get twos hours of life if you are using a 9V