# Dual motors out of sync

**URL:** <https://forum.arduino.cc/t/dual-motors-out-of-sync/53217>\
**Category:** Programming\
**Created:** [February 22, 2011, 8:02am UTC](https://forum.arduino.cc/t/dual-motors-out-of-sync/53217 "2011-02-22T08:02:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 22, 2011, 8:02am UTC](https://forum.arduino.cc/t/dual-motors-out-of-sync/53217/1 "2011-02-22T08:02:55Z")

</div>

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

}

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 22, 2011, 8:25am UTC](https://forum.arduino.cc/t/dual-motors-out-of-sync/53217/2 "2011-02-22T08:25:24Z")

</div>

> Then wait for 2 hours

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

```arduino
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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 22, 2011, 8:46am UTC](https://forum.arduino.cc/t/dual-motors-out-of-sync/53217/3 "2011-02-22T08:46:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 22, 2011, 9:02am UTC](https://forum.arduino.cc/t/dual-motors-out-of-sync/53217/4 "2011-02-22T09:02:28Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 22, 2011, 9:21am UTC](https://forum.arduino.cc/t/dual-motors-out-of-sync/53217/5 "2011-02-22T09:21:53Z")

</div>

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

}

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 22, 2011, 9:43am UTC](https://forum.arduino.cc/t/dual-motors-out-of-sync/53217/6 "2011-02-22T09:43:54Z")

</div>

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

```cpp
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"?

---

<div class="post-metadata">

**Author:** ![GaryP](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@GaryP](https://forum.arduino.cc/u/GaryP)\
**Post date:** [February 22, 2011, 10:17am UTC](https://forum.arduino.cc/t/dual-motors-out-of-sync/53217/7 "2011-02-22T10:17:20Z")

</div>

Why do you have two similar topics active?  
[http://arduino.cc/forum/index.php/topic,53114.0.html](http://arduino.cc/forum/index.php/topic,53114.0.html)

Kari

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 22, 2011, 5:36pm UTC](https://forum.arduino.cc/t/dual-motors-out-of-sync/53217/8 "2011-02-22T17:36:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 7:28pm UTC](https://forum.arduino.cc/t/dual-motors-out-of-sync/53217/9 "2021-05-05T19:28:14Z")

</div>


