Motors Acting Weird

Hi everyone! I am new here and i am new with arduino. My name is Atakan. I am studying mechanical engineering in Turkey. I am working on a scara robot arm project for my thesis. I know very little about electronics. I bought these;
Arduino Mega 2560 Rev3,
131:1 Metal Gearmotor 37Dx57L mm with 64 CPR Encoder Pololu - 131:1 Metal Gearmotor 37Dx57L mm 12V with 64 CPR Encoder (No End Cap)
67:1 Metal Gearmotor 37Dx54L mm with 64 CPR Encoder Pololu - 70:1 Metal Gearmotor 37Dx54L mm 12V with 64 CPR Encoder (No End Cap)
Pololu Dual VNH5019 Motor Driver Shield for Arduino Pololu Dual VNH5019 Motor Driver Shield for Arduino (ash02a)

I downloaded Arduino library for the Pololu dual VNH5019 motor driver shield and here it is.

#include "DualVNH5019MotorShield.h"

DualVNH5019MotorShield md;

void stopIfFault()
{
if (md.getM1Fault())
{
Serial.println("M1 fault");
while(1);
}
if (md.getM2Fault())
{
Serial.println("M2 fault");
while(1);
}
}

void setup()
{
Serial.begin(115200);
Serial.println("Dual VNH5019 Motor Shield");
md.init();
}

void loop()
{
for (int i = 0; i <= 400; i++)
{
md.setM1Speed(i);
stopIfFault();
if (i%200 == 100)
{
Serial.print("M1 current: ");
Serial.println(md.getM1CurrentMilliamps());
}
delay(2);
}

for (int i = 400; i >= -400; i--)
{
md.setM1Speed(i);
stopIfFault();
if (i%200 == 100)
{
Serial.print("M1 current: ");
Serial.println(md.getM1CurrentMilliamps());
}
delay(2);
}

for (int i = -400; i <= 0; i++)
{
md.setM1Speed(i);
stopIfFault();
if (i%200 == 100)
{
Serial.print("M1 current: ");
Serial.println(md.getM1CurrentMilliamps());
}
delay(2);
}

for (int i = 0; i <= 400; i++)
{
md.setM2Speed(i);
stopIfFault();
if (i%200 == 100)
{
Serial.print("M2 current: ");
Serial.println(md.getM2CurrentMilliamps());
}
delay(2);
}

for (int i = 400; i >= -400; i--)
{
md.setM2Speed(i);
stopIfFault();
if (i%200 == 100)
{
Serial.print("M2 current: ");
Serial.println(md.getM2CurrentMilliamps());
}
delay(2);
}

for (int i = -400; i <= 0; i++)
{
md.setM2Speed(i);
stopIfFault();
if (i%200 == 100)
{
Serial.print("M2 current: ");
Serial.println(md.getM2CurrentMilliamps());
}
delay(2);
}
}

At first, demo ran perfectly. One motor is working firstly and when in finishes it's work after a delay second motor works. But after a few repeat, only one motor is working now and it is acting weird. It turns on it's own and does random stuff. My friend has arduino uno rev3 and demo works perfectly on his board. I couldn't fix it on my mega 2560 rev3. I know very little about arduino and i've never worked with this stuff before. I appreciate every suggestion. Please help me, i have very limited time.

How are you powering the motors/motor shield? Is the power supply adequate for the motors in question?

MarkT:
How are you powering the motors/motor shield? Is the power supply adequate for the motors in question?

I need 12V and max. 10A for motors and motor shield. I am powering them with a 12V, 16.5A DC output power supply.

So what is the stall current for the motor(s)?