A4988 strange behavoiur

Good day.
I got a strange problem trying to rotate my nema 17 motor using a4988 driver and arduino due.
I used a pc power supply with 12V in order to power the driver and connected everything according to this scheme: http://b.pololu-files.com/picture/0J3360.600.png?0667de5863df6ae20561711d23752cb7

I used this code https://forum.pololu.com/download/file.php?id=487 and everything worked fine. At least the stepper rotated smoothly. But when I tryied to change the delay values in order to change the speed of rotation. However, it resulted into a vibrating and randomly rotating motor (it started vibrating hardly and instead of rotation it makes several random steps in one direction then back)

And how can I change the direction of rotation? What should I send to DIR pin for cw or ccw rotation?

{p.s}
if i use the simpliest possible code
like:
delay(500);
digitalWrite(stepPin, LOW);
digitalWrite(stepPin, HIGH);

in loop function

it makes one step forward and then one backwards.Even when dir is conected to the ground!!!

Hello.

What frequencies did you try stepping the motor at, both when it worked and when it did not?

-Derrill

Thanks for a reply.
Well, it worked with this code:

void loop()
{
int j;
// set the enablePin low so that we can now use our stepper driver.
digitalWrite(enablePin, LOW);
// wait a few microseconds for the enable to take effect
// (That isn't in the spec sheet I just added it for sanity.)
delayMicroseconds(2);
// we set the direction pin in an arbitrary direction.
digitalWrite(dirPin, HIGH);
for(j=0; j<=10000; j++) {
digitalWrite(stepPin, LOW);
delayMicroseconds(2);
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
}}

changing the last delayMicroseconds to less than 950 or higher than 1700 resulted into non-working stepper.

However, even when i used a working code I couldn’t control the direction. I tried to delete this line digitalWrite(dirPin, HIGH); or connect dir to the ground and even then after that every 10000steps it changed the direction randomly

I am not sure why the motor would stop stepping when you increase the delay, but the random change of direction makes me think your problem might be a bad connection somewhere. Can you post some pictures of your setup and of both sides of your A4988 stepper motor driver? What is the current rating of your 12V power supply? What is the current per phase rating of your stepper motor? Also, what do you have the current limit set to on your A4988 carrier?

-Derrill