Motors Activating on Uploading Program

When I upload my Sketch from my Computer to the Arduino, the motors activate at high speed for about three seconds. There is a command to tell the motors to stop; without this command, it continues indefinitely, or at least until we turn the motors off. It has been determined that the motors activate before the program is finished uploading, to the group's best estimations.

We are using a Roboclaw Motor Controller which we have connected to the Arduino. We are also using the Roboclaw Arduino Library (Found Here). There are examples of Arduino Roboclaw Programs in the Roboclaw User Manual.

Furthermore, when I begin the C# program in Visual Studio 2015, the motors also activate for a short period of time (about half a second), but there is a command in the program that orders the motors to stop.

Does anybody know how to fix this? Thank you.

My sketch for reference:

#include <SoftwareSerial.h>
#include <RoboClaw.h>

#define TX_PIN 10
#define RX_PIN 11
SoftwareSerial  serial1(RX_PIN, TX_PIN);
RoboClaw roboclaw(&serial1, 10000);

#define address 128
//Velocity PID coefficients.
#define Kp 650
#define Ki 115.5
#define Kd 0.0
#define qpps1 63028
#define qpps2 71854

int LED = 12;
int Buzz = 8;

// ****************************************************//////////////////
void blinkled(int x) {
  digitalWrite(LED, HIGH);
  delay(x);
  digitalWrite(LED, LOW);
  delay(x);
  digitalWrite(LED, HIGH);
  delay(x);
  digitalWrite(LED, LOW);
}
void Buzzer(int x) {
  digitalWrite(Buzz, HIGH);
  delay(x);
  digitalWrite(Buzz, LOW);
}
int b = 0;
int motor = 0;
int dir = 0;
// ****************************************************//////////////////
void setup() {
  Buzzer(1000);
  pinMode(Buzz, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(LED, OUTPUT);

  Serial.begin(115200);     //Output to computer
  serial1.begin(115200);     //Output to roboclaw
  blinkled(100);
  roboclaw.begin(115200);     // Begin Communications with RoboClaw
  //Set PID Coefficients
  roboclaw.SetM1VelocityPID(address,Kp,Ki,Kd,qpps1);
  roboclaw.SetM2VelocityPID(address,Kp,Ki,Kd,qpps2);    
  roboclaw.ForwardMixed(address, 0);
  roboclaw.TurnRightMixed(address, 0);
  
  //digitalWrite(relay, HIGH);
}
// ****************************************************//////////////////
void loop() {
  //Serial.println(Serial.available());
  if(Serial.available() > 0) // non blocking
  {
     b = Serial.read(); // read a byte
     Serial.println(b);
     Serial.write(b);
     int spd = b;
     motor = b & 0b00000001;
     b = b >> 1; // shift right one bit
     dir = b & 0b00000001;
     b = b >> 1; // shift right one bit
     if (motor == 0) // steering
     {
       if (dir == 0)
       {
         roboclaw.ForwardM2(address, spd);
       }
       else
       {
         roboclaw.BackwardM2(address, spd);
       }
     }
     else if(motor == 1) //wheels
     {
      if(dir == 0)
      {
        roboclaw.ForwardM1(address, spd);
      }
      else
      {
        roboclaw.BackwardM1(address, spd);
      }
     }
   }
  digitalWrite(LED, LOW);
  //roboclaw.SpeedM1M2(address, 0, 0);
  digitalWrite(LED, HIGH);//Command Status

//delay(100);
} // end of loop

Please post a hand drawn (not Fritzing) schematic diagram of the wiring.

Add a pin controlled relay to your esc data wire so that it doesn’t read the initial floating state of the pin before it’s set. Then as a final step in your setup loop, turn that pin on, to allow your Esc to only see valid signal.

Assuming of course you verified your esc doesn’t do that on its own on powerup . If it does you should put the relay on the output of the esc so that the arduino controls when the esc can power the motor.

Don’t understand why this is a problem , disconnect the motors whilst you upload the code, then reconnect them .

It has been determined that the motors activate before the program is finished uploading, to the group's best estimations.

did you use external powersupply for the motor?

On a reset all pins become floating - if you have actuators/motors driven from pins, use physical pull-down
resistors on those pins to prevent wild behaviour on reset or sketch upload.

Do not use pins 0 or 1 for motor control! They are the serial pins used to program a sketch

     motor = b & 0b00000001;
     b = b >> 1; // shift right one bit
     dir = b & 0b00000001;

There is a mistake with that code isnt?