Inconsistent stepper motors!

Hi, every time I run a simple code to run a stepper motor I get different outcomes. Here is the code I've been using.

#define STEP_PIN         54
#define DIR_PIN          55
#define ENABLE_PIN       38

int i = 0;

void setup() {
  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  pinMode(ENABLE_PIN, OUTPUT);
  
  digitalWrite(ENABLE_PIN, LOW);
  
}

void loop(){
  delay(1); 
  if ( i < 3230){ //it appears that for i = 3230 is for one rotation
    digitalWrite(DIR_PIN, HIGH);
   
    i++;
  }
else{
  digitalWrite(ENABLE_PIN, HIGH);
  //digitalWrite(DIR_PIN, LOW);
  
}
    digitalWrite(STEP_PIN    , HIGH);
      
      
 
      
      
      
      
      
    digitalWrite(STEP_PIN    , LOW);
  
}

when I try to run it for multiple rotations, it starts to sputter through the steps and is never consistent!

The motor that I'm using is a 1.8 degree stepper motor controlled with a RAMPS 1.4 and A4988 Pololu Stepper Motor Driver Carrier with and Mega 2560 arduino.

Please let me know how I can have complete, consistent, and precise control on this stepper motor! If you have any sample code I would greatly appreciate it!!!!!!

Hi murf83,

I don't have any actual code to share, but have a few things for you to check:

  1. You set the enable pin LOW. Verify that LOW enables the motor.
  2. You loop 3230 times for 1 rotation. In that loop all you do is set the DIR pin to high. It is never set LOW. Also, should you pulse the STEP pin instead?
  3. When you do pulse the STEP pin (after the if statement), you don't give the pulse a duration.

This is what I would expect (Caveat - I have not compiled this or tested it. The real details depend on your specific motor.):

#define STEP_PIN         54
#define DIR_PIN          55
#define ENABLE_PIN       38

const int ROTATION = 3230; // pulses in 1 rotation
const int PULSEWIDTH = xxx;  // each pulse is xxx ms long
const int BETWEEN = xxx; // ms between pulses

void setup() {
  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  pinMode(ENABLE_PIN, OUTPUT);
  
  digitalWrite(ENABLE_PIN, LOW);  // low  = enable motor control
  digitalWrite(DIR_PIN, HIGH);  // high = direction xxx
}

void loop(){

  for (int pulseCnt=0; pulseCnt<ROTATION; pulseCnt++)
  {
    digitalWrite(STEP_PIN    , HIGH);
    delay(PULSEWIDTH);
    digitalWrite(STEP_PIN    , LOW);
    delay(BETWEEN);
  }
}